Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

--JAVA Program-- Design an interface named Colorable with a void method named ho

ID: 3680290 • Letter: #

Question

--JAVA Program--

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. Implement howToColor to display the message Color all four sides.

Write a test program that creates an array of five GeometricObjects. (new Square(2), new Circle(5), new Square(5), new Rectangle(3, 4), new Square(4.5)) For each object in the array, display its area and invoke its howToColor method if it is colorable.

Expected results:

Area is 4.00

Color all four sides

Area is 78.54

Area is 25.00

Color all four sides

Area is 12.00

Area is 20.25

Color all four sides

Explanation / Answer

Code:

public class GeometricObject // GeometricObject class
{
   double area; // private variable area

   GeometricObject() // constructor
   {
       area = 0;
   }

   public static void main(String args[])
   {
       Square S1 = new Square(2);
       Circle C = new Circle(5);
       Square S2 = new Square(5);
       Rectangle R = new Rectangle(3, 4);
       Square S3 = new Square(4.5);
       S1.calculate_area();
       S1.howToColor();
       C.calculate_area();

       S2.calculate_area();
       S2.howToColor();
       R.calculate_area();

       S3.calculate_area();
       S3.howToColor();
   }

}

interface Colorable // interface colorable is defined
{
   void howToColor(); // only declaration of method in interface
}

class Square extends GeometricObject implements Colorable // square class extends GeometricObject and implement Colorable interface
{
   double side; // variable side of square

   Square(double d)
   {
       side = d;
   }

   @Override
   public void howToColor() // overriding of howToColor method of Colorable interface
   {

       System.out.println("Color all four sides");

   }

   public void calculate_area() // function for printing area of square
   {
       System.out.println("Area is :" + side * side);
   }

}

class Circle extends GeometricObject
{
   double radius;

   Circle(double rad)
   {
       radius = rad;
   }

   public void calculate_area()
   {
       System.out.println("Area is :" + 3.14 * radius * radius);
   }

}

class Rectangle extends GeometricObject
{
   double length, breadth;

   Rectangle(double len, double bre)
   {
       length = len;
       breadth = bre;
   }

   public void calculate_area()
   {
       System.out.println("Area is :" + length * breadth);
   }

}

Sample input and output:

Area is :4.0
Color all four sides
Area is :78.5
Area is :25.0
Color all four sides
Area is :12.0
Area is :20.25
Color all four sides