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

1. Using an inheritance hierarchy, design a Java program to model 3-dimensional

ID: 3676979 • Letter: 1

Question

1. Using an inheritance hierarchy, design a Java program to model 3-dimensional shapes (square pyramid, sphere, rectangular prism, cube, cylinder, circular cone). Have a top level shape interface with methods for getting the area and the volume (+ methods toString and equals). Next, build classes and subclasses for the above 3-dimensional shapes. Make sure that you place common behavior in superclasses whenever possible. Also, use abstract classes as appropriate. Add methods to subclasses to represent unique behavior particular to each 3-dimensional shape.
Write the definitions of these classes and do the testing with the client program provided.

Reference:

   1) Square pyramid

2) Sphere

3) Rectangular prism

4) Cube

5) Cylinder

6) Circular Cone

From: Google support

Classes:

// Interface Shape3D: for three-dimensional shapes.
public interface Shape3D {
    public double getArea();
    public double getVolume();
    public String toString();
    public boolean equals(Object obj);
}

// Class SquarePyramid. Implements Shape3D
// Represents a pyramid with a square as its base.
public class SquarePyramid implements Shape3D {
    private double length;
    private double height;
  
    public SquarePyramid() {
        length = 0;
        height = 0;
    }
    public SquarePyramid(double l, double h) {
        ...
    }

    public double getLength() {
        ...
    }

    public double getHeight() {
       ...
    }

    public double getArea() {
      return ((length / 2.0) * Math.sqrt((length *length) / 4.0 + height * height));
    }

    public double getVolume() {
       return length * length * height / 3.0;
    }
  
    public String toString() {
       ...
    }
  
    public boolean equals(Object obj) {
       ...
    }
}

// Class Sphere. Implements Shape3D
// Represents a perfect sphere.

public class Sphere implements Shape3D {
    private double radius;
  
    public Sphere() {
       ...
    }
  
    public Sphere(double r) {
       ...
    }

    public double getRadius() {
       ...
    }

    public double getArea() {
       return 4.0 * Math.PI * radius * radius;
    }


    public double getVolume() {
       return 4.0 * Math.PI * Math.pow(radius, 3) / 3.0;
    }
  
    public String toString() {
       ...
    }
  
    public boolean equals(Object obj) {
       ...
    }
}

// Class RectangularPrism. Implements Shape3D
// Represents a three-dimensional rectangular shape.

public class RectangularPrism implements Shape3D {
    private double length;
    private double width;
    private double height;
  
    public RectangularPrism() {
       ...
    }
  
    public RectangularPrism(double l, double w, double h) {
       ...
    }

    public double getLength() {
       ...
    }

    public double getWidth() {
       ...
    }
  
    public double getHeight() {
       ...
    }

    public double getArea() {
       return 2 * (width * length + height * length + height * width);
    }

    public double getVolume() {
       return length * length * height;
    }
  
    public String toString() {
       ...
    }
  
    public boolean equals(Object obj) {
       ...
    }
}

// Class Cube, subclass of RectangularPrism
// Represents a perfect cube.

public class Cube extends RectangularPrism {
    public Cube() {
       ...
    }
  
    public Cube(double size) {
       ...
    }
  
    public String toString() {
       ...
    }
}

// Class CircularShape. Implements Shape3D.
// ABSTRACT CLASS --> no objects of this type!
// An abstract superclass for shapes with a circular cross-section.

public abstract class CircularShape implements Shape3D {
    private double radius;
  
    public CircularShape() {
       ...
    }
  
    public CircularShape(double r) {
       ...
    }
  
    public double getDiameter() {
       ...
    }

    public double getRadius() {
       ...
    }
  
    public double getCrossSectionArea() {
       return Math.PI * Math.pow(radius, 2);
    }
  
    public double getCrossSectionPerimeter() {
       return 2 * Math.PI * radius;
    }
}

// Class CircularShapeWithHeight. Subclass of CircularShape
// ABSTRACT CLASS --> no objects of this type!
// An abstract superclass for shapes with a circular cross-section that extends over some height.

public abstract class CircularShapeWithHeight extends CircularShape {
    private double height;

    public CircularShapeWithHeight() {
       ...
    }
    public CircularShapeWithHeight(double radius, double height) {
       ...
    }

