Java Programming 03 - Create the 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. 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 little 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;
Java
03 - Create the 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. 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 little 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<Shape> shapes = new ArrayList<Shape>();
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());
}
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 13 images