




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、精選優質文檔-傾情為你奉上精選優質文檔-傾情為你奉上專心-專注-專業專心-專注-專業精選優質文檔-傾情為你奉上專心-專注-專業期末復習試題(二)程序題1、編寫程序創建Point類,要求如下:(1)double類型的數據域x和y分別表示點的坐標;(2)x、y的get和set方法;(3)一個無參構造方法;(4)一個創建點對象同時指定x和y坐標的有參的構造方法;(5)一個名為distance(Point p)的方法,返回從該點到指定點之間的距離;(6)一個名為distance(double x, double y)的方法,返回從該點到指定x和y坐標的指定點之間的距離。解題要求:編寫測試類,分別調用兩
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計算通過參數傳遞進來的p點與當前點的距離-p.getX()*(x-p.getX() + (y-p.getY()*(y-p.g
4、etY();計算通過參數傳遞進來的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(),可以輸出課程名稱、選課學生名單和選課人數。(4)一個添加學生的方法addSt
5、udent(String student)。(5)一個查詢選課學生數量的方法getNumberOfStduents()解題要求:編寫測試類,創建課程對象,添加3個選課學生,按照如下提示輸出課程信息。import java.util.ArrayList;public class TestCourse public static void main(String arg)Course c = new Course(面向對象技術);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 + 選課人數 + getNumberOfStudents()
8、 + n + 學生名單 + s;3、下圖描述了兩個類:Line(線段)和Point(點),以及兩個類之間的關聯關系,一條線段對象由對應的兩個點對象組成。解題要求:要求如下:(1) 編寫Line(線段)和Point(點)兩個類的代碼,注意滿足封裝的需求。將數據隱藏,通過方法訪問數據。(2) 使用Line類的Line(x1: int, y1: int, x2: int, y2: int)方法,創建Line對象,端點是(10,20)、(30,40),計算并輸出線段的長度。(3)使用Line類的Line(p1: Point,p2: Point)方法,創建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);在調用point1的方法的時候,piont1一定在某個位置通過new創建出來,否則出現空指針錯誤。如果Point類有參數為空的構造方法,則把上面的代碼如下修改就可以了 :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、創建矩形類Rectangle,包括(1)兩個名為width和height的double型數據域,它們分別表示矩形的寬和高.width和height的默認值都為1.(2)創建默認矩形的無參構造方法。(3)一個創建width和height為指定值的矩形的構造方法。(4)一個名為getArea()的方法返回這個矩形的面積(5)一個名為getPerimeter()的方法返回周長。解題要求:編寫測試程序,創建兩個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型私有數據域,存儲這個對象表示的int值;(2)一個為指定的int值創建MyInteger對象的構造方法;(3)一個返回/設置 value值的get和set方法。(4)如果值分別為偶數、奇數,那么isEven()、isOdd()的方法都會返回true。解題要求:編寫測試類,創建對
18、象n1,設置其value屬性值為5,分別調用isEven()和isOdd()方法,輸出結果; 創建對象n2,設置其value屬性值為6,分別調用isEven()和isOdd()方法,輸出結果。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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 電子顯微鏡設備維修技術指導
- 運動康復技術與設備市場調研報告高新領域探索
- 2025至2030通風機部件行業發展趨勢分析與未來投資戰略咨詢研究報告
- 2025至2030天然橄欖油行業產業運行態勢及投資規劃深度研究報告
- 體育產業外部資源整合與市場拓展策略研究
- 智能家居行業創新發展及競爭格局研究報告
- 辦公室員工健康安全工作指南
- Excel函數運用進階教程及案例分析
- 綠色智能家居精準守護者:家庭健康監測終端產品與技術解讀
- 年底創意活動方案
- 2024年深圳市中考生物試卷真題(含答案解析)
- 綠化養護服務投標方案(技術標)
- 溝通與演講2023學習通超星課后章節答案期末考試題庫2023年
- 三甲醫院必備醫療設備清單大全
- 播音主持重音的教學課件
- 暴雨產流計算(推理公式_四川省)
- NUDD新獨難異失效模式預防檢查表
- 中考數學復習經驗交流PPT課件
- 內部控制專項審計實施方案
- DSP課設——正弦波發生器
- 從《國際博物館》看世界博物館發展解析
評論
0/150
提交評論