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

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

ID: 3677035 • Letter: U

Question

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

Shape3D.java

package org.students;

public interface Shape3D
{
public double getArea();
public double getVolume();
public String toString();
public boolean equals(Object obj);
}

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

CircularCone.java

package org.students;

public class CircularCone extends CircularShapeWithHeight {
private double height,radius;
   public CircularCone(double radius, double height) {
       super(radius, height);
       this.radius=radius;
       this.height=height;
   }

   @Override
   public double getArea() {
   double r = getRadius();
   double h = getHeight();
   return Math.PI * r * (r + Math.sqrt(r * r + h * h));
   }
   public double getVolume() {
   return Math.PI*getRadius()*getRadius() * getHeight() / 3.0;
   }

   @Override
   public String toString() {
       return "CircularCone [height=" + height + ", radius=" + radius + "]";
   }


  


}

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

CircularShape.java

package org.students;

public abstract class CircularShape implements Shape3D
{
   private double radius;
     
   public CircularShape() {
  
   }
  
   public CircularShape(double r) {
   this.radius=radius;
   }
   public double getCrossSectionArea() {
   return Math.PI * Math.pow(radius, 2);
   }
  
   public double getCrossSectionPerimeter() {
   return 2 * Math.PI * radius;
   }

       @Override
       public boolean equals(Object obj) {
           if (this == obj)
               return true;
           if (obj == null)
               return false;
           if (getClass() != obj.getClass())
               return false;
           CircularShape other = (CircularShape) obj;
           if (Double.doubleToLongBits(radius) != Double
                   .doubleToLongBits(other.radius))
               return false;
           return true;
       }
  
}

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

CircularShapeWithHeight.java

package org.students;

public abstract class CircularShapeWithHeight extends CircularShape
{
   private double height;
   private double radius;

   public CircularShapeWithHeight(double radius,double height) {
      
       this.height = height;
       this.radius=radius;
   }

   public double getHeight() {
       return height;
   }

   public double getRadius() {
       return radius;
   }

   @Override
   public String toString() {
       return "CircularShapeWithHeight [height=" + height + ", radius="
               + radius + "]";
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       CircularShapeWithHeight other = (CircularShapeWithHeight) obj;
       if (Double.doubleToLongBits(height) != Double
               .doubleToLongBits(other.height))
           return false;
       if (Double.doubleToLongBits(radius) != Double
               .doubleToLongBits(other.radius))
           return false;
       return true;
   }

  


     

}

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

RectangularPrism.java

package org.students;

public class RectangularPrism implements Shape3D {

   private double length;
   private double width;
   private double height;
  
   public RectangularPrism() {
          
       }

   public RectangularPrism(double length, double width, double height) {
          
           this.length = length;
           this.width = width;
           this.height = height;
       }

   public double getLength() {
       return length;
   }

  

   public double getWidth() {
       return width;
   }


   public double getHeight() {
       return height;
   }

  

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

   @Override
   public double getVolume() {
       return length * length * height;
   }

   @Override
   public String toString() {
       return "RectangularPrism :length=" + length + ", width=" + width
               + ", height=" + height + "";
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       RectangularPrism other = (RectangularPrism) obj;
       if (Double.doubleToLongBits(height) != Double
               .doubleToLongBits(other.height))
           return false;
       if (Double.doubleToLongBits(length) != Double
               .doubleToLongBits(other.length))
           return false;
       if (Double.doubleToLongBits(width) != Double
               .doubleToLongBits(other.width))
           return false;
       return true;
   }

}

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

Sphere.java

package org.students;

public class Sphere implements Shape3D
{
   private double radius;
   public Sphere() {
  
   }
  
   public Sphere(double r) {
   this.radius=r;
   }
   @Override
   public double getArea() {
       return 4.0 * Math.PI * radius * radius;
      
   }

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

   public double getRadius() {
       return radius;
   }

   @Override
   public String toString() {
       return "Sphere :radius=" + radius + "";
   }


   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Sphere other = (Sphere) obj;
       if (Double.doubleToLongBits(radius) != Double
               .doubleToLongBits(other.radius))
           return false;
       return true;
   }
  

  
  

}

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

SquarePyramid.java

package org.students;

public class SquarePyramid implements Shape3D
{

   private double length;
   private double height;
  
   public SquarePyramid() {
   length = 0;
   height = 0;
   }
   public SquarePyramid(double l, double h) {
   this.length=l;
   this.height=h;
   }
  
   public double getLength() {
           return length;
       }
      
       public double getHeight() {
           return height;
       }
      
   @Override
   public double getArea() {
      
       return ((length / 2.0) * Math.sqrt((length *length) / 4.0 + height * height));
   }

   @Override
   public double getVolume() {
      
       return length * length * height / 3.0;
   }
   @Override
   public String toString() {
       return "SquarePyramid :length=" + length + ", height=" + height
               + "";
   }
   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       long temp;
       temp = Double.doubleToLongBits(height);
       result = prime * result + (int) (temp ^ (temp >>> 32));
       temp = Double.doubleToLongBits(length);
       result = prime * result + (int) (temp ^ (temp >>> 32));
       return result;
   }
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       SquarePyramid other = (SquarePyramid) obj;
       if (Double.doubleToLongBits(height) != Double
               .doubleToLongBits(other.height))
           return false;
       if (Double.doubleToLongBits(length) != Double
               .doubleToLongBits(other.length))
           return false;
       return true;
   }
  

}

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

Cube.java

package org.students;

public class Cube extends RectangularPrism
{
   private double size;
  
   public Cube() {
     
   }
  
   public Cube(double size) {
   this.size=size;
   }
  
   public double getArea(double size)
   {
       return 6*(size*size);
   }
  
   public double getVolume(double size)
   {
       return (size*size*size);
   }

       @Override
       public String toString() {
           return "Cube [size=" + size + "]";
       }

  
       @Override
       public boolean equals(Object obj) {
           if (this == obj)
               return true;
           if (!super.equals(obj))
               return false;
           if (getClass() != obj.getClass())
               return false;
           Cube other = (Cube) obj;
           if (Double.doubleToLongBits(size) != Double
                   .doubleToLongBits(other.size))
               return false;
           return true;
       }
  
}

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

Cylinder.java

package org.students;
import org.students.*;

public class Cylinder extends CircularShapeWithHeight {

   private double radius, height;

  
   public Cylinder(double radius, double height) {
       super(radius, height);
  
   }

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

   @Override
   public double getVolume() {
   return getCrossSectionArea() * getHeight();
   }

   @Override
   public String toString() {
       return "Cylinder []";
   }
  

}

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

Shape3D_Client.java

package org.students;

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());
       }
   }

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote