01
public 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);
}
}
classTriangleNew extends GeometricObject {
private double side1 = 1.0, side2 = 1.0, side3 = 1.0;
/** Constructor */
publicTriangleNew() {
}
/** Constructor */
publicTriangleNew(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;
returnMath.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;
}
}
02
importjava.util.ArrayList;
public class Exercise13_02 {
public static void main(String[] args) {
ArrayList
list.add(14);
list.add(24);
list.add(4);
list.add(42);
list.add(5);
shuffle(list);
for (inti = 0; i System.out.print(list.get(i) + " "); } public static void shuffle(ArrayList for (inti = 0; i int index = (int)(Math.random() * list.size()); Number temp = list.get(i); list.set(i, list.get(index)); list.set(index, temp); } } } 03 importjava.util.ArrayList; public class Exercise13_03 { public static void main(String[] args) { ArrayList list.add(14); list.add(24); list.add(4); list.add(42); list.add(5); sort(list); for (inti = 0; i System.out.print(list.get(i) + " "); } public static void sort(ArrayList for (inti = 0; i // Find the minimum in the list[i..list.length-1] Number currentMin = list.get(i); intcurrentMinIndex = i; for (int j = i + 1; j if (currentMin.doubleValue() >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); } } } } 04 importjava.util.*; public class Exercise13_04 { staticMyCalendar 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(args[1]); month = Integer.parseInt(args[0]); 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(args[0]); 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 intstartDay = getStartDay(); //get number of days in the month intnumOfDaysInMonth = calendar.daysInMonth(); //print headings printMonthTitle(year, month); //print body printMonthBody(startDay, numOfDaysInMonth); } staticintgetStartDay() { returncalendar.get(Calendar.DAY_OF_WEEK); } static void printMonthBody(intstartDay, intnumOfDaysInMonth) { //print padding space before the first day of the month inti = 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"); } } 05 public 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 circle's radius is " + circle.getRadius()); System.out.println(circle); } } abstract class GeometricObject1 implements Comparable 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(); publicintcompareTo(GeometricObject1 o) { if (getArea() return -1; else if (getArea() == o.getArea()) return 0; else return 1; } public static GeometricObject1 max(GeometricObject1 o1, GeometricObject1 o2) { if (https://www.doczj.com/doc/d618531446.html,pareTo(o2) > 0) return o1; else return o2; } } // Circle.java: The circle class that extends GeometricObject class 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 publicboolean equals(Circle1 circle) { returnthis.radius == circle.getRadius(); } @Override public String toString() { return "[Circle] radius = " + radius; } @Override publicintcompareTo(GeometricObject1 o) { if (getRadius() > ((Circle1) o).getRadius()) return 1; else if (getRadius() < ((Circle1) o).getRadius()) return -1; else return 0; } } 06 public 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 circle's radius is " + circle3.getRadius()); System.out.println(circle3); } } classComparableCircle extends Circle implements Comparable super(radius); } @Override publicintcompareTo(ComparableCircle o) { if (getRadius() >o.getRadius()) return 1; else if (getRadius() return -1; else return 0; } } //Max.java: Find a maximum object class Max { /** Return the maximum of two objects */ public static ComparableCircle max (ComparableCircle o1, ComparableCircle o2) { if (https://www.doczj.com/doc/d618531446.html,pareTo(o2) > 0) return o1; else return o2; } } 07 public 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 (inti = 0; i System.out.println("Area is " + objects[i].getArea()); if (objects[i] instanceof Colorable) ((Colorable)objects[i]).howToColor(); } } } interface Colorable { voidhowToColor(); } 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; } } 08 importjava.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 { privateArrayList publicbooleanisEmpty() { returnlist.isEmpty(); } publicintgetSize() { returnlist.size(); } public Object peek() { returnlist.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 } catch (CloneNotSupportedException ex) { return null; } } } 09 public 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(https://www.doczj.com/doc/d618531446.html,pareTo(obj2)); } } // Circle.java: The circle class that extends GeometricObject class Circle13_09 extends GeometricObject implements Comparable /** 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 publicintcompareTo(Circle13_09 obj) { if (this.getArea() >obj.getArea()) return 1; else if (this.getArea() return -1; else return 0; } publicboolean equals(Object obj) { returnthis.radius == ((Circle13_09)obj).radius; } } public 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(https://www.doczj.com/doc/d618531446.html,pareTo(obj2)); } } // Rectangle.java: The Rectangle class that extends GeometricObject class Rectangle13_10 extends GeometricObject implements Comparable 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 publicintcompareTo(Rectangle13_10 obj) { if (this.getArea() >obj.getArea()) return 1; else if (this.getArea() return -1; else return 0; } publicboolean equals(Object obj) { returnthis.getArea() == ((Rectangle13_10)obj).getArea(); } } 11 public 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 " + https://www.doczj.com/doc/d618531446.html,pareTo(a2)); } } class Octagon extends GeometricObject implements Comparable 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 publicintcompareTo(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 { returnsuper.clone(); // Automatically perform a shallow copy } catch (CloneNotSupportedException ex) { return null; } } } 12 public 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 (inti = 0; i sum += a[i].getArea(); return sum; } } // Rectangle.java: The Rectangle class that extends GeometricObject class 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*/ publicboolean equals(Rectangle rectangle) { return (width == rectangle.getWidth()) && (height == rectangle.getHeight()); } @Override public String toString() { return "[Rectangle] width = " + width + " and height = " + height; } } 13 public class Exercise13_13 { /** Main method */ public static void main(String[] args) { Course1 course1 = new Course1("DS"); course1.addStudent("S1"); 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 Cloneable { private String courseName; private String[] students = new String[100]; privateintnumberOfStudents; public Course1(String courseName) { this.courseName = courseName; } public void addStudent(String student) { students[numberOfStudents] = student; numberOfStudents++; } public String[] getStudents() { return students; } publicintgetNumberOfStudents() { returnnumberOfStudents; } public String getCourse1Name() { returncourseName; } 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 String[100]; System.arraycopy(students, 0, c.students, 0, 100); c.numberOfStudents = numberOfStudents; return c; } catch (CloneNotSupportedException ex) { return null; } } } 14 classNewRational extends Number implements Comparable private long[] r = new long[2]; /**Default constructor*/ publicNewRational() { this(0, 1); } /**Construct a rational with specified numerator and denominator*/ publicNewRational(long numerator, long denominator) { longgcd = gcd(numerator, denominator); this.r[0] = numerator/gcd; this.r[1] = denominator/gcd; } /**Find GCD of two numbers*/ private long gcd(long n, long d) { long t1 = Math.abs(n); long t2 = Math.abs(d); long remainder = t1%t2; while (remainder != 0) { t1 = t2; t2 = remainder; remainder = t1%t2; } return t2; } /**Return numerator*/ public long getNumerator() { return r[0]; } /**Return denominator*/ public long getDenominator() { return r[1]; } /**Add a rational number to this rational*/ publicNewRational add(NewRationalsecondNewRational) { long n = r[0]*secondNewRational.getDenominator() + r[1]*secondNewRational.getNumerator(); long d = r[1]*secondNewRational.getDenominator(); return new NewRational(n, d); } /**Subtract a rational number from this rational*/ publicNewRational subtract(NewRationalsecondNewRational) { long n = r[0]*secondNewRational.getDenominator() - r[1]*secondNewRational.getNumerator(); long d = r[1]*secondNewRational.getDenominator(); return new NewRational(n, d); } /**Multiply a rational number to this rational*/ publicNewRational multiply(NewRationalsecondNewRational) { long n = r[0]*secondNewRational.getNumerator(); long d = r[1]*secondNewRational.getDenominator(); return new NewRational(n, d); } /**Divide a rational number from this rational*/ publicNewRational divide(NewRationalsecondNewRational) {