    public double getHeight() {
       ...
    }
}

// Class Cylinder, subclass of CircularShapeWithHeight
// Represents a cylinder shape.

public class Cylinder extends CircularShapeWithHeight {
    public Cylinder() {
       ...
    }
  
    public Cylinder(double radius, double height) {
       ...
    }

    public double getArea() {
       return getCrossSectionPerimeter() * getHeight() + 2 * getCrossSectionArea();
    }

    public double getVolume() {
       return getCrossSectionArea() * getHeight();
    }
  
    public String toString() {
       ...
    }
  
    public boolean equals(Object obj) {
       ...
    }
}

// Class CircularCone, subclass of CircularShapeWithHeight
// Represents cones with a circular base.

public class CircularCone extends CircularShapeWithHeight {
    public CircularCone() {
       ...
    }
  
    public CircularCone(double radius, double height) {
       ...
    }

    public double getArea() {
       double r = getRadius();

       double h = getHeight();

       return Math.PI * r * (r + Math.sqrt(r * r + h * h));
    }

    public double getVolume() {
       return getCrossSectionArea() * getHeight() / 3.0;
    }
  
    public String toString() {
       ...
    }
  
    public boolean equals(Object obj) {
       ...
    }
}

//USE this client to test them all! Analyze the client.
public class Shape3D_Client {
    public static final int MAX = 6;
    public static void main(String[] args) {
        Shape3D[] shapes = new Shape3D[MAX];
        shapes[0] = new SquarePyramid(37, 20);
        shapes[1] = new Sphere(20);
        shapes[2] = new RectangularPrism(10, 20, 37);
        shapes[3] = new Cube(10);
        shapes[4] = new Cylinder(10, 20);
        shapes[5] = new CircularCone(10, 20);
  
        for (int i = 0; i < shapes.length; i++) {

            System.out.println("------------------------------------------------------------------");
            System.out.print("This is a ");

            switch(i) {
                case 0:
                    System.out.print("square pyramid. ");
                    break;
                case 1:
                    System.out.print("sphere. ");
                    break;
                case 2:
                    System.out.print("rectangular prism. ");
                    break;
                case 3:
                    System.out.print("cube. ");
                    break;
                case 4:
                    System.out.print("cylinder. ");
                    break;
                case 5:
                    System.out.print("circular cone. ");
            }

            
            System.out.printf("Area = %.2f. ", shapes[i].getArea());
            System.out.printf("Volume = %.2f. ", shapes[i].getVolume());

            System.out.println("------------------------------------------------------------------");
            System.out.println("Output calling the method printInfo - polymorphism at work!");
            printInfo(shapes[i]);
            System.out.println("------------------------------------------------------------------" );
        } // end of for
    } // end of main
  
    public static void printInfo(Shape3D s) {
        System.out.println(s);
        System.out.printf(" Area = %.2f. ", s.getArea());
        System.out.printf("Volume = %.2f ", s.getVolume());
    }
}

EXPECTED OUTPUT:

------------------------------------------------------------------
This is a square pyramid. Area = 504.02. Volume = 9126.67.
------------------------------------------------------------------
Output calling the method printInfo - polymorphism at work!
SqauarePyramid: length = 37.0, height = 20.0
    Area = 504.02. Volume = 9126.67.
------------------------------------------------------------------

------------------------------------------------------------------
This is a sphere. Area = 5026.55. Volume = 33510.32.
------------------------------------------------------------------
Output calling the method printInfo - polymorphism at work!
Sphere: radius = 20.0
    Area = 5026.55. Volume = 33510.32.
------------------------------------------------------------------

------------------------------------------------------------------
This is a rectangular prism. Area = 2620.00. Volume = 7400.00.
------------------------------------------------------------------
Output calling the method printInfo - polymorphism at work!
RectangularPrism: length = 10.0, length = 20.0, height = 37.0
    Area = 2620.00. Volume = 7400.00.
------------------------------------------------------------------

------------------------------------------------------------------
This is a cube. Area = 600.00. Volume = 1000.00.
------------------------------------------------------------------
Output calling the method printInfo - polymorphism at work!
Cube: size = 10.0
    Area = 600.00. Volume = 1000.00.
------------------------------------------------------------------

