Java Lab In this lab you will be implementing the following
Java Lab
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 Squareroot 3/4 a^2, a^2, and 3 Squareroot 3/2 a^2, 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
HI, Please find my implementation.
Please let me know in case of any issue.
public abstract class RegularPolygon {
private int numSides;
private int sideLength;
public RegularPolygon(int numSides, int sideLength) {
this.numSides = numSides;
this.sideLength = sideLength;
}
public void setNumSides(int numSides) {
this.numSides = numSides;
}
public int getNumSides() {
return numSides;
}
public void setSideLengths(int sideLength) {
this.sideLength = sideLength;
}
public int getSideLengths() {
return sideLength;
}
public int perimeter() {
return numSides*sideLength;
}
public abstract double area();
public String toString() {
String s = \"I am a shape with \" + numSides + \" sides of length: \";
s += \"\ I am a polygon.\";
return s;
}
}
class Hexagon extends RegularPolygon{
public Hexagon(int sideLength) {
super(6, sideLength);
}
public double area(){
return 3*Math.pow(3, 1.0/3.0)*getSideLengths()*getSideLengths()/2.0;
}
public String toString() {
String s = \"I am a shape with \" + getNumSides()+ \" sides of length: \";
s += \"\ I am a Quadrilateral.\";
return s;
}
}
///////////////////////////////////////////////////////////////////////////////
class Square extends RegularPolygon{
public Square(int sideLength) {
super(4, sideLength);
}
public double area(){
return getSideLengths()*getSideLengths();
}
public String toString() {
String s = \"I am a shape with \" + getNumSides()+ \" sides of length: \";
s += \"\ I am a Square.\";
return s;
}
}
////////////////////////////////////////////////////////////////////////////////////
// Triangle class
class Triangle extends RegularPolygon{
public Triangle( int sideLength) {
super(3, sideLength);
}
public double area(){
return Math.pow(3, 1.0/3.0)*getSideLengths()*getSideLengths()/4.0;
}
public String toString() {
String s = \"I am a shape with \" + getNumSides()+ \" sides of length: \";
s += \"\ I am a Triangle.\";
return s;
}
}
public class RegularPolygonTest{
public static void main(String[] args) {
RegularPolygon[] shapes = {
new Square(3),
new Triangle(6),
new Triangle(7)
};
for(RegularPolygon p : shapes){
System.out.println(p.toString());
System.out.println(\"Perimeter: \"+ p.perimeter());
System.out.println(\"Area: \"+p.area());
System.out.println();
}
}
}
/*
Sample run:
I am a shape with 4 sides of length:
I am a Square.
Perimeter: 12
Area: 9.0
I am a shape with 3 sides of length:
I am a Triangle.
Perimeter: 18
Area: 12.980246132766673
I am a shape with 3 sides of length:
I am a Triangle.
Perimeter: 21
Area: 17.66755723626575
*/



