Question: public class Main {public static void main(String[] args) { System.out.println(Comparing Areas of rectangles); ComparableRectangle[] rectangles = { new ComparableRectangle(3.4, 5.4), new ComparableRectangle(13.24, 55.4), }; java.util.Arrays.sort(rectangles);

public class Main {public static void main(String[] args) { System.out.println("Comparing Areas of rectangles"); ComparableRectangle[] rectangles = { new ComparableRectangle(3.4, 5.4), new ComparableRectangle(13.24, 55.4), }; java.util.Arrays.sort(rectangles); for (Rectangle rectangle: rectangles) { System.out.print(rectangle + " "); System.out.println(); } System.out.println("Comparing Areas of Circles"); ComparableCircle[] circles = { new ComparableCircle(3.4), new ComparableCircle(13), }; java.util.Arrays.sort(circles); for (Circle circle: circles) { System.out.print(circle + " "); System.out.println(); }

} } abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated;

/** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); }

/** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; }

/** Return color */ public String getColor() { return color; }

/** Set a new color */ public void setColor(String color) { this.color = color; }

/** Return filled. Since filled is boolean, * the get method is named isFilled */ public boolean isFilled() { return filled; }

/** Set a new filled */ public void setFilled(boolean filled) { this.filled = filled; }

/** Get dateCreated */ public java.util.Date getDateCreated() { return dateCreated; }

@Override public String toString() { return "created on " + dateCreated + " color: " + color + " and filled: " + filled; }

/** Abstract method getArea */ public abstract double getArea();

/** Abstract method getPerimeter */ public abstract double getPerimeter(); }

class Circle extends GeometricObject { private double radius;

public Circle() { }

public Circle(double radius) { this.radius = radius; }

/** Return radius */ public double getRadius() { return radius; }

/** Set a new radius */ public void setRadius(double radius) { this.radius = radius; }

@Override /** Return area */ public double getArea() { return radius * radius * Math.PI; }

/** Return diameter */ public double getDiameter() { return 2 * radius; }

@Override /** Return perimeter */ public double getPerimeter() { return 2 * radius * Math.PI; }

/* Print the circle info */ public void printCircle() { System.out.println("The circle is created " + getDateCreated() + " and the radius is " + radius); } }

class Rectangle extends GeometricObject { private double width; private double height;

public Rectangle() { }

public Rectangle(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; }

@Override /** Return area */ public double getArea() { return width * height; }

@Override /** Return perimeter */ public double getPerimeter() { return 2 * (width + height); } }

class ComparableRectangle extends Rectangle implements Comparable { /** Construct a ComparableRectangle with specified properties */ public ComparableRectangle(double width, double height) { super(width, height); }

@Override // Implement the compareTo method defined in Comparable public int compareTo(ComparableRectangle o) { if (getArea() > o.getArea()) return 1; else if (getArea() < o.getArea()) return -1; else return 0; } @Override // Implement the toString method in GeometricObject public String toString() { return "Width: " + getWidth() + " Height: " + getHeight() + " Area: " + getArea(); } } class ComparableCircle extends Circle implements Comparable< ComparableCircle> { /** Construct a ComparableRectangle with specified properties */ public ComparableCircle(double radius) { super(radius); }

@Override // Implement the compareTo method defined in Comparable public int compareTo(ComparableCircle a) { if (getArea() > a.getArea()) return 1; else if (getArea() < a.getArea()) return -1; else return 0; } @Override // Implement the toString method in GeometricObject public String toString() { return "radius: "+getRadius()+" Area: " + getArea();

} }

Write a test class to find the larger between an instance of a ComparableCircle and an instance of ComparableRectangle also by invoking the CompareTo method.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!