(The Octagon class) Write a class named Octagon thatextends GeometricObject and
ID: 3611423 • Letter: #
Question
(The Octagon class) Write a class named Octagon thatextends GeometricObject and implements the Comparableand Cloneable interfaces. Assume that all eight sides of theoctagon are of equal size. The area can be computed using thefollowing formula:area = A2 + 4/22Bside*sideDraw the UML diagram that involves Octagon, GeometricObject, Comparable, and Cloneable. Write a test programthat creates an Octagon object with side value 5 and displays itsarea and perimeter. Create a new object using the clone method andcompare the two objects using the compareTo method.
Explanation / Answer
public String toString()
{
return super.toString() + " Side size: "+ side;
}
// JUST fortesting!
public static void main(String[] args) throws Exception {
Octagon size4 = new Octagon(4);
System.out.println(" Size 4 Octagon: " + size4);
Octagon size5 = new Octagon(5);
System.out.println(" Size 5 Octagon: " + size5);
Octagon size6 = new Octagon(6);
System.out.println(" Size 6 Octagon: " + size6);
Octagon cloneCopy = (Octagon)size5.clone();
if (size5.compareTo(cloneCopy) == 0)
System.out.println("Original and clone are samesize.");
else
System.out.println("Original and clone are NOTthe same size???");
if(size4.compareTo(size5) < 0)
System.out.println("Size4 smaller thansize5.");
else if (size4.compareTo(size5) == 0)
System.out.println("Size4 same size assize5.");
else
System.out.println("Size4 larger thansize5.");
if (size6.compareTo(size5) < 0)
System.out.println("Size6 smaller thansize5.");
else if (size6.compareTo(size5) == 0)
System.out.println("Size6 same size assize5.");
else
System.out.println("Size6 larger thansize5.");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.