Enable Circle Comparable

1 minute read

The code has been modified slightly and now uploaded to the site.

Question

<— Return Home

Main.java

public class Main {
    public static void main(String[] args) {
        Circle circle1 = new Circle(10, "red", true);
        Circle circle2 = new Circle(25, "blue", false);
        System.out.println(circle1.equals(circle2));
    }
}

<— Return Home

Circle.java

public class Circle extends GeometricObject implements Comparable<Circle>{
    private double radius;

    public Circle(){

    }

    public Circle(double radius){
        this.radius = radius;
    }

    public Circle(double radius, String color, boolean filled){
        this.radius = radius;
        super.setColor(color);
        super.setFilled(filled);
    }

    /** Return radius */
    public double getRadius(){
        return radius;
    }

    /** Set a new radius */
    public void setRadius(double radius){
        this.radius = radius;
    }

    @Override/** Return area */
    public double getArea(){
        return radius * radius * Math.PI;
    }

    /** Return diameter */
    public double getDiameter(){
        return 2 * radius;
    }

    @Override/** Return perimeter */
    public double getPerimeter(){
        return 2 * radius * Math.PI;
    }

    /** Print the circle info */
    public void printCircle(){
        System.out.println("The circle is created " + super.getDateCreated() + " and the radius is " + radius);
    }

    @Override
    public int compareTo(Circle other){
        if (this.getRadius() > other.getRadius()){
            return 1;
        }
        else if (other.getRadius() > this.getRadius()){
            return -1;
        }
        else
            return 0;
    }

    public boolean equals(Circle other){
        // return this.getRadius().equals(((Circle)other).getRadius());
        return this.compareTo(other) == 0;
    }
}

<— Return Home

GeometricObject.java

public abstract class 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 getter method is named is Filled */
    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;
    }

    @Override
    public String toString(){
        return "created on " + dateCreated + "\ncolor: " + color + " and filled: " + filled;
    }

    /** Abstract method getArea */
    public abstract double getArea();

    /** Abstract method getPerimeter  */
    public abstract double getPerimeter();
}

<— Return Home