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

Q1 (15 Marks) Create a class named Hexagonthat extends shape class and implement

ID: 3860968 • Letter: Q

Question

Q1 (15 Marks) Create a class named Hexagonthat extends shape class and implements the Cloneableand Comparableinterfaces. The comparison should be based on the area of two shapes. Use the following method header: public int compareTo(Shape shp) Assume that all six sides of a hexagon are of equal length. Create appropriate setter and getter method(s) and constructor(s), and override the toString method to return a string representation of all attribute values as well as the area and perimeter of a hexagon object Write a test program that asks the user for the attributes of a Hexagonobject (i.e., side length, color, and filled), creates that object, and then displays its state (side, color, area, etc.). Assume users enter valid inputs. Create a new object using the clone method and compare the two objects using the compareTomethod. After running the compareTol) method, modify one the parameters of the cloned object (for example r2.height) then run the compareTo to check the outcome. Hint: Add "throws Clone NotSupportedException" to the header of the clonemethod as well as the mainmethod in your test program. You will learn more about exception handling in next lecture and lab. Sample run Color: Green Filled (Yes/No)? n Side: 5 First Hexagon color: Green. Not filled. Side: 5.8. Area : 64.9519852838329. Perimeter : 30.0 Clonned Hexagon color: Green. Not filled. side: 5.e. Area: 64.9519052838329. Perimeter: 30. Both Hexagon are identical. Modify the clonned Rec color: Green. Not filled. Side : 15.0. Area: 584.5671475544962. Perimeter: 90. the two Hexagon are different.

Explanation / Answer

Given below are the needed classes for the question. Please rate if the answer helped. Thank you.

Shape.java


public abstract class Shape {
   public abstract double getArea();
   public abstract double getPerimeter();
}

Hexagon.java


public class Hexagon extends Shape implements Cloneable, Comparable<Shape>{
   private double side;
   private boolean filled;
   private String color;
  
   public Hexagon()
   {
      
   }
   public Hexagon(double side, String color, boolean filled)
   {
       this.side = side;
       this.color = color;
       this.filled = filled;
      
   }
  
   public void setSide(double s)
   {
       side = s;
   }
  
   public double getSide()
   {
       return side;
   }
  
  
   public boolean isFilled() {
       return filled;
   }

   public void setFilled(boolean filled) {
       this.filled = filled;
   }

   public String getColor() {
       return color;
   }

   public void setColor(String color) {
       this.color = color;
   }

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

   @Override
   public double getPerimeter() {
       return 6 * side;
   }

   @Override
   public int compareTo(Shape o) {
       double a1 = getArea(), a2 = o.getArea();
       if(a1 == a2)
           return 0;
       else if(a1 > a2)
           return 1;
       else
           return -1;
          
   }
  
  
   @Override
   protected Object clone() throws CloneNotSupportedException {
       return new Hexagon(side, color, filled);
   }

   @Override
   public String toString(){
       String str = " Color: " + color ;
       if(filled)
           str += ". Filled. ";
       else
           str += ". Not filled. ";
       str += "Side: " + side + ", Area: " + getArea() + ", Perimeter: " + getPerimeter() + " ";
       return str;
   }

}

Rectangle.java


public class Rectangle extends Shape implements Cloneable, Comparable<Shape>{
   private double width, height;
   private boolean filled;
   private String color;
  
   public Rectangle()
   {
      
   }
   public Rectangle(double width, double height, String color, boolean filled)
   {
       this.width = width;
       this.height = height;
      
       this.color = color;
       this.filled = filled;
      
   }
  
   public void setWidth(double w)
   {
       width = w;
   }
   public void setHeight(double h)
   {
       height = h;
   }
  
   public double getWidth()
   {
       return width;
   }
  
   public double getHeight()
   {
       return height;
   }
  
  
   public boolean isFilled() {
       return filled;
   }

   public void setFilled(boolean filled) {
       this.filled = filled;
   }

   public String getColor() {
       return color;
   }

   public void setColor(String color) {
       this.color = color;
   }

   @Override
   public double getArea() {
       double area = width * height;
       return area;
   }

   @Override
   public double getPerimeter() {
       return 2 * (width + height);
   }

   @Override
   public int compareTo(Shape o) {
       double a1 = getArea(), a2 = o.getArea();
       if(a1 == a2)
           return 0;
       else if(a1 > a2)
           return 1;
       else
           return -1;
          
   }
  
  
   @Override
   protected Object clone() throws CloneNotSupportedException {
       return new Rectangle(width, height, color, filled);
   }

