-
Notifications
You must be signed in to change notification settings - Fork 0
10 13_Shapes_Easy
Implement the Shape hierarchy shown below. Each TwoDimensionalShape
should contain method getArea
to calculates the area of the two-dimensional shape. Each ThreeDimensionalShape
should have methods getArea
and getVolume
to calculate the surface area and volume, respectively, of the three-dimensional shape. Create a program that uses an array of Shape references to objects of each concrete class in the hierarchy. The program should print a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape
or a ThreeDimensionalShape
. If it's a TwoDimensionalShape
, display its area. If it's a ThreeDimensionalShape
, display its area and volume.
To see the values of random shapes and sizes with their calculated area and/or volume run the program. To see new values simply run the program again.
The program is static in the fact that no user input is required and the output is fixed. In the main class MyShapes
the main method does all the heavy lifting. An array with one of each Shape
is defined. Each shape is initialized with random values afterwards the shapes are printed to the screen. The shape prints its name and if the shape is an instance of TwoDimensionalShape
then just the area is printed. However if the shape is an instance of ThreeDimensionalShape
then the surface area and volume are also printed. To have the shapes always have the same dimensions manually enter the dimensions of each shape that were initialized in the array.
UML Diagram:
The java documents are served from a local web server on this machine. To start the web server, navigate to the directory immediately above where the source code is checked out (i.e. ~/git ) and then use "python -m SimpleHTTPServer" in that directory.
cd ~/git
python -m SimpleHTTPServer&
Note: if you are running python 3 (which you can check via opening a terminal and typing: python --version), then the command is:
python3 -m http.server