------------------------------------------------------------------
This is a cylinder. Area = 1884.96. Volume = 6283.19.
------------------------------------------------------------------
Output calling the method printInfo - polymorphism at work!
Cylinder: radius = 10.0, height = 20.0
    Area = 1884.96. Volume = 6283.19.
------------------------------------------------------------------

------------------------------------------------------------------
This is a circular cone. Area = 1016.64. Volume = 2094.40.
------------------------------------------------------------------
Output calling the method printInfo - polymorphism at work!
CircularCone: radius = 10.0, height = 20.0
    Area = 1016.64. Volume = 2094.40.
------------------------------------------------------------------

Explanation / Answer

SquarePyramid.java

public class SquarePyramid implements Shape3D {

private double length;

private double height;

   public SquarePyramid() {

       length = 0;

       height = 0;

   }

   public SquarePyramid(double l, double h) {

       length = l;

       height = h;

   }

   public double getLength() {

       return length;

   }

   public double getHeight() {

       return height;

   }

   public double getArea() {

       return ((length / 2.0) * Math.sqrt((length * length) / 4.0 + height * height));

   }

   public double getVolume() {

       return length * length * height / 3.0;

   }

   public String toString() {

       String s = "Square Pyramid: length = " + length + ", height = " + height;

       return s;

   }

   public boolean equals(Object obj) {

       if (this == obj) {

           return true;

       }

       //if obj is not a instance of given class no need to check further

       if (!(obj instanceof SquarePyramid)) {

           return false;

       }

       SquarePyramid sp = (SquarePyramid) obj;

       if (this.length == sp.length && this.height == sp.height) {

           return true;

       }

       return false;

   }

}

Sphere.java

public class Sphere implements Shape3D {

private double radius;

   public Sphere() {

       radius = 0;

   }

   public Sphere(double r) {

       radius = r;

   }

   public double getRadius() {

       return radius;

   }

   public double getArea() {

       return 4.0 * Math.PI * radius * radius;

   }

   public double getVolume() {

       return 4.0 * Math.PI * Math.pow(radius, 3) / 3.0;

   }

   public String toString() {

       String s = "Sphere : radius = " + radius;

       return s;

   }

   public boolean equals(Object obj) {

       if (this == obj) {

           return true;

       }

       // if obj is not a instance of given class no need to check further

       if (!(obj instanceof Sphere)) {

           return false;

       }

       Sphere sp = (Sphere) obj;

       if (this.radius == sp.radius) {

           return true;

       }

       return false;

   }

}

RectangularPrism.java

public class RectangularPrism implements Shape3D {

private double length;

private double width;

private double height;

   public RectangularPrism() {

       length = 0;

       width = 0;

       height = 0;

   }

   public RectangularPrism(double l, double w, double h) {

       length = l;

       width = w;

       height = h;

   }

   public double getLength() {

       return length;

   }

   public double getWidth() {

       return width;

   }

   public double getHeight() {

       return height;

   }

   public double getArea() {

       return 2 * (width * length + height * length + height * width);

   }

   public double getVolume() {

       return length * length * height;

   }

   public String toString() {

       String s = "Rectangular Prism: length = " + length + ", width = " + width + ", height = " + height;

       return s;

   }

   public boolean equals(Object obj) {

       if (this == obj) {

           return true;

       }

       // if obj is not a instance of given class no need to check further

       if (!(obj instanceof RectangularPrism)) {

           return false;

       }

       RectangularPrism sp = (RectangularPrism) obj;

       if (this.length == sp.length && this.height == sp.height && this.width == sp.width) {

           return true;

       }

       return false;

   }

}

Cube.java

public class Cube extends RectangularPrism {

   public Cube() {

       super();

   }

public Cube(double size) {

       super(size, size, size);

   }

   public String toString() {

       String s = "Cube : size = " + getLength();

       return s;

   }

}

CircularShape.java

public abstract class CircularShape implements Shape3D {

private double radius;

   public CircularShape() {

       radius = 0;

   }

   public CircularShape(double r) {

       radius = r;

   }

   public double getDiameter() {

       return 2 * radius;

   }

   public double getRadius() {

       return radius;

   }

   public double getCrossSectionArea() {

       return Math.PI * Math.pow(radius, 2);

   }

   public double getCrossSectionPerimeter() {

       return 2 * Math.PI * radius;

   }

}

