In this lab you will be implementing the following class hie

In this lab, you will be implementing the following class hierarchy. Your concrete classes must call the superclass constructor with the proper number of sides (which is constant for each concrete shape). The perimeter method is implemented in the superclass as it does not change based on the number of sides. The area method must be overridden in each subclass as the area is dependent on the type of polygon. The areas of an equilateral triangle, square, and a regular hexagon given side length a are Ma2, a2, and SyAa2, respectively. The superclass area method should just return 0 as a default value. You are also responsible for writing a small test driver that instantiates each of the concrete subclasses with test values (not necessarily user input), and prints the perimeter and area of each object to the console.

Solution

*************************** RegularPolygon.java **********************

package com.regularpolygon;

public class regularPolygon {
  
   private int numSides;
  
   public int getNumSides() {
       return numSides;
   }

   public void setNumSides(int numSides) {
       this.numSides = numSides;
   }

   private int sideLength;
  
   public int getSideLength() {
       return sideLength;
   }

   public void setSideLength(int sideLength) {
       this.sideLength = sideLength;
   }

   public regularPolygon(int numSides, int sideLength){
       this.numSides = numSides;
       this.sideLength = sideLength;
   }
  
   protected double area(){
       return this.getNumSides();
   }
  
   protected double perimeter(){
       return this.getNumSides() * this.getSideLength();
   }
  

}

******************************* Triangle.java *************************************

package com.regularpolygon;
import java.lang.Math;

public class Triangle extends regularPolygon{

   public Triangle(int sideLength) {
       super(3, sideLength);
   }
  
   protected double area(){
       // Rounding to the 2 decimal places
       return Math.round((Math.sqrt(3)/4)*Math.pow(this.getSideLength(),2) * 100.0) / 100.0;
   }

   public static void main(String[] args) {
       Triangle t = new Triangle(4);
       System.out.println(\"The Perimeter of the Triangle is: \" + t.perimeter());
       System.out.println(\"The area of the Triangle is: \" + t.area());
   }

}

************************************ Square.java ************************************

package com.regularpolygon;

public class Square extends regularPolygon{

   public Square(int sideLength) {
       super(4, sideLength);
   }
  
   public double area(){
       return Math.round(Math.pow(this.getSideLength(), 2) * 100.0) / 100.0;
   }

   public static void main(String[] args) {
       Square s = new Square(5);
       System.out.println(\"The Perimeter of the Square is: \" + s.perimeter());
       System.out.println(\"The area of the Square is: \" + s.area());
   }

}

********************************* Hexagon.java *******************************************

package com.regularpolygon;

public class Hexagon extends regularPolygon {

   public Hexagon(int sideLength) {
       super(6, sideLength);
   }
  
   public double area(){
       return Math.round((3*Math.sqrt(3)*Math.pow(this.getSideLength(), 2))/2 * 100.0) / 100.0;
   }

   public static void main(String[] args) {
       Hexagon h = new Hexagon(6);
       System.out.println(\"The Perimeter of the Hexagon is: \" + h.perimeter());
       System.out.println(\"The area of the Hexagon is: \" + h.area());
   }

}

Instructions:

In eclipse, create a new Java project and then enter com.regularpolygon in the package field, or else just place these files in com/regularpolygon and run the files

************************* Output ***********************************

The Perimeter of the Triangle is: 12.0
The area of the Triangle is: 6.93

The Perimeter of the Square is: 20.0
The area of the Square is: 25.0

The Perimeter of the Hexagon is: 36.0
The area of the Hexagon is: 93.53

 In this lab, you will be implementing the following class hierarchy. Your concrete classes must call the superclass constructor with the proper number of sides
 In this lab, you will be implementing the following class hierarchy. Your concrete classes must call the superclass constructor with the proper number of sides
 In this lab, you will be implementing the following class hierarchy. Your concrete classes must call the superclass constructor with the proper number of sides

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site