java語言程序設(shè)計(jì)基礎(chǔ)篇第十版第十三章練習(xí)答案.docx

上傳人:good****022 文檔編號(hào):116792420 上傳時(shí)間:2022-07-06 格式:DOCX 頁數(shù):34 大?。?5.54KB
收藏 版權(quán)申訴 舉報(bào) 下載
java語言程序設(shè)計(jì)基礎(chǔ)篇第十版第十三章練習(xí)答案.docx_第1頁
第1頁 / 共34頁
java語言程序設(shè)計(jì)基礎(chǔ)篇第十版第十三章練習(xí)答案.docx_第2頁
第2頁 / 共34頁
java語言程序設(shè)計(jì)基礎(chǔ)篇第十版第十三章練習(xí)答案.docx_第3頁
第3頁 / 共34頁

下載文檔到電腦,查找使用更方便

10 積分

下載資源

還剩頁未讀,繼續(xù)閱讀

資源描述:

《java語言程序設(shè)計(jì)基礎(chǔ)篇第十版第十三章練習(xí)答案.docx》由會(huì)員分享,可在線閱讀,更多相關(guān)《java語言程序設(shè)計(jì)基礎(chǔ)篇第十版第十三章練習(xí)答案.docx(34頁珍藏版)》請(qǐng)?jiān)谘b配圖網(wǎng)上搜索。

