Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Circle class that extends the GeometricObject class and implement the C

ID: 1271338 • Letter: C

Question

Create a Circle class that extends the GeometricObject class and implement the Comparable interface The Circle class has a double type data field called radius (provide the get and set methods for the radius) Add 2 constructors: The no-argument constructor sets the radius to 5.0 Another constructor takes the value of the radius Since the Circle class extends from an abstract class, it has to implement the abstract methods defined in the GeometricObject class Override the toString method to display the class name and the radius of the circle

Explanation / Answer

abstract class GeometricObject implements Comparable<GeometricObject> {

public abstract double getArea();

@Override
public abstract int compareTo(GeometricObject c);

public static Comparable max(GeometricObject o1, GeometricObject o2) {
if (o1.compareTo(o2) > 1)
return o1;
else
return o2;
}   
}
class Circle extends GeometricObject {
private double radius;

   Circle(){
       radius = 5;
   }
   Circle(double r){
       radius = r;
   }
   double getRadius(){
       return radius;
   }
   public void setRadius(double radius) {
       this.radius = radius;
   }
@Override
   public String toString(){
       return ""+radius;
   }
  
   public boolean equals(Circle obj){
       if(obj.getRadius() == radius)
           return true;
       return false;
   }
public double getArea() {
return radius * radius * Math.PI;
}

@Override
public int compareTo(GeometricObject o) {
if (getArea() > o.getArea()) {
return 1;
} else if (getArea() < o.getArea()) {
return -1;
} else {
return 0;
}   
}
}
public class TestCircle {

public static void main(String[] args) {
Circle c1 = new Circle();
Circle c2 = new Circle();
       Circle c3 = new Circle(7.5);
System.out.println("Circle 1 is equal to Circle 2: "+c1.equals(c2));
       System.out.println("Comparing circle 1 to circle 2 gives: "+c1.compareTo(c2));
       System.out.println("Circle 1 is equal to Circle 3: "+c1.equals(c3));
       System.out.println("Comparing circle 1 to circle 3 gives: "+c1.compareTo(c3));
}
}