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;

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter11: Advanced Inheritance Concepts
Section: Chapter Questions
Problem 5PE
icon
Related questions
Question

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;

 

 

 

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

 

                      }

 

               }

 

       }

 

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 13 images

Blurred answer
Knowledge Booster
Data members
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT