资源预览内容
第1页 / 共34页
第2页 / 共34页
第3页 / 共34页
第4页 / 共34页
第5页 / 共34页
第6页 / 共34页
第7页 / 共34页
第8页 / 共34页
第9页 / 共34页
第10页 / 共34页
亲,该文档总共34页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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( +calen
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号