- 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:
Extracted text: Shape 1: Circle (2D (Green, 7)) Updated color: Red Area = 153.938 I am a Circle shape with color changed to Red Shape 2: Triangle (2D (Red, 8, 7, 5)) Updated color: White Area = 17.321 I am a triangle shape with color changed to White Shape 3: Trapezoid (2D (Red, 5, 5, 7, 9), 2) Updated color: Green Arca = 10.00e I am a trapezoid with color changed to Green Shape 4: Tetrahedron (3D (Yellow, 3.371)) Surface area = 19.681 Volume = 4.514 Size reduced by 17.5%: Tetrahedron (3D (Yellow, 2.779)) Updated surface area = 13.379 Updated volume = 2.530 I am a tetraheron shape Four objects were generated and stored in an array list and you displayed the list. You can see Shapes 1 to 3 are 2D objects, their colors are changed during runtime (you must make sure that the color is really changed to a different color); Shape 4, a tetrahedron, its sizes, area, volume were reduced by 17.5 % (this percentage was randomly generated). Note that the list may be empty ...
Extracted text:
> A ShapeColor > > o Shape > -o For TwoD A Blue -o For ThreeD AYellow O+double perimeter) O+ double volume) O +void resize(double percentage) O+double area0 A Red O+ void recolor(ShapeColor sc) A Green A White > > > > > O ThreeD O TwoD #ShapeColor sc # ShapeColor sc # double a # int a # int b O+ ThreeDO O# int c + ThreeD(ShapeColor sc, double a) 1# int d O+ ThreeD(ThreeD td) O+ double getA0 O+ TwoDO O+ TwoD(ShapeColor sc, int a) O+void set(ShapeColor sc, double a) O+void resize(double percentage) O+ String toString0 O+ TwoD(ShapeColor sc, int a, int b) O+ TwoD(ShapeColor sc, int a, int b, int c) O+ TwoD(ShapeColor sc, int a, int b, int c, int d) O+ TwoD(TwoD td) 4. O +int getAO IS O +int getBO O +int getCO O + int getDO O Sphere O+ShapeColor getShapeColoro O+void set(ShapeColor sc, int a) is O+void set(ShapeColor sc, int a, int b) O + Sphere0 O+void set(ShapeColor sc, int a, int b, int c) O + Sphere(ShapeColor sc, double a) O+Sphere(S O+void set(ShapeColor sc, int a, int b, int c, int d) O Cube O+void recolor(ShapeColor sc) O+ double ar O+ String toString0 O duuble vu O+double ge o+Cube0 O+void set(S o+Cube(ShapeColor sc, double a) O+ String toS O + Cube(Cube c) O Tetrahedron O+ double area0 O+double volume O+ double getA) 0+ Tetrahedron) O+ Tetrahedron(ShapeColor sc, double a) is O Circle O+void set(Shapec O+ String toString0 +Tetrahedron(Tetrahedron t) O+ double area0 O + Circleo O+ double volume) O+ double getA) O+ Circle(ShapeColor sc, int radius) O+ Circle(Circle c) O Rectangle O+void set(ShapeColor sc, double a) O+ double area0 O+ double perimeter O + int getRadius) O+ String toString0 O+Rectangle0 O+void set(ShapeCo O +Rectangle(ShapeColor sc, int length, int width) O +Rectangle(Rectangle O + double area O+ String toString0 O Triangle O+ double perimeter) O+int getLength0 O +int getWidth0 O + Triangle) O+void set(ShapeColor +Triangle(ShapeColor sc, int a, int b, int c) O+ Triangle(Triangle t) O+double area O+double peri O+ String toString0 O Trapezoid O- int h O+int getAO O +int getB) O +int getCO O+void set(Sha o+Trapezoid(ShapeColor sc, int a, int b, int c, int d, int h) O+ String toStri o+Trapezoid(Trapezoid t) +Trapezoid0 O+ double area0 O+double perimeter) O +int getA0 O+ int getB) O +int getCO O+int getD0 O + int getHeight0 O +void set(ShapeColor sc, int a, int b, int c, int d, int h) O+ String toString0