Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Practice java programming with inheritance and polymorphism. < < > For TwoD < >>> - Shape > ShapeColor A Green A White < >

 


imageimageimageimageimageimageimageimage

Practice java programming with inheritance and polymorphism. < < > For TwoD < >>> - Shape > ShapeColor A Green A White < > simplements < > implements < >> TwoD < >> ThreeD A quick look to the UML diagram: Circle Rectangle Cube Triangle Tetrahedron Trapezoid Sphere 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 (Cuble, Trapezoid and Cube) 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") **enumeration>>>> ShapeColor Blue Yellow Red Green White +Circle( Circle +double area + double perimeter + int getRadius() +Circle(ShapeColor sc, int radius) +Circle(Circle c) + void set(Shape + String to String( +double perimeter +void recolor(ShapeColor sc) < > ForTwoD > 1 Fint a int b intc int d **TWODO ShapeColor sc J +int getA *Rectangle( TwoD(ShapeColor sc, inta) +TwoD(ShapeColor sc, int a, int b) +TwoD(ShapeColor sc, inta, int b, int c) +TwoD(ShapeColor sc, int a, int b, int c, int d) +TwoD(TwoD td) < TwoD inget() int getCo +int getD0 +ShapeColor getShapeColor +void set(ShapeColor sc, inta) void set(ShapeColor sc, int a, int b) +void set(ShapeColor sc, int a, int b, int c) + void set(ShapeColor sc, inta, int b, int c, int d) +void recolor(ShapeColor sc) +String toString0 Rectangle < < > + double perimeter() +int getLength() + int get Width() *void set(ShapeColor +String toString0 Rectangle(ShapeColor sc, int length, nt width) +Rectangle(Rectangle,A +double area( Triangle -inth 15 +Triangle( Triangle(ShapeColor sc, int a, Intb, int c) +Triangle(Triangle t +double area +double peri + int getA() +int getB() +int getCo +double perimeter + int getA() < > Shape +double area String toStri+Trapezoid(Trapezoid t) + double area( +int getB0 +int getCO +int getDO +int getHeighto Trapezoid 3 < > +Sphere0 ShapeColor sc #double a Sphere +Trapezoid( void set(Sha+Trapezoid(ShapeColor sc, int a, int b, int c, int d, int h) +double volume +void resize(double percentage) +ThreeDO +ThreeD(ShapeColor sc, double a) + ThreeD(ThreeD td) double Yo double ge+Cube < > For ThreeD < 1 +Sphere(ShapeColor sc, double a) +Sphere (S Cube +double and +void set(ShapeColor sc, inta, int b, int c, int d, int h) +String to String( void set(+Cube (ShapeColor sc, double a) String tos Cube(Cube c) double area double volumed +double getAO +void set(Shaped +String toString0| Tetrahedron *Tetrahedron( Tetrahedron(ShapeColor sc, double a) Tetrahedron(Tetrahedront) +double area +double volume +double getA() *void set(ShapeColor sc, double a) String toString0 Wow... so difficult to see; no worry, I will break it down bit by bit and explain what you must do ... Let us explore the above UML diagram at the highest hierarchy; you can see we have an enum class and three interfaces: < >> ShapeColor Blue Yellow Red A Green White < > For TwoD +double perimeter() +void recolor(ShapeColor sc) 1 #int c #int d #ShapeColor sc #int al #int b + TwoD0 < > * Shape + double area( < TwoD I I 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 R Let us now explore the abstract class TwoD which implements the Shape and the ForTwoD interfaces: 4 V +TwoD(ShapeColor sc, int a) +TwoD(ShapeColor sc, int a, int b) +TwoD(ShapeColor sc, int a, int b, int c) +TwoD(ShapeColor sc, int a, int b, int c, int d) +TwoD(TwoD td) +int getA() > +int getB() + int getC0 +int getD(0) + ShapeColor getShapeColor( +void set(ShapeColor Sc, int a) +void set(ShapeColor sc, int a, int b) + void set(ShapeColor sc, int a, int b, int c) < > For ThreeD +void set(ShapeColor sc, int a, int b, int c, int d) +void recolor(ShapeColor sc) +String to String0 +double volume() +void resize(double percentage) 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. TRAPEZOID The following UML diagram shows the four concrete subclasses of TwoD: Circle + Circle ( +Circle(ShapeColor sc, int radius) +Circle(Circle c) + double area() + double perimeter() + int getRadius()) +void set(ShapeColor sc, int radius) + String toString( 5 +Rectangle( Rectangle +Rectangle(ShapeColor sc, int length, int width) +Rectangle(Rectangle r) + double area() + double perimeter() +int getLength() +int getWidth()) +void set(ShapeColor sc, int length, int width) O + String to String( Triangle +Triangle( +Triangle (ShapeColor sc, int a, int b, int c) +Triangle(Triangle t) + double area() + double perimeter() +int getAO + int getB0 + int getC0 +void set(ShapeColor sc, int a, int b, int c) + String to String0 -int h +Trapezoid() #ShapeColor sc #double a +Trapezoid(ShapeColor sc, int a, int b, int c, int d, int h) +Trapezoid(Trapezoid t) + double area() + double perimeter() +int getA() O +int getB( + ThreeDO + int getCo + int getD0 Some methods just override the super class methods (same implementations) Information defined in each of the subclasses should be obvious in definitions. Trapezoid +int getHeight() +void set(ShapeColor sc, int a, int b, int c, int d, int h) + String to String( 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. Next, we explore the abstract class ThreeD which implements the Shape and ForThreeD interfaces: < > ThreeD +ThreeD(ShapeColor sc, double a) +ThreeD(ThreeD td) + double getA() +void set(ShapeColor sc, double a) +void resize(double percentage) +String to String( 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 For ThreeD interface class. The method resize is to reduce the size by certain percentage. The following UML diagram shows the three concrete subclasses of ThreeD: +Cube() Cube +Cube(ShapeColor sc, double a) +Cube(Cube c) +double area() + double volume() +double getA0 +void set(ShapeColor sc, double a) + String toString() Tetrahedron +Tetrahedron() +Tetrahedron (ShapeColor sc, double a) +Tetrahedron (Tetrahedront) + double area() + double volume() +double getA( +void set(ShapeColor sc, double a) +String to String( Sphere 7 +Sphere () +Sphere (ShapeColor sc, double a) +Sphere (Sphere s) + double area( + double volume() +double getA( +void set(ShapeColor sc, double a) + String to String() 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 All shapes (2D or 3D) should be randomly generated and store them in an ArrayList of Shape' s. A2 S1_2024 -static int getint -static double getDouble() -static ShapeColor getColor -static boolean is Triangle(int a, int b. int c), -static TwoD getTwoD() -static ThreeD getThreeD() -static void process2DShape(Shape ss) - static void process3DShape(Shape ss) -static void displayList(ArrayList > alist) + static void main(String args) 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: 8 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 Area 10.000 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 ... IMPORTANT Put all your classes in a file called YourName_A2.java and make sure that this file can be compiled and can be executed. Upload ONLY this file to Moodle. ALL ZIP FILE SUBMISSION WILL BE REJECTED. No re-submission will be allowed after grading. In the above file, remember to put down your name and the following declaration (some similar contents): 9

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Genetics Analysis And Principles

Authors: Robert Brooker

5th Edition

978-0073525341, 0073525340

More Books

Students also viewed these Algorithms questions

Question

What major factors influence the cost of home insurance?

Answered: 1 week ago