Question
1. Based on the code below, modify the GeometricObject class to implement the Comparable interface, and define a static max method in the GeometricObject class
1. Based on the code below, modify the GeometricObject class to implement the Comparable interface, and define a static max method in the GeometricObject class for finding the larger of two GeometricObject objects.
2. Write a test program that uses the max() method to find the larger of two circles and the larger of two rectangles. Use the appropriate methods to determine if two GeometricObjects are the same.
3. Add all your objects to an ArrayList, sort it, then print out the sorted ArrayList.
package project.ipi;
public class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
private String name = "noname";
/** Construct a default geometric object */
public GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with the specified color
* and filled value */
public GeometricObject(String color, boolean filled, String name) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
this.name = name;
}
/** 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,
its 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;
}
/** Return name */
public String getName() {
return name;
}
/** Set a new name */
public void setName(String name) {
this.name = name;
}
/** Return a string representation of this object */
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}
}
public class Circle extends GeometricObject { private double radius; public Circle() { radius = 1.0; } public void setRadius(double radius) { this.radius = radius; } public double getRadius() { return radius; } public double getArea() { return 3.14 * (radius * radius); } public double getPerimeter() { return 2 * 3.14 * radius; } public double getDiameter() { return radius * 2; } }
public class Rectangle extends GeometricObject{
private double width; private double height; public Rectangle() { width = 1.0; height = 1.0; } public void setWidth(double width) { this.width = width; } public void setHeight(double height) { this.height = height; } public double getWidth() { return width; } public double getHeight() { return height; } public double getPerimeter() { return 2 * (height * width); } public double getArea() { return width * height; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started