   @Override
   public String toString(){
       String str = " Color: " + color ;
       if(filled)
           str += ". Filled. " ;
       else
           str += ". Not filled. ";
       str += "Width: " + width + ", Height: " + height
               + ", Area: " + getArea() + ", Perimeter: " + getPerimeter() + " ";
       return str;
   }

}

ShapeTest.java

import java.util.Scanner;

public class ShapeTest {
   static Scanner keybd = new Scanner(System.in);
  
  
  
   public static void main(String[] args) throws CloneNotSupportedException {
       testHexagon();
       testRectangle();
   }
  
   private static void testHexagon() throws CloneNotSupportedException
   {
      
       Hexagon h1 = new Hexagon();
      
       System.out.println("Enter details of hexagon: ");
       System.out.print(" Color: ");
       h1.setColor(keybd.next());
      
       System.out.print(" Filled (y/n) ? ");
       if(keybd.next().equalsIgnoreCase("y"))
           h1.setFilled(true);
       else
           h1.setFilled(false);
      
       System.out.print(" Side: ");
       h1.setSide(keybd.nextInt());
      
      
       System.out.println(" First Hexagon" + h1);
       Hexagon h2 = (Hexagon)h1.clone();
       System.out.println("Clonned hexagon.");
       System.out.println(" Second Hexagon" + h2);
      
       if(h1.compareTo(h2) == 0)
           System.out.println("Both Hexagon are identical.");
       else
           System.out.println("The 2 hexagon are different.");
      
       System.out.println("Modifying the clonned hexagon");
       h2.setSide(h2.getSide() + 10);
       System.out.println(" Second Hexagon" + h2);
      
      
       if(h1.compareTo(h2) == 0)
           System.out.println("Both Hexagon are identical.");
       else
           System.out.println("The 2 hexagon are different.");
      
      
   }
  
   private static void testRectangle() throws CloneNotSupportedException
   {
      
       Rectangle r1 = new Rectangle();
      
       System.out.println("Enter details of rectangle: ");
       System.out.print(" Color: ");
       r1.setColor(keybd.next());
      
       System.out.print(" Filled (y/n) ? ");
       if(keybd.next().equalsIgnoreCase("y"))
           r1.setFilled(true);
       else
           r1.setFilled(false);
      
       System.out.print(" Width and Height: ");
       r1.setWidth(keybd.nextInt());
       r1.setHeight(keybd.nextInt());
      
      
       System.out.println(" First Rectangle" + r1);
       Rectangle r2 = (Rectangle)r1.clone();
       System.out.println("Clonned rectangle.");
       System.out.println(" Second rectangle" + r2);
      
       if(r1.compareTo(r2) == 0)
           System.out.println("Both rectangle are identical.");
       else
           System.out.println("The 2 rectangles are different.");
      
       System.out.println("Modifying the clonned rectangle");
       r2.setHeight(r2.getHeight() + 10);
       System.out.println(" Second rectangle" + r2);
      
      
       if(r1.compareTo(r2) == 0)
           System.out.println("Both rectangle are identical.");
       else
           System.out.println("The 2 rectangles are different.");
      
      
   }
}

output

Enter details of hexagon:
   Color: Green
   Filled (y/n) ? n
   Side: 5

First Hexagon
Color: Green. Not filled.
Side: 5.0, Area: 64.9519052838329, Perimeter: 30.0

Clonned hexagon.

Second Hexagon
Color: Green. Not filled.
Side: 5.0, Area: 64.9519052838329, Perimeter: 30.0

Both Hexagon are identical.
Modifying the clonned hexagon

Second Hexagon
Color: Green. Not filled.
Side: 15.0, Area: 584.5671475544962, Perimeter: 90.0

The 2 hexagon are different.
Enter details of rectangle:
   Color: Red
   Filled (y/n) ? y
   Width and Height: 5 15

First Rectangle
Color: Red. Filled.
Width: 5.0, Height: 15.0, Area: 75.0, Perimeter: 40.0

Clonned rectangle.

Second rectangle
Color: Red. Filled.
Width: 5.0, Height: 15.0, Area: 75.0, Perimeter: 40.0

Both rectangle are identical.
Modifying the clonned rectangle

Second rectangle
Color: Red. Filled.
Width: 5.0, Height: 25.0, Area: 125.0, Perimeter: 60.0

The 2 rectangles are different.