Write a class named Octagon that extends the class Circ and implements Comparabl
ID: 3839881 • Letter: W
Question
Write a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces.
Assume that all the 8 sides of the octagon are of equal size. The area can be computed according to the formula: area = (2 + 4/sqrt(2)) * side * side. Write a test program that creates an array of shapes consisting of several instances of Circ, Rect, and Octagon and displays their area and perimeter.
Create a new Octagon object by using the clone() method and compare the two objects using the compareTo() method. Include in your test program the method that returns the largest object in an array of objects. The method header is public static Object max(Object[] a) All the objects are assumed to be the instances of the Comparable interface. Apply the method max to your array of shapes to find and display the maximum one.
Therefore, your project should include the following classes: Circ, Rect, Shape, Octagon, and OctagonTester (the main class containing the main() method). Redesign the first two classes to implement the Comparable and Cloneable interfaces.
Explanation / Answer
public abstract class GeometricObject
{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
protected GeometricObject()
{
dateCreated = new java.util.Date();
}
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 + " Color: " + color + " and filled: " + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
public class Octagon extends GeometricObject
{
private double side = 1.0;
public Octagon()
{
}
public Octagon(double side)
{
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;
}
}
import java.util.*;
public class OctagonTester
{
public static void main(String[] args)
{
Octagon test = new Octagon(5.0);
System.out.println("The area of the octagon is: " + test.getArea();
System.out.println("The perimeter of the octagon is: " + test.getPerimeter();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.