155k views
5 votes
Design an interface named Colorable with a void method named howToColor(). Every class of a Colorable object must implement the Colorable interface. Design a class named Square that extends GeometricObject and implements Colorable. Override the method howToColor to display the message "Color all four sides." The Square class contains a private data field side with getter and setter methods, and a constructor for constructing a square with a specified side. Class GeometricObject can be downloaded from the Assignment 7 link on Canvas. Test your code by creating an array of GeometricObject and initialize it with three instances of Square, one instance of Circle, and one instance of Rectangle. Use a loop to iterate through the array and invoke method getArea from each instance; if applicable, invoke method howToColor on each instance.

1 Answer

5 votes

In Java, an interface named Colorable can be designed as follows:

The Java Code

// Colorable interface

interface Colorable {

void howToColor(); // Method declaration

}

// Square class extending GeometricObject and implementing Colorable

class Square extends GeometricObject implements Colorable {

private double side; // Private data field

// Constructor for Square class

public Square(double side) {

this.side = side;

}

// Getter method for side

public double getSide() {

return side;

}

// Setter method for side

public void setSide(double side) {

this.side = side;

}

// Override method howToColor to display the message

Override

public void howToColor() {

System.out.println("Color all four sides."); // Message to display

}

}

User Spinkoo
by
8.6k points