Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the Shape hierarchy shown in Fig. 9. 3 in the text. Each Two Dimension

ID: 3667210 • Letter: I

Question

Implement the Shape hierarchy shown in Fig. 9. 3 in the text. Each Two Dimensional Shape should contain methods getArea and getPerimeter to calculate the area and perimeter of the two-dimensional shape. Each three Dimensional Shape 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 and perimeter. If it's a ThreeDimensionalShape, display its area and volume. The 3 top level classes should be abstract and should have at least one abstract method. You can instantiate a triangle object with 5 data points: base, height, and the length of each of the three sides. This needs to be coded in JAVA. Please also show output and explain how you did with comments.

Explanation / Answer

public abstract class Shape {

   private String id;
public Shape (String id)
{this.id = id;}

//This abstract method must be defined in a concrete subclass.
//Note that it is called in this class in the toString method.
public abstract double getArea();

public String getId()
{return id;}
  
public String toString( )
{return "Shape[id="+id+",area="+getArea()+"]";}

}

abstract class TwoDimensionalShape extends Shape{

   public TwoDimensionalShape(String id) {
       super(id);
   }
  
   public abstract double getPerimeter();
  
}

abstract class ThreeDimensionalShape extends Shape{

   public ThreeDimensionalShape(String id) {
       super(id);
   }
  
   public abstract double getVolume();
  
}

class Rectangle extends TwoDimensionalShape {
   private double width, height;
public Rectangle (String name, double w, double h)
{
super(name);
width = w;
height = h;
}   
//Overide the abstract method declared in Shape
public double getArea()
{return width*height;}
  
public double getWidth()
{return width;}
  
public double getHeight()
{return height;}
  
public void setWidthHeight(double newWidth, double newHeight)
{
width = newWidth;
height = newHeight;
}
  
public String toString( )
{
   return "Rectangle[width=" +width+ ",height="+height+ "," +super.toString()+ "]";
}
   @Override
   public double getPerimeter() {
       return 2*(width+height);
   }

}

class Circle extends TwoDimensionalShape {


private double radius;
public Circle (String name, double r)
{
super(name);
radius = r;
}
  
//Overide the abstract method declared in shape
public double getArea()
{return Math.PI * radius * radius;}
  
public double getRadius()
{return radius;}
  
public void setRadius(double newRadius)
{radius = newRadius;}
  
public String toString( )
{
   return "Circle[radius="+radius+","+super.toString()+"]";
}

   @Override
   public double getPerimeter() {
       return 2*Math.PI*radius;
   }
}

class Triangle extends TwoDimensionalShape {
private double a, b, c; // sides

public Triangle(String name,double a, double b, double c) {
super(name);
this.a = a;
this.b= b;
this.c = c;
}
   @Override
   public double getPerimeter() {
       return a + b + c;
   }

   @Override
   public double getArea() {
       double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
   }
   public String toString( )
{
       return "Triangle[side1=" +a+ ",side2="+b+ ",side3="+c+ "," +super.toString()+ "]";
}
}

class Sphere extends ThreeDimensionalShape {

private double radius;
public Sphere (String name, double r)
{
super(name);
radius = r;
}
  
//Overide the abstract method declared in shape
public double getArea()
{return 4*Math.PI * radius * radius;}
  
public double getRadius()
{return radius;}
  
public void setRadius(double newRadius)
{radius = newRadius;}
  
public String toString( )
{
   return "Circle[radius="+radius+","+super.toString()+"]";
}

   @Override
   public double getVolume() {
       return (4.0/3)*Math.PI*radius*radius*radius;
   }
}

class Cube extends ThreeDimensionalShape {
   private double a;
public Cube (String name, double a)
{
super(name);
this.a = a;
}   
//Overide the abstract method declared in Shape
public double getArea()
{
   return 6*a*a;
   }
  
public double getSide()
{return a;}
  
public String toString( )
{
   return "Cube[side=" +a +super.toString()+ "]";
}
   @Override
   public double getVolume() {
       return a*a*a;
   }

}

class Tetrahedron extends ThreeDimensionalShape {
  
   private double a;
public Tetrahedron (String name, double a)
{
super(name);
this.a = a;
}   
//Overide the abstract method declared in Shape
public double getArea()
{
   return Math.sqrt(3)*a*a;
   }
  
public double getSide()
{return a;}
  
public String toString( )
{
   return "Tetrahedron[side=" +a +super.toString()+ "]";
}
   @Override
   public double getVolume() {
       return (Math.sqrt(2)/12.0)*a*a*a;
   }

}

public class TestShape {
  
   public static void main(String[] args) {
       //Scanner sc = new Scanner(System.in);
      
       Shape[] shapes = {new Rectangle("Rectangle", 3, 4), new Circle("circle", 4), new Cube("cube", 3),
                           new Tetrahedron("tetrahedron", 5), new Triangle("triangle", 3,4,5)};
      
       for(Shape s: shapes){
           System.out.println(s.toString());
       }
      
   }

}