Enable GeometricObject 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(20);
        Circle circle2 = new Circle(15);
        // GeometricObject geoobjects = new GeometricObject();
        System.out.println(Circle.max(circle1, circle2));
        Rectangle rect1 = new Rectangle(10, 20);
        Rectangle rect2 = new Rectangle(30, 15);
        System.out.println(Rectangle.max(rect1, rect2));
    }
}

<— 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

Rectangle.java

public class Rectangle extends GeometricObject{
    private double length;
    private double width;

    public Rectangle(){

    }

    public Rectangle(double length, double width){
        this.length = length;
        this.width = width;
    }

    public double getArea(){
        return length * width;
    }

    public double getPerimeter(){
        return (2 * length) + (2 * width);
    }

    @Override
    public String toString(){
        return "Area of Rectangle: " + 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