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

Write a class named Hexagon that extends GeometricObject and implements the Comp

ID: 3805898 • Letter: W

Question

Write a class named Hexagon that extends GeometricObject and implements the Comparable interface. Assume all six sides of the hexagon are of equal size. The Hexagon class is defined as follows:

public class Hexagon extends GeometricObject implements Cloneable,    

    Comparable<Hexagon> {

private double side;

/** Construct a Hexagon with the specified side */

public Hexagon(double side) {

    // Implement it

   }

   @Override

public double getArea() {

       // Implement it ()

}

@Override

public double getPerimeter() {

    // Implement it

}

@Override

public int compareTo(Hexagon obj) {

    // Implement it (compare two Hexagons based on their sides)

   }

@Override

public Object clone() {

    // Implement it

}

}

Explanation / Answer

GeometricObject.java

public abstract class GeometricObject {

   GeometricObject() {

   }

   public abstract double getArea();

   public abstract double getPerimeter();

}

_________________

Hexagon.java

public class Hexagon extends GeometricObject implements Cloneable,
Comparable<Hexagon> {
   private double side;
  
  
   public Hexagon(double side) {
       this.side = side;
   }

   public double getSide() {
       return side;
   }

   public void setSide(double side) {
       this.side = side;
   }

   @Override
   public double getArea(){
       return ((3*Math.sqrt(3))/2)*side*side;
       }

   @Override
   public int compareTo(Hexagon o) {
       if (getArea() == ((Hexagon) o).getArea())
       return 0;
       else if (getArea() > ((Hexagon) o).getArea())
       return 1;
       else
       return -1;
   }

   @Override
   public double getPerimeter() {
      
       return 6*getSide();
   }
   @Override
   public Object clone() throws CloneNotSupportedException {
   return super.clone();
     
   }

}

___________________

package org.students;

public class Test {

   public static void main(String[] args) throws CloneNotSupportedException {
       Hexagon h1 = new Hexagon(6.5);
       System.out.printf("Area of Hexagon 1:%.2f " ,h1.getArea());
       System.out.printf("Perimeter of Hexagon 1:%.2f " , h1.getPerimeter());
       System.out.println("-----------------------");
       Hexagon h2 = new Hexagon(7.5);

       System.out.println("");
       System.out.printf("Area of Hexagon 2:%.2f " ,h2.getArea());
       System.out.printf("Perimeter of Hexagon 2:%.2f " , h2.getPerimeter());
       System.out.println("-----------------------");
       Hexagon h3 = new Hexagon(6.5);

       if (h1.compareTo(h3) == 1)
           System.out.println("Hexagon 1 is larger than Hexagon 3");
       else if (h1.compareTo(h3) == 0)
           System.out.println("Hexagon 1 is and Hexagon 3 are equal");
       else if (h1.compareTo(h3) == -1)
           System.out.println("Hexagon 1 is smaller than Hexagon 3");
       System.out.println("-----------------------");
       Hexagon h4 = (Hexagon) h2.clone();
       System.out.printf("Area of Hexagon 4:%.2f " ,h4.getArea());
       System.out.printf("Perimeter of Hexagon 4:%.2f " , h4.getPerimeter());
       if (h4.compareTo(h2) == 1)
           System.out.println("Hexagon 4 is larger than Hexagon 2");
       else if (h4.compareTo(h2) == 0)
           System.out.println("Hexagon 4 is and Hexagon 2 are equal");
       else if (h4.compareTo(h2) == -1)
           System.out.println("Hexagon 4 is smaller than Hexagon 2");

   }

}

____________________

Output:

Area of Hexagon 1:109.77
Perimeter of Hexagon 1:39.00
-----------------------

Area of Hexagon 2:146.14
Perimeter of Hexagon 2:45.00
-----------------------
Hexagon 1 is and Hexagon 3 are equal
-----------------------
Area of Hexagon 4:146.14
Perimeter of Hexagon 4:45.00
Hexagon 4 is and Hexagon 2 are equal

______________Thank You

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