CircularShapeWithHeight.java

public abstract class CircularShapeWithHeight extends CircularShape {

private double height;

   public CircularShapeWithHeight() {

       super();

       height = 0;

   }

   public CircularShapeWithHeight(double radius, double height) {

       super(radius);

       this.height = height;

   }

   public double getHeight() {

       return height;

   }

}

Cylinder.java

public class Cylinder extends CircularShapeWithHeight {

   public Cylinder() {

       super();

   }

   public Cylinder(double radius, double height) {

       super(radius, height);

   }

   public double getArea() {

       return getCrossSectionPerimeter() * getHeight() + 2 * getCrossSectionArea();

   }

   public double getVolume() {

       return getCrossSectionArea() * getHeight();

   }

   public String toString() {

       String s = "Cylinder : radius = " + getRadius() + ", height = " + getHeight();

       return s;

   }

   public boolean equals(Object obj) {

       if (this == obj) {

           return true;

       }

       // if obj is not a instance of given class no need to check further

       if (!(obj instanceof Cylinder)) {

           return false;

       }

       Cylinder sp = (Cylinder) obj;

       if (this.getRadius() == sp.getRadius() && this.getHeight() == sp.getHeight()) {

           return true;

       }

       return false;

   }

}

CircularCone.java

public class CircularCone extends CircularShapeWithHeight {

   public CircularCone() {

       super();

   }

   public CircularCone(double radius, double height) {

       super(radius, height);

   }

   public double getArea() {

       double r = getRadius();

       double h = getHeight();

       return Math.PI * r * (r + Math.sqrt(r * r + h * h));

   }

   public double getVolume() {

       return getCrossSectionArea() * getHeight() / 3.0;

   }

   public String toString() {

       String s = "Circular Cone : radius = " + getRadius() + ", height = " + getHeight();

       return s;

   }

   public boolean equals(Object obj) {

       if (this == obj) {

           return true;

       }

       // if obj is not a instance of given class no need to check further

       if (!(obj instanceof CircularCone)) {

           return false;

       }

       CircularCone sp = (CircularCone) obj;

       if (this.getRadius() == sp.getRadius() && this.getHeight() == sp.getHeight()) {

           return true;

       }

       return false;

   }

}

There is a error in client class

replace statement

System.out.println("------------------------------------------------------------------" );

with

System.out.println("------------------------------------------------------------------ ");

Sample output

------------------------------------------------------------------

This is a square pyramid. Area = 504.02. Volume = 9126.67.

------------------------------------------------------------------

Output calling the method printInfo - polymorphism at work!

Square Pyramid: length = 37.0, height = 20.0

   Area = 504.02. Volume = 9126.67

------------------------------------------------------------------

------------------------------------------------------------------

This is a sphere. Area = 5026.55. Volume = 33510.32.

------------------------------------------------------------------

Output calling the method printInfo - polymorphism at work!

Sphere : radius = 20.0

   Area = 5026.55. Volume = 33510.32

------------------------------------------------------------------

------------------------------------------------------------------

This is a rectangular prism. Area = 2620.00. Volume = 3700.00.

------------------------------------------------------------------

Output calling the method printInfo - polymorphism at work!

Rectangular Prism: length = 10.0, width = 20.0, height = 37.0

   Area = 2620.00. Volume = 3700.00

------------------------------------------------------------------

------------------------------------------------------------------

This is a cube. Area = 600.00. Volume = 1000.00.

------------------------------------------------------------------

Output calling the method printInfo - polymorphism at work!

Cube : size = 10.0

   Area = 600.00. Volume = 1000.00

------------------------------------------------------------------

------------------------------------------------------------------

This is a cylinder. Area = 1884.96. Volume = 6283.19.

------------------------------------------------------------------

Output calling the method printInfo - polymorphism at work!

Cylinder : radius = 10.0, height = 20.0

   Area = 1884.96. Volume = 6283.19

------------------------------------------------------------------

------------------------------------------------------------------

This is a circular cone. Area = 1016.64. Volume = 2094.40.

------------------------------------------------------------------

Output calling the method printInfo - polymorphism at work!

Circular Cone : radius = 10.0, height = 20.0

   Area = 1016.64. Volume = 2094.40

------------------------------------------------------------------