Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(The Octagon class) Write a class named Octagon that extends GeometricObject and

ID: 3677809 • Letter: #

Question

(The Octagon class) Write a class named Octagon that extends
GeometricObject 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/22)* side * side
Draw the UML diagram that involves Octagon, GeometricObject,
Comparable, and Cloneable. Write a test program that creates an Octagon
object with side value 5 and displays its area and perimeter. Create a new object
using the clone method and compare the two objects using the compareTo
method.

Explanation / Answer

//GeometricObject.java
public abstract class GeometricObject
   implements Comparable<GeometricObject>
{
  
   //private members of the class GeometricObject
   private String color;
   private boolean filled;
  
   //default constructor
   public GeometricObject()
   {
       color="";
       filled=false;
   }
   //Parameterized constructor
   public GeometricObject(String color, boolean filled)
   {  
       this.color = color;
       this.filled = filled;
   }

   //Returns color of object
   public String getColor()
   {
       return color;
   }

   //Sets color of the object
   public void setColor(String color)
   {
       this.color = color;
   }

   //Returns boolean of filled color
   public boolean isFilled()
   {
       return filled;
   }

   //set boolean value to filled color
   public void setFilled(boolean filled)
   {
       this.filled = filled;
   }

   //Returns string object of color and filled status
   public String toString()
   {
       return "Color: " + color + " and filled "+filled;               
   }
  
   public int compareTo(GeometricObject o) {
        if (this.getArea() > o.getArea())
            return 1;
        else if (this.getArea() < o.getArea())
            return -1;
        else
            return 0;
    }
      //abstract method declartations

    public abstract double getArea();
    public abstract double getPerimeter();
  
  
}

----------------------------------------------------------------------------------------------------------------------------------------------------

/**The java class Octagon that extends the GeometricObject
* and overrids methods getArea and getPerimeter*/
//Octagon.java
public class Octagon extends GeometricObject{
   //Declare side
   private double side;  
   //Constructor to set side
   public Octagon(double side) {
       this.side=side;
   }

   //Returns the area of the Octagon
   @Override
   public double getArea() {
       double area = (2 + 4/22)* side * side;
       return area;
   }
  

   //Returns the perimeter of the Octagon
   @Override
   public double getPerimeter() {      
       return 8*side;
   }
   //Clone the Octagon object
   public Object clone() {
       Octagon oct;
       oct = new Octagon(this.side);      
       return oct;
   }
}

----------------------------------------------------------------------------------------------------------------------------------------------------

/**
* The java program OctagonDriver that tests the Octagon
* and GeometricObject. Then finds the area and perimeter.
* Then test if two Octagon objects are equal and clone the
* octagon object and print to console.
* */
//OctagonDriver.java
public class OctagonDriver {  
   public static void main(String[] args) {
      
      
       Octagon octagon=new Octagon(5);
       System.out.println("Area of octagon: "+octagon.getArea());
       System.out.println("Perimeter of octagon: "+octagon.getPerimeter());
      
      
       Octagon octagonObject=new Octagon(5);
      
       if(octagon.compareTo(octagonObject)==0)
           System.out.println("Equal Octagon objects.");
       else
           System.out.println("Not equal.");
          
      
       Octagon cloneObject=(Octagon) octagon.clone();
       System.out.println("Clone object");
       System.out.println(octagonObject.toString());
      
      
      
   }

}

----------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output:

Area of octagon: 50.0
Perimeter of octagon: 40.0
Equal Octagon objects.
Clone object
Color: and filled false