162k views
0 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, along with a constructor for constructing a square with a specified side. The GeometricObject class 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 the getArea() method from each instance; if applicable, invoke the howToColor() method on each instance.

Here is a sample run:
Area is 4.0. Color all four sides.
Area is 20.25. Color all four sides.
Area is 25.0. Color all four sides.
Area is 78.53981633974483.
Area is 12.0.

User Tornike
by
8.1k points

1 Answer

2 votes

Final answer:

The question is about designing an interface and implementing it in a class. The code creates an array of objects and calls methods on each object, including those defined in the interface.

Step-by-step explanation:

The subject of this question is Computers and Technology, and it is aimed at High School students.

The Colorable interface is designed to have a void method called howToColor(). Every class that implements the Colorable interface must provide an implementation for the howToColor() method.

In this case, the Square class extends the GeometricObject class and implements the Colorable interface. The Square class overrides the howToColor() method to display the message 'Color all four sides'. The Square class also has a private data field called side with getter and setter methods, as well as a constructor to initialize the side length.

The code then creates an array of GeometricObject and initializes it with instances of the Square, Circle, and Rectangle classes. A loop is used to iterate through the array and invoke the getArea() method for each instance. If the instance implements the Colorable interface, the loop also invokes the howToColor() method.