question archive Design an interface named Colorable with a void method named howToColor()

Design an interface named Colorable with a void method named howToColor()

Subject:Computer SciencePrice:2.87 Bought7

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. For each object in the array, display its area and invoke its howToColor method if it is colorable.

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:

we created an interface Colorable with a void method named howToColor()

Colorable.java

interface Colorable {

public void howToColor();
}

 

the class Square that extends Geometric Objectand implements Colorable and created two constructors one of them as default and another with argument and also created setter and getter method

 

Square.java

 

class Square extends GeometricObject implements Colorable {

public double side;

public Square() {

side=0.0;

}
public Square(double side) {

this.side = side;

}
public void howToColor() {

System.out.println("The Color of all four sides in Square");

}
public double getSide() {

return side;

}
public void setSide(double side) {

this.side = side;

}

public double getArea() {

double area = side * side;

return area;

}

}


 

GeometricObject .java created from the textbook

class GeometricObject {
private String color = "white";
private boolean filled;
private final java.util.Date dateCreated;
protected GeometricObject() {

dateCreated = new java.util.Date();

}
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;

}

public String getColor() {
return color;

}

public void setColor(String color) {
this.color = color;

}

public boolean isFilled() {

return filled;

}

public void setFilled(boolean filled) {

this.filled = filled;

}
public java.util.Date getDateCreated() {

return dateCreated;

}

public String toString() {

return "created on " + dateCreated + "\ncolor: " + color

+ " and filled: " + filled;

}
public double getArea(){

return 0.0;

}
}

 

 

test driver to run the program

 

TestDriver.java

public class TestDriver{

public static void main(String[] args) {

// created 5 array list 
GeometricObject[] output = {new Square(5), new GeometricObject(), new Square(9), new GeometricObject(), new Square(14)};

for (int i = 0; i < output.length; i++) {

System.out.println("The Area : " + output[i].getArea());

// for each loop
if (output[i] instanceof Colorable) {


((Colorable) output[i]).howToColor();
}}}}// end class


Related Questions