In this lab, you will be implementing the following class hierarchy. Your concre
ID: 3799454 • Letter: I
Question
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.Explanation / Answer
RegularPolygon.java
public class RegularPolygon {
//Declaring instance variables
private int numSides;
private int sideLength;
/* Parameterized constructor by passing
* the no of sides and side length as arguments
*/
public RegularPolygon(int numSides, int sideLength) {
super();
this.numSides = numSides;
this.sideLength = sideLength;
}
//Getter methods
public int getNumSides() {
return numSides;
}
public int getSideLength() {
return sideLength;
}
//Method which calculate the perimeter
public double perimeter() {
return getNumSides() * getSideLength();
}
public double area() {
return 0;
}
}
___________________
Triangle.java
public class Triangle extends RegularPolygon {
//Creating parameterized constructor
public Triangle(int sideLength) {
super(3, sideLength);
}
//This method is used to calculate the area of the triangle
@Override
public double area() {
return (Math.sqrt(3)/4)*getSideLength()*getSideLength();
}
}
_____________________
Square.java
public class Square extends RegularPolygon {
//Creating parameterized constructor
public Square( int sideLength) {
super(4, sideLength);
}
//This method is used to calculate the area of the square
@Override
public double area() {
return getSideLength()*getSideLength();
}
}
___________________
Hexagon.java
public class Hexagon extends RegularPolygon {
//Creating parameterized constructor
public Hexagon(int sideLength) {
super(6, sideLength);
}
//This method is used to calculate the area of the Hexagon
@Override
public double area() {
return 3*(Math.sqrt(3)/2)*getSideLength()*getSideLength();
}
}
__________________
TestDriver.java
import java.text.DecimalFormat;
public class TestDriver {
public static void main(String[] args) {
// DecimalFormat class is used to format the output
DecimalFormat df = new DecimalFormat("#.##");
/*
* Creating the Triangle class object by passing the length of the side
* as argument
*/
Triangle t = new Triangle(5);
// Displaying the area of the equilateral triangle
System.out.println("The Area of Equilateral Traingle is :" + df.format(t.area()));
// Displaying the perimeter of the equilateral triangle
System.out.println("The Perimeter of Equilateral Traingle is :"+ df.format(t.perimeter()));
/*
* Creating the Square class object by passing the length of the side as
* argument
*/
Square s = new Square(6);
// Displaying the area of the Square
System.out.println("The Area of Square is :" + df.format(s.area()));
// Displaying the perimeter of the square
System.out.println("The Perimeter of Square is :" + df.format(s.perimeter()));
/*
* Creating the Hexagon class object by passing the length of the side
* as argument
*/
Hexagon h = new Hexagon(7);
// Displaying the area of the Hexagon
System.out.println("The Area of Hexagon is :" + df.format(h.area()));
// Displaying the perimeter of the hexagon
System.out.println("The Perimeter of Hexagon is :" + df.format(h.perimeter()));
}
}
_____________________
output:
The Area of Equilateral Traingle is :10.83
The Perimeter of Equilateral Traingle is :15
The Area of Square is :36
The Perimeter of Square is :24
The Area of Hexagon is :127.31
The Perimeter of Hexagon is :42
__________Thank YOu
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.