《java语言程序设计基础篇第十版第十三章练习答案.docx》由会员分享,可在线阅读,更多相关《java语言程序设计基础篇第十版第十三章练习答案.docx(34页珍藏版)》请在金锄头文库上搜索。
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+) 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( +calen