




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)專心-專注-專業(yè)精選優(yōu)質(zhì)文檔-傾情為你奉上專心-專注-專業(yè)期末復習試題(二)程序題1、編寫程序創(chuàng)建Point類,要求如下:(1)double類型的數(shù)據(jù)域x和y分別表示點的坐標;(2)x、y的get和set方法;(3)一個無參構(gòu)造方法;(4)一個創(chuàng)建點對象同時指定x和y坐標的有參的構(gòu)造方法;(5)一個名為distance(Point p)的方法,返回從該點到指定點之間的距離;(6)一個名為distance(double x, double y)的方法,返回從該點到指定x和y坐標的指定點之間的距離。解題要求:編寫測試類,分別調(diào)用兩
2、個distance方法,計算:(1)點(2,3)到(10,30)之間的距離并顯示;(2)點(4,5)到(20,50)之間的距離并顯示。public class TestPoint public static void main(String a)System.out.println(前兩點間的距離是 + new Point(2,3).distance(new Point(10,30);System.out.println(后兩點間的距離是 + new Point(4,5).distance(20,50);class Pointprivate double x,y;public Point()p
3、ublic Point(double x, double y)this.x = x;this.y = y;public double getX() return x;public void setX(double x) this.x = x;public double getY() return y;public void setY(double y) this.y = y;public double distance(Point p)return Math.sqrt(x計算通過參數(shù)傳遞進來的p點與當前點的距離-p.getX()*(x-p.getX() + (y-p.getY()*(y-p.g
4、etY();計算通過參數(shù)傳遞進來的p點與當前點的距離public double distance(double x, double y)return Math.sqrt(x-this.x)*(x-this.x) + (y-this.y)*(y-this.y);2、下圖是課程類Course的UML類圖,說明如下:(1)成員變量包括課程名稱(courseName)和選課學生(students),選課學生存放在ArrayList鏈表中。(2)包括成員變量的set和get方法。(3)一個輸出課程信息的方法toString(),可以輸出課程名稱、選課學生名單和選課人數(shù)。(4)一個添加學生的方法addSt
5、udent(String student)。(5)一個查詢選課學生數(shù)量的方法getNumberOfStduents()解題要求:編寫測試類,創(chuàng)建課程對象,添加3個選課學生,按照如下提示輸出課程信息。import java.util.ArrayList;public class TestCourse public static void main(String arg)Course c = new Course(面向?qū)ο蠹夹g);c.addStudents(張三);c.addStudents(李四);c.addStudents(王五);System.out.println(c.toString()
6、; class Courseprivate String courseName;private ArrayList students = new ArrayList();public Course(String courseName)this.courseName = courseName;public void addStudents(String student)students.add(student);public ArrayList getStudents()return students;public int getNumberOfStudents()return students
7、.size();public String getCourseName()return courseName;public void dropStudent(String student)students.remove(student);public String toString()String s = ;局部變量在使用前需要初始化局部變量在使用前需要初始化for(int i = 0; i students.size(); i+)s += + students.get(i);return 課程名稱 + courseName + n + 選課人數(shù) + getNumberOfStudents()
8、 + n + 學生名單 + s;3、下圖描述了兩個類:Line(線段)和Point(點),以及兩個類之間的關聯(lián)關系,一條線段對象由對應的兩個點對象組成。解題要求:要求如下:(1) 編寫Line(線段)和Point(點)兩個類的代碼,注意滿足封裝的需求。將數(shù)據(jù)隱藏,通過方法訪問數(shù)據(jù)。(2) 使用Line類的Line(x1: int, y1: int, x2: int, y2: int)方法,創(chuàng)建Line對象,端點是(10,20)、(30,40),計算并輸出線段的長度。(3)使用Line類的Line(p1: Point,p2: Point)方法,創(chuàng)建Line對象,端點是(3,4)、(9,,10),
9、計算并輸出線段的長度。public class UseLine public static void main(String arg) Line lFirst = new Line(10,20,30,40); System.out.println(線段的長度為 + lFirst.getLength(); Line lSecond = new Line(new Point(3,4),new Point(9,10); System.out.println(線段的長度為 + lSecond.getLength(); class Pointprivate int x; private int y; p
10、ublic Point(int x, int y)this.x = x;this.y = y;public int getX() return x;public void setX(int x) this.x = x;public int getY() return y;public void setY(int y) this.y = y;class Lineprivate Point point1; private Point point2; public Line() public Line(int x1, int y1, int x2, int y2)point1 = new Point
11、(x1, y1);注意不要寫成:point1.setX(x1);point1.setY(y1);注意不要寫成:point1.setX(x1);point1.setY(y1);在調(diào)用point1的方法的時候,piont1一定在某個位置通過new創(chuàng)建出來,否則出現(xiàn)空指針錯誤。如果Point類有參數(shù)為空的構(gòu)造方法,則把上面的代碼如下修改就可以了 :point1 = new Point();point1.setX(x1);point1.setY(y1);point2 = new Point(x2, y2);public Line(Point p1, Point p2)point1 = p1;point
12、2 = p2;public double getLength()return Math.sqrt(point1.getX()-point2.getX()*(point1.getX()-point2.getX() + (point1.getY()-point2.getY()*(point1.getY()-point2.getY();public Point getPoint1() return point1;public void setPoint1(Point point1) this.point1 = point1;public Point getPoint2() return point2
13、;public void setPoint2(Point point2) this.point2 = point2;4、創(chuàng)建矩形類Rectangle,包括(1)兩個名為width和height的double型數(shù)據(jù)域,它們分別表示矩形的寬和高.width和height的默認值都為1.(2)創(chuàng)建默認矩形的無參構(gòu)造方法。(3)一個創(chuàng)建width和height為指定值的矩形的構(gòu)造方法。(4)一個名為getArea()的方法返回這個矩形的面積(5)一個名為getPerimeter()的方法返回周長。解題要求:編寫測試程序,創(chuàng)建兩個Rectangle對象,其中一個寬為4而高為40,另一個矩形的寬為3.5而
14、高為35.9.按照如下順序顯示每個矩形的寬,高 ,周長和面積。public class TestRectangle public static void main(String args) Rectangle rectangle = new Rectangle(4, 40); System.out.println(n寬度 +rectangle.width); System.out.println(n高度 +rectangle.height); System.out.println(n周長 + rectangle.getPerimeter(); System.out.println(n面積 +
15、rectangle.getArea(); Rectangle rectangle1 = new Rectangle(3.5, 35.9); System.out.println(n寬度 +rectangle1.width); System.out.println(n高度 +rectangle1.height); System.out.println(n周長 + rectangle1.getPerimeter(); System.out.println(n面積 + rectangle1.getArea(); class Rectangle double width; double height;
16、 public Rectangle() public Rectangle(double width, double height) this.width = width; this.height = height; public double getWidth() return width; public void setWidth(double width) this.width = width; public double getHeight() return height; public void setHeight(double height) this.height = height
17、; public double getArea() return width * height; public double getPerimeter() return 2 * (width + height); 5、模擬Integer編寫int類型的包裝類MyInteger,要求如下:(1)一個名為value的int型私有數(shù)據(jù)域,存儲這個對象表示的int值;(2)一個為指定的int值創(chuàng)建MyInteger對象的構(gòu)造方法;(3)一個返回/設置 value值的get和set方法。(4)如果值分別為偶數(shù)、奇數(shù),那么isEven()、isOdd()的方法都會返回true。解題要求:編寫測試類,創(chuàng)建對
18、象n1,設置其value屬性值為5,分別調(diào)用isEven()和isOdd()方法,輸出結(jié)果; 創(chuàng)建對象n2,設置其value屬性值為6,分別調(diào)用isEven()和isOdd()方法,輸出結(jié)果。public class TestMyInteger public static void main(String args) MyInteger n1 = new MyInteger(5); System.out.println(n1 is even? + n1.isEven(); System.out.println(n1 is odd? + n1.isOdd(); MyInteger n2 = new MyInteger(6); System.out.println(n2 is even? + n2.isEven(); System.out.println
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 江蘇省淮安市本年度(2025)小學一年級數(shù)學統(tǒng)編版階段練習(下學期)試卷及答案
- 2025-2030年中國散熱排風機市場運行新形勢與投資前景報告
- 大學生要如何網(wǎng)絡安全防范論文
- 英語中國文化閱讀教學設計
- 2025屆江蘇省徐州一中高三六校第一次聯(lián)考英語試卷含解析
- 湖南省長沙市岳麓區(qū)湖南師范大學附中2025屆高三(最后沖刺)英語試卷含解析
- 職業(yè)技能鑒定初級光纖通信模擬題及參考答案
- 【9道 一模】2025年4月邯鄲市邯山區(qū)七校聯(lián)考中考一模道法試卷含答案
- 北京市第五十七中學2024-2025學年高二下學期期中考試英語試題(原卷版+解析版)
- 稀有金屬礦選礦廠安全生產(chǎn)標準化實施指南考核試卷
- 教科版四年級下冊科學《植物的生長變化》單元解讀
- 年產(chǎn)吲哚美辛的生產(chǎn)設計設計說明書
- 學校安全事故應急處置流程圖
- 車位租賃合同證明書
- GB/T 3091-2015低壓流體輸送用焊接鋼管
- 廣東省國家公務員錄用體檢表
- GB/T 19582.2-2008基于Modbus協(xié)議的工業(yè)自動化網(wǎng)絡規(guī)范第2部分:Modbus協(xié)議在串行鏈路上的實現(xiàn)指南
- GB/T 12686-2017草甘膦原藥
- 細胞的能量“貨幣”ATP說課課件-高一上學期生物人教版必修1
- 企業(yè)合規(guī)管理培訓課件講義
- 解剖學課件神經(jīng)系統(tǒng)課件
評論
0/150
提交評論