1、01public class Exercise13_01 public static void main(String args) TriangleNew triangle = new TriangleNew(1, 1.5, 1); triangle.setColor(yellow); triangle.setFilled(true); System.out.println(triangle); System.out.println(The area is + triangle.getArea(); System.out.println(The perimeter is + triangle.

2、getPerimeter(); System.out.println(triangle); class TriangleNew extends GeometricObject private double side1 = 1.0, side2 = 1.0, side3 = 1.0; /* Constructor */ public TriangleNew() /* Constructor */ public TriangleNew(double side1, double side2, double side3) this.side1 = side1; this.side2 = side2;

3、this.side3 = side3; /* Implement the abstract method findArea in GeometricObject */ public double getArea() double s = (side1 + side2 + side3) / 2; return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3); /* Implement the abstract method findCircumference in * GeometricObject */ public double g

4、etPerimeter() return side1 + side2 + side3; Override public String toString() / Implement it to return the three sides return TriangleNew: side1 = + side1 + side2 = + side2 + side3 = + side3; 02import java.util.ArrayList;public class Exercise13_02 public static void main(String args) ArrayList list

5、= new ArrayList(); list.add(14); list.add(24); list.add(4); list.add(42); list.add(5); shuffle(list); for (int i = 0; i list.size(); i+) System.out.print(list.get(i) + ); public static void shuffle(ArrayList list) for (int i = 0; i list.size() - 1; i+) int index = (int)(Math.random() * list.size();

6、Number temp = list.get(i); list.set(i, list.get(index); list.set(index, temp); 03import java.util.ArrayList;public class Exercise13_03 public static void main(String args) ArrayList list = new ArrayList(); list.add(14); list.add(24); list.add(4); list.add(42); list.add(5); sort(list); for (int i = 0

7、; i list.size(); i+) System.out.print(list.get(i) + ); public static void sort(ArrayList list) for (int i = 0; i list.size() - 1; i+) / Find the minimum in the listi.list.length-1 Number currentMin = list.get(i); int currentMinIndex = i; for (int j = i + 1; j list.get(j).doubleValue() currentMin = l

8、ist.get(j); currentMinIndex = j; / Swap list.get(i) with list.get(currentMinIndex) if necessary; if (currentMinIndex != i) list.set(currentMinIndex, list.get(i); list.set(i, currentMin); 04import java.util.*;public class Exercise13_04 static MyCalendar calendar = new MyCalendar(); public static void

9、 main(String args) int month = calendar.get(MyCalendar.MONTH) + 1; int year = calendar.get(MyCalendar.YEAR); if (args.length 2) System.out.println(Usage java Exercise13_04 month year); else if (args.length = 2) /use user-defined month and year year = Integer.parseInt(args1); month = Integer.parseInt

10、(args0); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month - 1); else if (args.length = 1) /use user-defined month for the current year month = Integer.parseInt(args0); calendar.set(Calendar.MONTH, month-1); /set date to the first day in a month calendar.set(Calendar.DATE, 1); /p

11、rint calendar for the month printMonth(year, month); static void printMonth(int year, int month) /get start day of the week for the first date in the month int startDay = getStartDay(); /get number of days in the month int numOfDaysInMonth = calendar.daysInMonth(); /print headings printMonthTitle(ye

12、ar, month); /print body printMonthBody(startDay, numOfDaysInMonth); static int getStartDay() return calendar.get(Calendar.DAY_OF_WEEK); static void printMonthBody(int startDay, int numOfDaysInMonth) /print padding space before the first day of the month int i = 0; for (i = 0; i startDay-1; i+) Syste

13、m.out.print( ); for (i = 1; i = numOfDaysInMonth; i+) if (i 10) System.out.print( +i); else System.out.print( +i); if (i + startDay - 1) % 7 = 0) System.out.println(); System.out.println(); static void printMonthTitle(int year, int month) System.out.println( +calendar.getMonthName()+, +year); System

14、.out.println(-); System.out.println( Sun Mon Tue Wed Thu Fri Sat); 05public class Exercise13_05 / Main method public static void main(String args) / Create two comparable circles Circle1 circle1 = new Circle1(5); Circle1 circle2 = new Circle1(4); / Display the max circle Circle1 circle = (Circle1) G

15、eometricObject1.max(circle1, circle2); System.out.println(The max circles radius is + circle.getRadius(); System.out.println(circle); abstract class GeometricObject1 implements Comparable protected String color; protected double weight; / Default construct protected GeometricObject1() color = white;

16、 weight = 1.0; / Construct a geometric object protected GeometricObject1(String color, double weight) this.color = color; this.weight = weight; / Getter method for color public String getColor() return color; / Setter method for color public void setColor(String color) this.color = color; / Getter m

17、ethod for weight public double getWeight() return weight; / Setter method for weight public void setWeight(double weight) this.weight = weight; / Abstract method public abstract double getArea(); / Abstract method public abstract double getPerimeter(); public int compareTo(GeometricObject1 o) if (ge

18、tArea() 0) return o1; else return o2; / Circle.java: The circle class that extends GeometricObjectclass Circle1 extends GeometricObject1 protected double radius; / Default constructor public Circle1() this(1.0, white, 1.0); / Construct circle with specified radius public Circle1(double radius) super

19、(white, 1.0); this.radius = radius; / Construct a circle with specified radius, weight, and color public Circle1(double radius, String color, double weight) super(color, weight); this.radius = radius; / Getter method for radius public double getRadius() return radius; / Setter method for radius publ

20、ic void setRadius(double radius) this.radius = radius; / Implement the findArea method defined in GeometricObject public double getArea() return radius * radius * Math.PI; / Implement the findPerimeter method defined in GeometricObject public double getPerimeter() return 2 * radius * Math.PI; / Over

21、ride the equals() method defined in the Object class public boolean equals(Circle1 circle) return this.radius = circle.getRadius(); Override public String toString() return Circle radius = + radius; Override public int compareTo(GeometricObject1 o) if (getRadius() (Circle1) o).getRadius() return 1;

22、else if (getRadius() (Circle1) o).getRadius() return -1; else return 0; 06public class Exercise13_06 / Main method public static void main(String args) / Create two comarable rectangles ComparableCircle circle1 = new ComparableCircle(5); ComparableCircle circle2 = new ComparableCircle(15); / Display

23、 the max rect ComparableCircle circle3 = (ComparableCircle)Max.max(circle1, circle2); System.out.println(The max circles radius is + circle3.getRadius(); System.out.println(circle3); class ComparableCircle extends Circle implements Comparable /* Construct a ComparableRectangle with specified propert

24、ies */ public ComparableCircle(double radius) super(radius); Override public int compareTo(ComparableCircle o) if (getRadius() o.getRadius() return 1; else if (getRadius() 0) return o1; else return o2; 07public class Exercise13_07 public static void main(String args) GeometricObject objects = new Sq

25、uare(2), new Circle(5), new Square(5), new Rectangle(3, 4), new Square(4.5); for (int i = 0; i objects.length; i+) System.out.println(Area is + objectsi.getArea(); if (objectsi instanceof Colorable) (Colorable)objectsi).howToColor(); interface Colorable void howToColor();class Square extends Geometr

26、icObject implements Colorable private double side; public Square(double side) this.side = side; Override public void howToColor() System.out.println(Color all four sides); Override public double getArea() return side * side; Override public double getPerimeter() return 4 * side; 08import java.util.A

27、rrayList;public class Exercise13_08 public static void main(String args) MyStack1 stack = new MyStack1(); stack.push(S1); stack.push(S2); stack.push(S); MyStack1 stack2 = (MyStack1) (stack.clone(); stack2.push(S1); stack2.push(S2); stack2.push(S); System.out.println(stack.getSize(); System.out.print

28、ln(stack2.getSize(); class MyStack1 implements Cloneable private ArrayList list = new ArrayList(); public boolean isEmpty() return list.isEmpty(); public int getSize() return list.size(); public Object peek() return list.get(getSize() - 1); public Object pop() Object o = list.get(getSize() - 1); lis

29、t.remove(getSize() - 1); return o; public void push(Object o) list.add(o); /* Override the toString in the Object class */ public String toString() return stack: + list.toString(); public Object clone() try MyStack1 c = (MyStack1) super.clone(); c.list = (ArrayList) this.list.clone(); return c; catc

30、h (CloneNotSupportedException ex) return null; 09public class Exercise13_09 public static void main(String args) Circle13_09 obj1 = new Circle13_09(); Circle13_09 obj2 = new Circle13_09(); System.out.println(obj1.equals(obj2); System.out.println(pareTo(obj2); / Circle.java: The circle class that ext

31、ends GeometricObjectclass Circle13_09 extends GeometricObject implements Comparable private double radius; /* Return radius */ public double getRadius() return radius; /* Set a new radius */ public void setRadius(double radius) this.radius = radius; /* Implement the getArea method defined in Geometr

32、icObject */ public double getArea() return radius * radius * Math.PI; /* Implement the getPerimeter method defined in GeometricObject*/ public double getPerimeter() return 2 * radius * Math.PI; Override public String toString() return Circle radius = + radius; Override public int compareTo(Circle13_

33、09 obj) if (this.getArea() obj.getArea() return 1; else if (this.getArea() obj.getArea() return -1; else return 0; public boolean equals(Object obj) return this.radius = (Circle13_09)obj).radius; 10public class Exercise13_10 public static void main(String args) Rectangle13_10 obj1 = new Rectangle13_

34、10(); Rectangle13_10 obj2 = new Rectangle13_10(); System.out.println(obj1.equals(obj2); System.out.println(pareTo(obj2); / Rectangle.java: The Rectangle class that extends GeometricObjectclass Rectangle13_10 extends GeometricObject implements Comparable private double width; private double height; /

35、* Default constructor */ public Rectangle13_10() this(1.0, 1.0); /* Construct a rectangle with width and height */ public Rectangle13_10(double width, double height) this.width = width; this.height = height; /* Return width */ public double getWidth() return width; /* Set a new width */ public void

36、setWidth(double width) this.width = width; /* Return height */ public double getHeight() return height; /* Set a new height */ public void setHeight(double height) this.height = height; /* Implement the getArea method in GeometricObject */ public double getArea() return width*height; /* Implement th

37、e getPerimeter method in GeometricObject */ public double getPerimeter() return 2*(width + height); Override public String toString() return Rectangle width = + width + and height = + height; Override public int compareTo(Rectangle13_10 obj) if (this.getArea() obj.getArea() return 1; else if (this.g

38、etArea() obj.getArea() return -1; else return 0; public boolean equals(Object obj) return this.getArea() = (Rectangle13_10)obj).getArea(); 11public class Exercise13_11 public static void main(String args) Octagon a1 = new Octagon(5); System.out.println(Area is + a1.getArea(); System.out.println(Peri

39、meter is + a1.getPerimeter(); Octagon a2 = (Octagon)(a1.clone(); System.out.println(Compare the methods + pareTo(a2); class Octagon extends GeometricObject implements Comparable, Cloneable private double side; /* Construct a Octagon with the default side */ public Octagon () / Implement it this.side

40、 = 1; /* Construct a Octagon with the specified side */ public Octagon (double side) / Implement it this.side = side; Override /* Implement the abstract method getArea in GeometricObject */ public double getArea() / Implement it return (2 + 4 / Math.sqrt(2) * side * side; Override /* Implement the a

41、bstract method getPerimeter in GeometricObject */ public double getPerimeter() / Implement it return 8 * side; Override public int compareTo(Octagon obj) if (this.side obj.side) return 1; else if (this.side = obj.side) return 0; else return -1; Override /* Implement the clone method in the Object cl

42、ass */ public Object clone() / Octagon o = new Octagon();/ o.side = this.side;/ return o;/ / Implement it try return super.clone(); / Automatically perform a shallow copy catch (CloneNotSupportedException ex) return null; 12public class Exercise13_12 public static void main(String args) new Exercise

43、13_12(); public Exercise13_12() GeometricObject a = new Circle(5), new Circle(6), new Rectangle13_12(2, 3), new Rectangle13_12(2, 3); System.out.println(The total area is + sumArea(a); public static double sumArea(GeometricObject a) double sum = 0; for (int i = 0; i a.length; i+) sum += ai.getArea()

44、; return sum; / Rectangle.java: The Rectangle class that extends GeometricObjectclass Rectangle13_12 extends GeometricObject private double width; private double height; /* Construct a rectangle with width and height */ public Rectangle13_12(double width, double height) this.width = width; this.heig

45、ht = height; /*Return width*/ public double getWidth() return width; /*Set a new width*/ public void setWidth(double width) this.width = width; /*Return height*/ public double getHeight() return height; /*Set a new height*/ public void setHeight(double height) this.height = height; /*Implement the g

46、etArea method in GeometricObject*/ public double getArea() return width*height; /*Implement the getPerimeter method in GeometricObject*/ public double getPerimeter() return 2*(width + height); /*Override the equals method defined in the Object class*/ public boolean equals(Rectangle rectangle) retur

47、n (width = rectangle.getWidth() & (height = rectangle.getHeight(); Override public String toString() return Rectangle width = + width + and height = + height; 13public class Exercise13_13 /* Main method */ public static void main(String args) Course1 course1 = new Course1(DS); course1.addStudent(S1)

48、; course1.addStudent(S2); course1.addStudent(S3); Course1 course2 = (Course1) course1.clone(); course2.addStudent(S4); course2.addStudent(S5); course2.addStudent(S6); System.out.println(course1.getNumberOfStudents(); System.out.println(course2.getNumberOfStudents(); class Course1 implements Cloneabl

49、e private String courseName; private String students = new String100; private int numberOfStudents; public Course1(String courseName) this.courseName = courseName; public void addStudent(String student) studentsnumberOfStudents = student; numberOfStudents+; public String getStudents() return student

50、s; public int getNumberOfStudents() return numberOfStudents; public String getCourse1Name() return courseName; public void dropStudent(String student) / Left as an exercise in Exercise 10.9 public Object clone() try Course1 c = (Course1) super.clone(); c.students = new String100; System.arraycopy(st

51、udents, 0, c.students, 0, 100); c.numberOfStudents = numberOfStudents; return c; catch (CloneNotSupportedException ex) return null; 14class NewRational extends Number implements Comparable / Data fields for numerator and denominator private long r = new long2; /*Default constructor*/ public NewRational() this(0, 1);

展開閱讀全文
溫馨提示:
1: 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
2: 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
3.本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
5. 裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

相關(guān)資源

更多
正為您匹配相似的精品文檔
關(guān)于我們 - 網(wǎng)站聲明 - 網(wǎng)站地圖 - 資源地圖 - 友情鏈接 - 網(wǎng)站客服 - 聯(lián)系我們

copyright@ 2023-2025  zhuangpeitu.com 裝配圖網(wǎng)版權(quán)所有   聯(lián)系電話:18123376007

備案號(hào):ICP2024067431號(hào)-1 川公網(wǎng)安備51140202000466號(hào)


本站為文檔C2C交易模式,即用戶上傳的文檔直接被用戶下載,本站只是中間服務(wù)平臺(tái),本站所有文檔下載所得的收益歸上傳人(含作者)所有。裝配圖網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)上載內(nèi)容本身不做任何修改或編輯。若文檔所含內(nèi)容侵犯了您的版權(quán)或隱私,請(qǐng)立即通知裝配圖網(wǎng),我們立即給予刪除!