224k views
4 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 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 Chapter 13 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 thru the array and invoke method getArea from each instance; if applicable, invoke method howToColor 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 DaveGreen
by
9.1k points

1 Answer

5 votes

Final answer:

Answer involves defining a Colorable interface and a Square class that implements it, as well as testing with an array of GeometricObject instances.

Step-by-step explanation:

The student is required to design an interface named Colorable with a method howToColor(). Subsequently, a class named Square needs to be created which extends GeometricObject and implements the Colorable interface. The Square class should include a private data field for the side length and corresponding getter and setter methods. The constructor should initialize the side length, and the howToColor method must be overridden to display "Color all four sides".

To test the code, an array of GeometricObject instances should be created, containing three instances of Square, one instance of Circle, and one instance of Rectangle. A loop will iterate through the array, calling the getArea method on each and, if the object is colorable, the howToColor method as well, thus demonstrating polymorphism and interface implementation.

The subject of this question is Computers and Technology. It involves the design of an interface named Colorable and the implementation of this interface in the Square class. The goal is to override the howToColor() method in the Square class to display a specific message. The code provided includes the construction of objects and the testing of the getArea() and howToColor() methods.

User Damien B
by
8.4k points