The Colorable Interface

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) {
        GeometricObject[] shapes = new GeometricObject[5];
        shapes[0] = new Square(10);
        shapes[1] = new Square(20);
        shapes[2] = new Square(5);
        shapes[3] = new Square(1);
        shapes[4] = new Square(50);

        for (int i = 0; i < shapes.length; i++){
            System.out.println("Area: " + shapes[i].getArea());
            System.out.print("Tips to colour: ");
            ((Square)shapes[i]).howToColor();
        }
    }
}

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

Square.java

public class Square extends GeometricObject implements Colorable{
    private double side;
    public Square(){

    }

    public Square(double side){
        setSide(side);
    }

    public void setSide(double side){
        this.side = side;
    }

    public double getSide(){
        return side;
    }

    public void howToColor(){
        System.out.println("Color all four sides.");
    }

    public double getArea(){
        return Math.pow(side, 4);
    }

    public double getPerimeter(){
        return side * 4;
    }
}

<— Return Home

Colorable.java

public interface Colorable {
    void howToColor();
}

<— Return Home