Objectives:

Practice java programming with inheritance and polymorphism.

Task

Implement the Shape hierarchy shown in Fig. 1 (Class name in italic indicates an abstract class).

Fig 1: Inheritance heirarchy of Shapes: see image.

- Each TwoDimensionalShape should contain methods area and getArea to calculate and to return the area of the two-dimensional shape.

- Each ThreeDimensionalShape should have methods area, getArea, volume and getVolume to calculate and to return the surface area and the volume, respectively, of the three-dimensional shape.

- Try to surf the internet to look for some formulas, for example, to compute the areas, the surface areas and the volumes I did that too

- The following UML diagram show briefly their relationship: see image.

A more detailed UML diagram: (Note that the #s before the instance variables and the methods names mean protected) see image.

Let us explore the above UML diagram. At the highest hierarchy, you can see that Shape is an interface class. Two abstract classes TwoD and ThreeD will need to implement the Shape interface see image.

The UML diagram TestShape between the TwoD and the ThreeD will be the main class that has a main method. We will discuss it later.

Three possible shapes for TwoD: one value, for example the radius, is a circle shape; two values, for example the length and the width, is a rectangle; and three values for example the three sides of a triangle (provided it can be formed). In this class, you can see that we have three constructors to describe the three shapes, a copy constructor and a toString method.

Note that accessor and the mutator methods did not show in the diagram, you should add them in.

The following UML diagram shows the three subclasses of TwoD: see image.

Three possible shapes for ThreeD: Just one value can determine the shapes of a sphere, a cube and or a tetrahedron. In this class, we only have one constructor, a copy constructor, and a toString method. Again the accessor and mutator methods are missing the diagrams; you should add them in too. For a 3D object, we can compute and return the volume too. Therefore we have two additional abstract methods in this abstract class ThreeD. Let us now look at the UML diagram for the three subclasses of ThreeD class: see image.

Look for the surface area and volume formulas somewhere in internet to compute and to return their values

Let us now explore the main class, i.e. main method is defined in this class see image.

All shapes (2D or 3D) should be randomly generated and store them in an ArrayList of Shape.

You can see a few private class methods defined in this class (TestShape):

  • a method generates and returns a single positive integer
  • a method generates and returns a TwoD shape
  • a method generates and returns a ThreeD shape
  • a method to display the objects stored in the ArrayList: see image.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.