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

Write a java program that has a class named Octagon that extends the class Circ

ID: 3841404 • Letter: W

Question

Write a java program that has 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. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables.

Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method getSide() for computing Octagon's side. Its area then 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 java classes: Circ, Rect, Shape, Octagon, and ShapeMaster (the main class containing the main() method). Redesign the first two classes to implement the Comparable and Cloneable interfaces.

Explanation / Answer

Shape.java

package shape;

public abstract class Shape{
  
    public abstract double getArea();   
   public abstract double getPerimeter();
   public static void main(String args[])
   {
   }
}

Circ.java

package shape;
import shape.*;

public class Circ extends Shape{
   //data fields
        protected double r;
  
   //Constructor
   Circ(){}
   Circ(double r)
   {
       this.r = r;
   }   
   //Getter and Setter methods
   public void setRadius(double r)
   {
       this.r = r;
   }
   public double getRadius()
   {
       return r;
   }
        public double getArea()
   {
       return (3.14 * r *r);
   }   
   public double getPerimeter()
   {
       return (2 * 3.14 * r);
   }   
   public static void main(String args[])
   {
   }
}

Rect.java

package shape;
import shape.*;

public class Rect extends Shape{
   //data fields
        private double length;
   private double breadth;
       
   //Getter and Setter methods
   public void setLength(double l)
   {
       length = l;
   }
   public double getLength()
   {
       return length;
   }
   public void setBreadth(double b)
   {
       breadth= b;
   }
   public double getBreadth()
   {
       return breadth;
   }
        public double getArea()
   {
       return (length * breadth);
   }  
   public double getPerimeter()
   {
       return (2 * (length + breadth));
   }
}

Octagon.java

package shape;
import shape.*;


public class Octagon extends Circ implements Comparable, Cloneable
     {
        Octagon()
        {  
        }
        Octagon(double r)
        {
            super();
        }       
        public double getSide()
        {
       return Math.sqrt((4*r*r) / 4+(2*1.141));
        }
        @Override
        public double getArea()
        {          
       return (2 + (4 / (Math.sqrt(2))) * this.getSide()*this.getSide());
        }
        @Override
        public double getPerimeter()
        {
       return (this.getSide() * 8);
            }
        @Override
        public int compareTo(Shape S1)
        {
       if(this.getArea() > S1.getArea())
           return -1;
       else if(this.getArea() > S1.getArea())
           return 1;
              else
           return 0;
        }
        @Override
        public Octagon clone() throws CloneNotSupportedException{
           return ((Octagon)super.clone());
        }
    }

OctagonTester

package shape;
import shape.*;

public class OctagonTester {
public static void main(String args[]){

   Circ C1 = new Circ(4);
   Octagon O2 = new Octagon(){
   O2.clone();
   };
  
   System.out.println("Area of Circle C1: "+C1.getArea()+" "+"Perimeter of Circle C1"+C1.getPerimeter());  
   System.out.println("Side of Octagon O2: "+O2.getSide()+" "+"Area of Octagon O2"+O2.getArea());

   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote