Practice java programming with inheritance and polymorphism. < < > For TwoD < >>> - Shape...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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 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
Expert Answer:
Related Book For
Posted Date:
Students also viewed these algorithms questions
-
The lease agreement and related facts indicate the following: a. Leased equipment had a retail cash selling price of $300,000. Its useful life was five years with no residual value. b. The lease term...
-
How does scientific inquiry try to avoid the common pitfalls that produce error in nonscientific inquiry?
-
Consider a pulsar, a collapsed star of extremely high density, with a mass M equal to that of the Sun (1.98 x l030 kg), a radius R of only 12 km, and a rotational period T of 0.041s. By what...
-
Internal energy of a perfect gas depends upon: (a) Temperature only (b) Temperature and pressure (c) Temperature, pressure and specific heats (d) None of these
-
The financial statements of Tootsie Roll are presented in Appendix A at the end of this book. Instructions (a) Using the consolidated income statement and balance sheet, identify items that may...
-
In March 2022, Ginger Graham, age 46 and wife of Greg Graham, engaged in the transactions described below. Determine Gingers gift tax liability for 2022 if she and Greg elect gift splitting and Greg...
-
C1. Assume a cache of 1 MB organized as 32 bytes each line. The main memory is 256 MB. [6 marks] a. Determine the number of blocks in main memory [1.5 marks] b. Determine how to split the address...
-
In late 2007, some observers were concerned that the U.S economy was experiencing a dangerous combination or reduced output growth (resulting in increased unemployment) and higher inflation, as had...
-
Q TR TC MR MC 0 0 3 - - 1 6 5 6 2 2 12 8 3 18 12 4 24 17 5 30 23 6 36 30 7 42 38 8 48 47 TO CALCULATE MAXIMAZING LEVEL OF OUTPUT AND THEN SKETCH A GRAPH TO REPRESENT MR AND MC DATA
-
4 5 Miso Corp. borrowed $575,000 from the bank on August 1, 2024. The note had an 4 percent annual rate of interest and matured on April 30, 2025. Interest and principal were paid in cash on the...
-
In general, should a country's policymakers be concerned if the country has trade imbalances with other countries? If your answer ispolicymakers "no", are there situations in which policy makers...
-
It is argued that decisions made in relatively risk-free environments are not optimal or not efficient. If this assertion is true, can we declare that the decision-making environment constrained by...
-
Ashanti Partnership, a mining equipment business, states in its partnership agreement that Jonah and Tutu agree to split profits and losses according to a 40/60 ratio. Additionally, the partnership...
-
The Higher the time period of the financial security the higher the. ............... risk. O a. Maturity O b. Default and Maturity Oc. Default O d. Liquidity
-
In a cotransformation experiment (see solved problem S4), DNA was isolated from a donor strain that was proA+ and srrC+ and sensitive to tetracycline. (The proA and strC genes confer the ability to...
-
Describe the important features of cloning vectors. Explain the purpose of selectable marker genes in cloning experiments.
-
When applying a chi square approach in a linkage problem, explain why an independent assortment hypothesis is used.
-
You manage a small ice cream shop in which your employees scoop the ice cream by hand. Each night, you total your sales and the total volume of ice cream sold. You find that on nights when an...
-
At the Web sites of the American Lung Association and the U.S. Environmental Protection Agency, find statistical data concerning the health effects of secondhand (passive) smoke. Write a short...
-
Find two examples of data distributions in recent news reports; they may be given either as tables or as graphs. In each case, state the range of the distribution and explain its meaning in the...
Study smarter with the SolutionInn App