Three interfaces ForTwoD, Shape and ForThreeD - TwoD class implements ForTwoD and Shape interfaces - ThreeD class implements ForThreeD and Shape interfaces - Four subclasses to TwoD (Circle, Rectangle, Triangle and Trapezoid) - Three subclasses to ThreeD (Cube, Tetrahedron, Sphere) - Implement the Shape hierarchy as shown in the above diagram - Basically, each two-dimensional shape needs to compute its area and its perimeter.
- Three interfaces ForTwoD, Shape and ForThreeD
- TwoD class implements ForTwoD and Shape interfaces
- ThreeD class implements ForThreeD and Shape interfaces
- Four subclasses to TwoD (Circle, Rectangle, Triangle and Trapezoid)
- Three subclasses to ThreeD (Cube, Tetrahedron, Sphere)
-
Implement the Shape hierarchy as shown in the above diagram
- Basically, each two-dimensional shape needs to compute its
area and its perimeter.
- Each three-dimensional shape needs to compute its area
(also known as surface area) and its volume.
- 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
A more detailed UML diagram is shown as follow: (Note that the #’s before
the instance variables and the methods’ names mean “protected”)
- Enum class defines some enum constants (various colors, feel free to
change)
- The main interface should be the Shape interface consists of only one
abstract method, the area method. You can put some useful constant
in this interface, for example the PI.
- The interface ForTwoD consists of two abstract methods
- The interface OnyThreeD consists of two abstract methods
Four 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; three
values for example the three sides of a triangle (provided it can be formed),
five values for trapezoid (one additional value was the height, as we need
that to compute the area). In this class, you can see that we have four
important constructors to describe the four shapes, a default constructor, a
copy constructor, some accessor and mutator methods and a toString
method. Each 2D shape also has a color and the color can be changed
(recolor method) during runtime. You can see we also define an enumeration
type to specify the color of the shapes.
Some methods just override the super class methods (same implementations)
Information defined in each of the subclasses should be obvious in
definitions.
Note that in the Trapezoid class, the sides c and d will not be used in the
computation, You can assume that a and b are the two parallel sides; and the
height is the instance variable h defined in the Trapezoid class.
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 also have
constructors, copy constructor, accessor methods, mutator methods and a
toString method. For a 3D object, we can compute and return the volume
too.
Note that ThreeD class also implements ForThreeD interface class. The
method resize is to reduce the size by certain percentage.
Look for the surface area and volume formulas somewhere in internet to
compute and to return their values
All shapes (2D or 3D) should be randomly generated and store them in an
ArrayList of Shape’s.
You can see a few private class methods are defined in this class:
- a method generates and returns a positive integer, not too large
- a method generates and returns a positive real number, not too large
- a method generates and returns a color.
- a method to test if three sides can form a triangle.
- a method generates and returns a TwoD shape
- a method generates and returns a ThreeD shape
- the two process methods of 2D and 3D can be called in the displayList
(optional)
- a method to display the objects stored in the ArrayList. Note that in
the display method, you display the details of each shape object, i.e.,
the toString methods for each of the classes only display a “brief”
object info, display of area / volume / resizable/ recolor should be
done in this method.
Convenient to your design, minor updates to methods or additional methods
are allowed. The name of classes / methods in the UML diagram cannot be
changed
In the main method, you repeatedly generate an integer k (0 or 1 or 2). If k
is 1 you construct a 2D object; if k is 2, you construct a 3D object and k is 0,
you end the task. The following shows one of the possible displays:
Step by step
Solved in 2 steps