Question: Java Programming Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube,
Java Programming
Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. The cube should inherit from Rectangular Prism. The two-dimensional shapes should include methods to calculate Area. The three-dimensional shapes should include methods to calculate surface area and volume. Use as few methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which level of the hierarchy and what can be shared. Use the following to test your code:
import java.util.ArrayList;
public class ShapeTest {
public static void main(String[] args) {
ArrayList
shapes.add(new Square(10));
shapes.add(new Circle(10));
shapes.add(new Cube(10));
shapes.add(new RectangularPrism(10, 20, 30));
shapes.add(new Sphere(10));
for(Shape s : shapes) {
System.out.println(s);
System.out.println("Area: " + s.calculateArea());
if (s instanceof ThreeDimensionalShape) {
ThreeDimensionalShape threed = (ThreeDimensionalShape) s;
System.out.println("Volume: " + threed.calculateVolume());
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
