Design an interface named Colorable with a void method named howToColor). Every
ID: 3720222 • Letter: D
Question
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". The Square class contains a data field named side with its getter and setter methods, and constructor for constructing Square with specific side. It has no- arg constructor to create a Square with side 0, and another constructor that creates a Square with specified side Write a test program that creates an array of four GeometricObjects. For each object in the array, display its area and invoke its howToColor method if it is colorable. Note: You can use three classes: Circle (from Problem), Rectangle (from attached code) and Square class from this problem. You may create one more (4th) concrete class from the attached abstract class GeometricObject, as an example Triangle.Explanation / Answer
public class Geometric
{
public static void main(String[] args) {
GeometricObject[] geoShapes = new GeometricObject[5];
geoShapes[0] = new MyRectangle2D();
geoShapes[1] = new Circle2D();
geoShapes[2] = new Square();
geoShapes[3] = new MyRectangle2D(0, 0, 10, 5);
geoShapes[4] = new Square(0,0,25);
for (int i = 0; i < geoShapes.length; i++) {
System.out.println("shape #" + (i + 1) + " area = " + geoShapes[i].getArea());
if (geoShapes[i] instanceof Colorable) {
System.out.println("How to color: "+((Colorable)geoShapes[i]).howToColor());
}
}
}
}
class Square extends GeometricObject implements Colorable {
private double x;
private double y;
private double side;
Square() {
this(0,0,10);
}
Square(double x, double y, double side) {
this.x = x;
this.y = y;
this.side = side;
}
@Override
public double getArea() {
return side * side;
}
@Override
public double getPerimeter() {
return side * 4;
}
@Override
public String howToColor() {
return "Color all four sides.";
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
}
interface Colorable {
String howToColor();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.