Problem: Compare areas of different geometric shapes (filename: CompareShapeArea
ID: 3740144 • Letter: P
Question
Problem: Compare areas of different geometric shapes
(filename: CompareShapeAreas.java)
Define a class named ComparableCircle that extends the Circle class and implements the Comparable interface as shown below. The Circle class itself extends the GeometricObject class.
class ComparableCircle extends Circle implements Comparable<GeometricObject>
Implement the CompareTo method defined inside the Comparable interface to compare the areas between a Circle object and a GeometricObject object. It returns 1 if the circle has a larger area, else if the circle has a smaller area, it returns -1, else it returns 0.
For implementing the Geometric class and the Circle class, you may use the code from the book or the slides
Similarly, based on the code from the book or the slides, define a class named ComparableRectangle that extends the Rectangle class and implements the Comparable interface. The Rectangle class itself extends the GeometricObject class. Implement the CompareTo method defined inside the Comparable interface to compare the areas between a Rectangle object and a GeometricObject object. It returns 1 if the rectangle has a larger area, else if the rectangle has a smaller area, it returns -1, else it returns 0.
Write a test class to find the larger of two instances of ComparableCircle objects by invoking the CompareTo method, and the larger between an instance of a ComparableCircle and an instance of ComparableRectangle also by invoking the CompareTo method.
Explanation / Answer
public abstract class GeometricObject implements Comparable<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;
}
/** Return a string representation of this object */
@Override
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}
@Override
public int compareTo(GeometricObject o) {
if (getArea() > o.getArea())
return 1;
else if (getArea() < o.getArea())
return -1;
else
return 0;
}
public static GeometricObject max(GeometricObject o1, GeometricObject o2) {
return (o1.compareTo(o2) >= 0) ? o1 : o2;
}
public static double sumArea(GeometricObject[] a) {
double sum = 0;
for (GeometricObject o : a) {
sum += o.getArea();
}
return sum;
}
/** Abstract method getArea */
public abstract double getArea();
/** Abstract method getPerimeter */
public abstract double getPerimeter();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.