Modify the GeometricObject class to implement the Comparable interface, and defi
ID: 3656598 • Letter: M
Question
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. Write a test program that uses the max method to find the larger of two circles and the larger of two rectangles. GeometricObject: public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ public 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, so, the get method name is 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 */ 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(); } Example of a comparator: import java.util.Comparator; public class GeometricObjectComparator implements Comparator, java.io.Serializable { public int compare(GeometricObject o1, GeometricObject o2) { double area1 = o1.getArea(); double area2 = o2.getArea(); if (area1 < area2) return -1; else if (area1 == area2) return 0; else return 1; } }Explanation / Answer
import java.util.Date; public abstract class GeometricObject implements Comparable{ private String color = "white"; private boolean filled; private Date dateCreated; protected GeometricObject(){ dateCreated = new Date(); } public String getColor(){ return color; } public void setColor (String color){ this.color = color; } public boolean isFilled (){ return filled; } public void setFilled (boolean filled){ this.filled = filled; } public Date getDateCreated(){ return dateCreated; } public abstract double getArea(); public abstract double getPerimeter(); public int compareTo(Object o1){ if (getArea() == ((GeometricObject) o1).getArea()) return 0; else if (getArea()>((GeometricObject)o1).getArea()) return 1; else return -1; } public static void max (Object o,Object o1){ if (((GeometricObject)o).compareTo(o1)==1){ System.out.println("The First Object is larger than the Second Object"); } else if (((GeometricObject)o).compareTo(o1)==0){ System.out.println("Both Object is equal"); } else { System.out.println("The First Object is smaller than the Second"); } } } Circle.java public class Circle extends GeometricObject { private double radius; public Circle (double radius){ this.radius=radius; } public double getArea(){ return radius*radius*Math.PI; } public double getPerimeter(){ return radius*Math.PI*2; } public double getRadius (){ return radius; } public double getDiameter(){ return 2*radius; } public void setRadius(double radius){ this.radius=radius; } } Rectangle public class Rectangle extends GeometricObject { private double width,height; public double getArea(){ return width*height; } public double getPerimeter(){ return (width+height)*2; } public void setWidth(double width){ this.width=width; } public void setHeigth(double height){ this.height=height; } public double getWidth (){ return width; } public double getHeight (){ return height; } public Rectangle (double width,double height){ this.width=width; this.height=height; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.