OO Exercise 03
OO Exercise 03
• From the following smiley table, pick up four smileys, one has to be happy, another one
has to be sad.
• Test the Smiley class by finishing the main() method: creating happy and sad by calling
the appropriate constructors.
• Using set methods to change happy and sad’s attributes around so that they become the
other two smileys you picked.
• calling equals() method to compare happy with sad. If they are equal, print out “happy
and sad are equal”, in case they are not equal, print out “happy and sad are not equal.”
Class Variables and Methods
1. Create a Slogan class to represent slogans. The UML diagram for the class is the following:
Use a static variable count to count the objects created from this class.
Slogan
-phrase: String
-count: int
+Slogan()
+Slogan(phrase: String)
+getPhrase(): String
+getCount(): int
+setPhrase (phrase: String): void
+toString():String
Please create SloganTest.java to test the Slogan class. In the main() method, create 5 Slogan
objects: slogan01, slogan02…slogan05. The phrase of slogan01 is “Remember the Alamo”; “The
phrase of slogan02 is “Don’t Worry. Be Happy”; The phrase of slogan03 is “Live Free or Die”;
The phrase of slogan04 is “Talk is Cheap”; The phrase of slogan05 is “Write Once, Run
Anywhere”. Print out the information of all slogan objects and the count information of Slogan
class. The output should looks like the following:
--------------------------------------------------
Slogan: Remember the Alamo.
--------------------------------------------------
--------------------------------------------------
Slogan: Don't Worry. Be Happy.
--------------------------------------------------
--------------------------------------------------
Slogan: Live Free or Die.
--------------------------------------------------
--------------------------------------------------
Slogan: Talk is Cheap.
--------------------------------------------------
--------------------------------------------------
Slogan: Write Once, Run Anywhere.
--------------------------------------------------
Slogans Created: 5
2. Create a Rectangle class to represent rectangles. The UML diagram for the class is the
following: Suppose that all the rectangles are the same color. Use a static variable for color.
Rectangle
-width: double
-height: double
-color: String
+Rectangle()
+Rectangle(width: double, height: double, color: String)
+getWidth(): double
+getHeight(): double
+getColor(): String
+setWidth (width: double): void
+setHeight (height: double): void
+setColor (color: String): void
+findArea(): double
+findPerimeter(): double
+toString():String
Please create Main.java to test the Rectangle class. In the main() method, create the first
Rectangle object named myRect. Assign width 4, height 40 and color blue to myRect. Print out
the information about myRect. Create the second Rectangle object named yourRect. Assign
width 20, height 5 and color red to yourRect. Print out the information of both myRect and
yourRect. The output should looks like the following: