0% found this document useful (0 votes)
192 views3 pages

OO Exercise 03

This document describes how to create a Smiley class with attributes for eyes, nose, and mouth and methods to get, set, and compare smiley objects. It then provides instructions to create a Main class with a main method that initializes happy and sad smiley objects using different constructors, sets their attributes to match other smileys from a table, and compares them using the equals method.

Uploaded by

david johnson
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views3 pages

OO Exercise 03

This document describes how to create a Smiley class with attributes for eyes, nose, and mouth and methods to get, set, and compare smiley objects. It then provides instructions to create a Main class with a main method that initializes happy and sad smiley objects using different constructors, sets their attributes to match other smileys from a table, and compares them using the equals method.

Uploaded by

david johnson
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2.

Create a Smiley class with the following attributes and methods


Smiley
-eyes: char
-nose: char
-mouth: char
+Smiley()
+Smiley(eyes: char, nose: char, mouth: char)
+getEyes(): char
+getNose(): char
+getMouth(): char
+setEyes(eyes: char): void
+setNose(nose: char): void
+setMouth(mouth: char): void
+equals(anotherSmiley: Smiley): boolean
+toString(): String

• Create the Main.java program:


public class Main
{ public static void main(String[ ] args)
{
}
}

• From the following smiley table, pick up four smileys, one has to be happy, another one
has to be sad.

:-) I’m happy (-: Australian :+) Big nose


:-( I’m sad/angry :-O I’m surprised 8-) Wears glasses
:-| I’m apathetic ;-) I’m winking :-P I’m naughty

• 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:

information about myRect before creating yourRect:


##############################
# Width: 4.0
# Height: 40.0
# Color: Blue
# Area: 160.0
# Perimeter: 88.0
##############################

information about myRect after creating yourRect:


##############################
# Width: 4.0
# Height: 40.0
# Color: Red
# Area: 160.0
# Perimeter: 88.0
##############################

information about yourRect:


##############################
# Width: 20.0
# Height: 5.0
# Color: Red
# Area: 100.0
# Perimeter: 50.0
##############################

You might also like