Implement the shape hierarchy show in the figure below. Each TwoDimensionalShape
ID: 3712576 • Letter: I
Question
Implement the shape hierarchy show in the figure below. Each TwoDimensionalShape should contain method getArea to calculate the area of the two-dimensional shape. Each ThreeDimensionalShape should have methods getArea and getVolume to calculate the surface area and volume, respectively, of the three-dimensional shape. Create a program that uses an array of Shape references to objects of each class in the hierarchy. The program should printa text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether cach shape is a TwoDimensionalShape or a ThreeDimensionalShape. If it's a TwoDimensionalShape, display its area. If it's a ThreeDimensionalShape, display its area and volume. Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube TetrahedronExplanation / Answer
Shape.java
public class Shape {
//Declaring instance variables
private String name;
//Parameterized constructor
public Shape(String name) {
this.name = name;
}
//getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Shape is "+name;
}
}
________________
public abstract class TwoDimensionalShape extends Shape {
public TwoDimensionalShape(String name) {
super(name);
}
public abstract double getArea();
public abstract double getPerimeter();
}
_____________________
public class Circle extends TwoDimensionalShape {
//Declaring instance variables
private double radius;
//Parameterized constructor
public Circle(String name, double radius) {
super(name);
this.radius = radius;
}
//This method will calculate the area of the Circle
@Override
public double getArea() {
return Math.PI*radius*radius;
}
//This method will calculate the perimeter of the Circle
@Override
public double getPerimeter() {
return 2*Math.PI*radius;
}
}
__________________
public class Square extends TwoDimensionalShape {
//Declaring instance variables
private double side;
//Parameterized constructor
public Square(String name,double side) {
super(name);
this.side=side;
}
//This method will calculate the area of the Square
@Override
public double getArea() {
return side*side;
}
//This method will calculate the perimeter of the Square
@Override
public double getPerimeter() {
return 4*side;
}
}
_________________
public class Triangle extends TwoDimensionalShape {
//Declaring instance variables
private double base;
private double height;
//Parameterized constructor
public Triangle(String name, double base, double height) {
super(name);
this.base = base;
this.height = height;
}
//This method will calculate the area of the Triangle
@Override
public double getArea() {
return 0.5*base*height;
}
//This method will calculate the Perimeter of the Triangle
@Override
public double getPerimeter() {
return base+height+Math.sqrt((base*base)+(height*height));
}
}
________________
public abstract class ThreeDimensionalShape extends Shape {
public ThreeDimensionalShape(String name) {
super(name);
}
public abstract double getArea();
public abstract double getVolume();
}
________________
public class Cube extends ThreeDimensionalShape {
//Declaring instance variables
private double side;
//Parameterized constructor
public Cube(String name, double side) {
super(name);
this.side = side;
}
//This method will calculate the area of the Cube
@Override
public double getArea() {
return 6*side*side;
}
//This method will calculate the volume of the Cube
@Override
public double getVolume() {
return Math.pow(side,3);
}
}
_______________
public class Sphere extends ThreeDimensionalShape {
//Declaring instance variables
private double raidus;
//Parameterized constructor
public Sphere(String name, double raidus) {
super(name);
this.raidus = raidus;
}
//This method will calculate the area of the Sphere
@Override
public double getArea() {
return 4*Math.PI*raidus*raidus;
}
//This method will calculate the volume of the Sphere
@Override
public double getVolume() {
return (4.0/3.0)*Math.PI*Math.pow(raidus,3);
}
}
_________________
Tetrahedron.java
public class Tetrahedron extends ThreeDimensionalShape {
private double edge;
public Tetrahedron(String name, double edge) {
super(name);
this.edge = edge;
}
@Override
public double getArea() {
return Math.sqrt(3)*edge*edge;
}
@Override
public double getVolume() {
return Math.pow(edge,3)/6*Math.sqrt(2);
}
}
____________________
Test.java
public class Test {
public static void main(String[] args) {
Shape s[]={
new Circle("Ciecle",5.5),
new Square("Square",5),
new Triangle("Triangle",3,5),
new Sphere("Sphere",4.5),
new Cube("Cube",3),
new Tetrahedron("Tetrahedron",5)
};
for(int i=0;i<s.length;i++)
{
if(s[i] instanceof TwoDimensionalShape)
{
System.out.println("___"+s[i].getName()+"___");
System.out.printf("Area :%.2f ",((TwoDimensionalShape)s[i]).getArea());
}
else if(s[i] instanceof ThreeDimensionalShape)
{
System.out.println("___"+s[i].getName()+"___");
System.out.printf("Area :%.2f ",((ThreeDimensionalShape)s[i]).getArea());
System.out.printf("Volume :%.2f ",((ThreeDimensionalShape)s[i]).getVolume());
}
}
}
}
_____________________
Output:
___Ciecle___
Area :95.03
___Square___
Area :25.00
___Triangle___
Area :7.50
___Sphere___
Area :254.47
Volume :381.70
___Cube___
Area :54.00
Volume :27.00
___Tetrahedron___
Area :43.30
Volume :29.46
_____________Thank You