Implement the Shape hierarchy shown in 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 concrete class in the hierarchy. The program should print a 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 each 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.
Instructions
1) Implement the Shape
hierarchy shown above.
Each
TwoDimensionalShape
should contain the methodgetArea
to calculate the area of the two-dimensional shape.Each
ThreeDimensionalShape
should have methodsgetArea
andgetVolume
to calculate the surface area and volume, respectively, of the three-dimensional shape.
2) Create a program that uses an array of Shape
references to objects of each concrete class in the hierarchy.
The program should print a 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 each shape is a
TwoDimensionalShape
or aThreeDimensionalShape
.If it's a
TwoDimensionalShape
, display its area. If it's aThreeDimensionalShape
, display its area and volume.

Solution
Shape.java
package shapes;
/**
*
* @author Archangel Macsika
*/
public abstract class Shape {
private double dimOne;
private double dimTwo;
private double dimThree;
//1 arg constructor
public Shape (double dim1) {
dimOne = dim1;
}
//2 arg constructor
public Shape (double dim1, double dim2) {
dimOne = dim1;
dimTwo = dim2;
}
//3 arg constructor
public Shape (double dim1, double dim2, double dim3){
dimOne = dim1;
dimTwo = dim2;
dimThree = dim3;
}
//Setters and getters:
//dim 1
public void setDimOne(double dim1) {
dimOne = dim1;
}
public double getDimOne() {
return dimOne;
}
//dim2
public void setDimTwo(double dim2) {
dimTwo = dim2;
}
public double getDimTwo() {
return dimTwo;
}
//dim3
public void setDimThree(double dim3) {
dimThree = dim3;
}
public double getDimThree() {
return dimThree;
}
public abstract double getArea();
public String getName() {
return getClass().getName();
}
@Override
public String toString(){
return "area = " + getArea();
}
}
TwoDimensionalShape.java
package shapes;
/**
*
* @author Archangel Macsika
*/
public abstract class TwoDimensionalShape extends Shape {
public TwoDimensionalShape(double dim1) {
super(dim1);
}
public TwoDimensionalShape(double dim1, double dim2) {
super(dim1, dim2);
}
public abstract double getArea();
}
ThreeDimensionalShape.java
package shapes;
/**
*
* @author Archangel Macsika
*/
public abstract class ThreeDimensionalShape extends Shape {
public ThreeDimensionalShape(double dim1) {
super(dim1);
}
public ThreeDimensionalShape (double dim1, double dim2, double dim3) {
super(dim1, dim2, dim3);
}
public abstract double getArea();
public abstract double getVolume();
}
Circle.java
package shapes;
/**
*
* @author Archangel Macsika
*/
import java.lang.Math;
public class Circle extends TwoDimensionalShape {
public Circle(double dim1) {
super(dim1);
}
private double circleArea = (Math.pow(getDimOne(), 2) * Math.PI);
@Override
public double getArea()
{
return circleArea;
}
@Override
public String toString()
{
return " radius: " + getDimOne();
}
}
Square.java
package shapes;
/**
*
* @author Archangel Macsika
*/
public class Square extends TwoDimensionalShape {
public Square(double dim1) {
super(dim1);
}
private double squareArea = getDimOne() * getDimOne();
@Override
public double getArea()
{
return squareArea;
}
@Override //first line of console
public String toString()
{
return " length: " + getDimOne();
}
}
TriangleShape.java
package shapes;
/**
*
* @author Archangel Macsika
*/
public class Triangle extends TwoDimensionalShape {
//2 arg constructor
public Triangle(double dim1, double dim2) {
super(dim1, dim2);
}
private double triangleArea = getDimOne() * getDimTwo() * 0.5;
@Override
public double getArea() {
return triangleArea;
}
@Override
public String toString(){
return " base: " + getDimOne() + " height: " + getDimTwo();
}
}
Tetrahedron.java
package shapes;
/**
*
* @author Archangel Macsika
*/
public class Tetrahedron extends ThreeDimensionalShape{
public Tetrahedron (double dim1, double dim2, double dim3) {
super(dim1, dim2, dim3);
}
private double tetrahedronArea = 4 * (0.5 * getDimOne() * getDimTwo()) ;
private double tetrahedronVolume = ((0.5 * getDimOne() * getDimTwo()) * getDimThree())/3;
@Override
public double getArea() {
return tetrahedronArea;
}
@Override
public double getVolume() {
return tetrahedronVolume;
}
@Override
public String toString() {
return " base width: " + getDimOne() + " base height: " + getDimTwo() + " height " + getDimThree();
}
}
Cube.java
package shapes;
/**
*
* @author Archangel Macsika
*/
public class Cube extends ThreeDimensionalShape {
public Cube(double dim1) {
super(dim1);
}
private double cubeArea = 6 * Math.pow(getDimOne(), 2.0);
private double cubeVolume = Math.pow(getDimOne(), 3.0);
@Override
public double getArea() {
return cubeArea;
}
@Override
public double getVolume() {
return cubeVolume;
}
@Override
public String toString() {
return " length: " + getDimOne();
}
}
Sphere.java
package shapes;
/**
*
* @author Archangel Macsika
*/
public class Sphere extends ThreeDimensionalShape {
public Sphere(double dim1) {
super(dim1);
}
private double sphereArea = 4 * (Math.pow(getDimOne(), 2) * Math.PI);
private double sphereVolume = Math.PI * 4/3 * Math.pow(getDimOne(), 3.0);
@Override
public double getArea() {
return sphereArea;
}
@Override
public double getVolume() {
return sphereVolume;
}
@Override
public String toString() {
return " radius: " + getDimOne();
}
}
Main Class
/**
*
* @author Archangel Macsika
*/
public class Shapes {
public static void main(String[] args) {
Shape shape[] = new Shape[6];
shape[0] = new Circle(7.0);
shape[1] = new Square(7.0);
shape[2] = new Triangle(4.0, 5.0);
shape[3] = new Sphere(1.0);
shape[4] = new Cube(1.0);
shape[5] = new Tetrahedron(1.0);
for (Shape curentShape : shape) {
System.out.println(curentShape);
if (curentShape instanceof TwoDimentionalShape) {
TwoDimentionalShape twoDimentionalShape = (TwoDimentionalShape) curentShape;
System.out.printf("%sArea: %.2f\n\n", twoDimentionalShape.toString(), twoDimentionalShape.getArea());
} else if (curentShape instanceof ThreeDimensionalShape) {
ThreeDimensionalShape threeDimensionalShape = (ThreeDimensionalShape) curentShape;
System.out.printf("%sArea: %.2f\n\n", threeDimensionalShape.toString(), threeDimensionalShape.getVolume());
System.out.printf("%sArea: %.2f\n\n", threeDimensionalShape.toString(), threeDimensionalShape.getArea());
}
}
}
}