In this lab you will be implementing the following class hie
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


