The ComparableCircle Class

2 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) {
        ComparableCircle circle1 = new ComparableCircle(18);
        ComparableCircle circle2 = new ComparableCircle(25);
        System.out.println(circle1);
        System.out.println(circle2);

        if (circle1.compareTo(circle2) == 1){
            System.out.println("Circle1 is bigger than Circle2!");
        }
        else
            System.out.println("Circle2 is bigger than Circle1!");
    }
}

<— Return Home

ComparableCircle.java

public class ComparableCircle extends Circle implements Comparable<GeometricObject>{

    public ComparableCircle(){

    }

    public ComparableCircle(double radius){
        super(radius);
    }

    public double getArea(){
        return super.getArea();
    }

    public int compareTo(ComparableCircle other){
        if (this.getArea() > other.getArea()){
            return 1;
        }
        else if (other.getArea() > this.getArea()){
            return -1;
        }
        else
            return 0;
    }

    // public static ComparableCircle max(ComparableCircle first, ComparableCircle second){
    //     if (first.compareTo(second) == 1){
    //         return first;
    //     }
    //     else 
    //         return second;
    // }
}

<— Return Home

Circle.java

public class Circle extends GeometricObject{
    private double radius;
    public Circle(){

    }

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

    public double getArea(){
        return 2 * Math.PI * (Math.pow(radius, 2));
    }

    public double getPerimeter(){
        return 2 * Math.PI * radius;
    }

    @Override
    public String toString(){
        return "Area of Circle: " + this.getArea();
    }
}

<— Return Home

GeometricObject.java

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 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;
    }

    public int compareTo(GeometricObject other){
        if (this.getArea() > other.getArea()){
            return 1;
        }
        else if (this.getArea() < other.getArea()){
            return -1;
        }
        else
            return 0;
    }

    public static GeometricObject max(GeometricObject first, GeometricObject second){
        if (first.compareTo(second) == 1){
            return first;
        }
        else
            return second;
    }


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

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

<— Return Home