1、Java语言程序设计基础篇第10版 梁勇 著第十三章练习题答案Java语言程序设计(基础篇)(第10版 梁勇 著)第十三章 练习题答案13.1public 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
2、+ 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,
3、 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 abst
4、ract 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; 13.1附加import java.util.Scanner;public c
5、lass Exercise13_01Extra public static void main(String args) Scanner input = new Scanner(System.in); System.out.print(Enter time1 (hour minute second): ); int hour, minute, second; hour = input.nextInt(); minute = input.nextInt(); second = input.nextInt(); Time time1 = new Time(hour, minute, second)
6、; System.out.println(time1); System.out.println(Elapsed seconds in time1: + time1.getSeconds(); System.out.print(nEnter time2 (elapsed time): ); long seconds = input.nextLong(); Time time2 = new Time(seconds); System.out.println(time2); System.out.println(Elapsed seconds in time2: + time2.getSeconds
7、(); System.out.println(pareTo(time2)? + pareTo(time2); Time time3 = (Time)time1.clone(); System.out.println(pareTo(time3)? + pareTo(time3); class Time implements Comparable, Cloneable private long seconds; public Time() seconds = System.currentTimeMillis() / 1000; public Time(int hour, int minute, i
8、nt second) seconds = hour * 3600 + minute * 60 + second; public Time(long seconds) this.seconds = seconds; public long getSeconds() return seconds; public int getHour() return (int)(seconds / 3600 % 24); public int getMinute() return (int)(seconds / 60 % 60); public int getSecond() return (int)(seco
9、nds % 60); public String toString() return getHour() + (getHour() 1 ? hours : hour ) + getMinute() + (getMinute() 1 ? minutes : minute ) + getSecond() + (getSecond() 1 ? seconds : second ); public int compareTo(Time t) return (int)(this.seconds - t.seconds); public Object clone() try return super.cl
10、one(); catch (CloneNotSupportedException ex) ex.printStackTrace(); return null; 13.2import 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
11、 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); 13.3 import java.util.ArrayList
12、;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 =
13、 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 (curren
14、tMinIndex != i) list.set(currentMinIndex, list.get(i); list.set(i, currentMin); 13.4 import 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.YE
15、AR); 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.lengt
16、h = 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 st
17、art 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 cale
18、ndar.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.p
19、rint( +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); 13.5public class Exercise13_05 / Main
20、 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
21、(); 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)
22、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 setWe
23、ight(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 GeometricObjectclas
24、s 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 publ
25、ic 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 Geom
26、etricObject 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
27、.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; 13.6public class Exercise13_06
28、 / 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.print
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1