Write a class named Octagon that extends Geometric object and implements the Com
ID: 3683627 • Letter: W
Question
Write a class named Octagon that extends Geometric object and implements the Comparable and Cloneable interfaces. Assume that all eight sides of the octagon are of equal length. The area can be computed using the following formula: area = (2 + 4/squareroot 2) middot side middot side Write a test program that creates an Octagon object with side value 9 and displays its area and perimeter. Create a new object using the clonc method and compare the two objects using the SJ9.TOBAJDPJJ0 method. Expected results: Area is 391.10 Perimeter is 72.0 Compare the methods 0 2. Problem Design an interface named Colorable with a void method namedEvery class of a colorable object must implement the Colorable interface. Design a class named Square that extends and implements Colorable. Implement to display the message Color all four sides. Write a test program that creates an array of five Geometric objects (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 how To Color, 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 sidesExplanation / Answer
1)
public abstract class Octagon extends GeometricObject implements Comparable<Octagon>
{
private double side = 1.0;
protected native Object clone() throws CloneNotSupportedException;
public Octagon()
{
}
public Octagon(double side)
{
super();
this.side = side;
}
public void setSide(double side)
{
this.side = side;
}
public double getSide(double side)
{
return side;
}
public double getArea()
{
return (2 + (4 / (Math.sqrt(2))) * side * side);
}
public double getPerimeter()
{
return side * 8;
}
public String toString()
{
return "The length of each side is: " + side;
}
public int compareTo(Octagon octagon1)
{
if (getArea() >= octagon1.getArea())
return 1;
else if (getArea() < octagon1.getArea())
return -1;
else
return 0;
}
public interface Cloneable
{
}
}
2)
public class colorablesquare
{
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;
}
public double getArea()
{
return side * side;
}
public double getPerimeter()
{
return side * 4;
}
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.