In Java: (Enable GeometricObject comparable) Modify the GeometricObject class to
ID: 663736 • Letter: I
Question
In Java: (Enable GeometricObject comparable) 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.java: The abstract GeometricObject class
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
/**Default construct*/
protected GeometricObject() {
}
/**Construct a geometric object*/
protected GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
/**Getter method for color*/
public String getColor() {
return color;
}
/**Setter method for color*/
public void setColor(String color) {
this.color = color;
}
/**Getter method for filled. Since filled is boolean,
so, the get method name is isFilled*/
public boolean isFilled() {
return filled;
}
/**Setter method for filled*/
public void setFilled(boolean filled) {
this.filled = filled;
}
/**Abstract method findArea*/
public abstract double getArea();
/**Abstract method getPerimeter*/
public abstract double getPerimeter();
}
Explanation / Answer
So you given already code....some methods needed....comments added
public abstract class GeometricObject {
private String color = "white";
........
//my methods starts from here
public int compareTo(Object obj) {
return getArea() - ((GeometricObject)obj).getArea();//by using area returning result
}
//compare method
public static GeometricObject maximum (GeometricObject obj1, GeometricObject obj2 ) {//static method which comapres by area calculated
if(obj1.compareTo(obj2)) > 0)
return obj1;
else
return obj2;
}
--------------------------------------------------------------
public class TestProgram{
public static void main(String args[]){
GeometricObject circle = new GeometricObject("green",true);
GeometricObject rectangle = new GeometricObject("yellow",true);
//you given only abstaract class..i hope might be another class to set dimensions
//....now compare using static methods we written
int result = maximum(circle,rectangle);//calling commpare method
if(result==1){
System.out.println("Circle is bigger object");
}
else if(result==1){
System.out.println("Rectanglr is bigger object");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.