java语言程序设计基础篇第十版第十三章练习答案.docx_第1页
java语言程序设计基础篇第十版第十三章练习答案.docx_第2页
java语言程序设计基础篇第十版第十三章练习答案.docx_第3页
java语言程序设计基础篇第十版第十三章练习答案.docx_第4页
java语言程序设计基础篇第十版第十三章练习答案.docx_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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.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; 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 getPerimeter() 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 = 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(); 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; 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 = list.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 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(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); /print 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(year, 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+) System.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.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) GeometricObject1.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; 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 method 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 (getArea() 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(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 public 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; / Override 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; 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 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 properties */ 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 Square(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 GeometricObject 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.ArrayList;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.println(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); list.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; catch (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 extends 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 GeometricObject */ 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_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_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; /* 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 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 the 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.getArea() 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(Perimeter 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 = 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 abstract 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 class */ 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 Exercise13_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(); 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.height = 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 getArea 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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论