Sie sind auf Seite 1von 5

An introduction to Object-Oriented Concepts*

This is an exercise that I use it in the class to illustrate some of the OO terms and
concepts such as class, object, method, operation, attribute, message, polymorphism,
among other terms and concepts.
Dr. Alshare

You are a Blackboard (this is a class)

[Your data is the drawing space in front of you. Do not draw outside the lines.], this is an
attribute
When you are asked to DrawSquare ( ), do so – anywhere within the confines of
your drawing space.

When you are asked to DrawCircle ( ), do so – anywhere within the confines of


your drawing space.

When you are asked to DrawText (phrase), you will also be given a word (or
phrase). Draw it somewhere within the confines of your drawing space.

When you are asked to Clear ( ), erase your drawing space. (Leave the boundary
lines)
Explanations:
1. A Blackboard is a class which has four operations or methods (even though the
textbook defines a method as part of an operation, but for simplicity we can use
both terms interchangeably) and the drawing space represents dimensions for
attributes.
2. If we assign (create) an object(s) from that class for example we might create
(assign role of blackboard to Leah and Kevin). Now Leah and Kevin are objects
or instances of a class called Blackboard.
3. One of the four methods (DrawSquare ( ), DrawCircle ( ), DrawText (phrase),
Clear ( )) needs to receive a value for its parameter (phrase), thus when we ask or
pass a message to that object (Leah) to DrawText, then we must provide that
object with the value for that parameter either one word or phrase. So, the method
DrawText (phrase) has one parameter and its type of data is text.
Examples:
1. “Kevin, DrawSquare ( )” ==> Kevin should draw a square within his drawing
space.
2. “Kevin, DrawCircle ( )” == > Kevin should draw a circle within his drawing
space.
3. “Leah, DrawText (Hi class!)” ==> Leah should draw the phrase (Hi class!)
within his drawing space.
4. “Kevin, Clear ( )” ==> Kevin should erase only his own area of blackboard.
5. “Kevin, DrawText” ==> Kevin should reply that he doesn’t know how since the
command is missing the parameter (phrase).

*. The primary developer of this Role Play exercise is Dr. David Levine.
You are a Bamboozler (this is a class)
1
When you are asked to Add (Num1, Num2), you will be given two numbers.
Give back their sum. Do so verbally. Note that you can only add two numbers. You
cannot add three or more numbers.

When you are asked to Subtract (Num1, Num2), you will be given two numbers.
DO NOT give back their difference. Instead double the first number and then give back
the difference of the two. (Thus, if you were asked to subtract 6 and 4, you would get 8.)
Do so verbally.
Explanations:
1. The class Bamboozler has two operations or methods (Add and Subtract) both
methods have two parameters (num1 and num2) which have numerical data type.
2. Examine the way we define the Subtract method and compare it with the same
method (Subtract) for Calculator class which will be defined later.
Examples: Assume we have created an object (Mike) from the Bamboozler class.
1. “Mike, Add (10, 4)” ==> Mike should reply verbally “14”.
2. “Mike, Subtract (10, 4)” ==> Mike should reply verbally “16”.

You are an Acrobat (this is a class)

When you are asked to Clap (N), you will be given a number. Clap your hands
that many times.

When you are asked to KneeBend (N), you will be given a number. Stand up and
sit down that many times. Note that if you are told “2”, then you will stand up twice
AND sit down twice

When you are asked to Roll ( ), lie down on the floor and roll over once. Stand up
when finished.

When you are asked to Count ( ), you will reply (verbally) with the total number
of exercises you have done. Note that Clap-ping four times counts as four exercises, and
KneeBend-ing twice counts as two. If you have done these things (and only these things)
then your reply should be “6”.
Explanation:
Examples: Let us assume that we have created two objects from the Acrobat class
(Mary and Luke). Sending the following messages
1. “Mary, Clap (3)” ==> Mary should clap her hands 3 times (no values are
return from the object).
2. “Mary, KneeBend (2)” ==> Mary should stand up and sit 2 times.
3. “Mary, Count ( )” ==> Mary should reply verbally “5” (the object returns
value in this case the number 5).
4. “Luke, Kneebend (5)” ==> Luke should stand up and sit 5 times.
5. “Luke, Clap (4)” ==> Luke should clap his hands 4 times.
6. “Luke, Count( )” ==> Luke should reply verbally “9”
7. “Mary, Clap (2)” ==> Mary should clap her hands 2 times

2
8. “Mary, Count ( )” ==> Mary should reply verbally “7”
9. “Luke, TripleBackFlip (2)” ==> Luke should reply that he doesn’t know how
since TripleBackFlip is not a capability of an Acrobat.
10. “Luke, Clap ( )” ==> Luke should reply that he doesn’t know how since the
proper Clap command must include a number.
11. “Acrobat, Clap (4)” ==> this command should not result in any action since
commands must be directed at objects not at the class.
12. “Mary, Roll (2)” ==> Mary should reply that she doesn’t know how since the
proper Roll ( ) command should not have a number.
13. “Luke, Roll ( )” ==> Luke should roll one time.

You are a Dice (this is a class)

Your capabilities are:

When you are asked to Roll ( ), you should reply with a “random” whole number
between 1 and 6. (Just make up the number.) Respond verbally.

When you are asked NumRolls ( ), simply reply with the number of times you
have been asked to Roll. Respond verbally.

When you are asked to Reset ( ), change your “internal” counter to zero, i.e.
pretend that all of the Roll-s in the past never happened.
Explanation:
1. Notice the definition of Roll ( ) method for Acrobat class and the definition for
Roll ( ) method for Dice class. This is what we mean by “polymorphism” when
you use the same method (operation) but each object from different classes reacts
or behaves differently based on what we have stored as a procedure (instructions)
for that method.
Examples: Let us assume that we have created one object from the Dice class.
1. “Joe, Roll ( )” ==> Joe should reply with any whole number between 1 and 6.
2. “Joe, Roll ( )” ==> Joe should reply with any whole number between 1 and 6.
3. “Joe, NumRolls ( )” ==> Joe should respond, “2”
4. “Joe, Roll ( )” ==> Joe should reply with any whole number between 1 and 6.
5. “Joe, NumRolls ( )” ==> Joe should respond, “3”
6. “Joe, Reset ( )” ==> Joe should change “internal” counter to zero.
7. “Joe, NumRolls ( )” ==> Joe should respond, “0”
8. “Joe, Roll ( )” ==> Joe should reply with any whole number between 1 and 6.
9. “Joe, NumRolls ( )” ==> Joe should respond, “1”
14. “Joe, Roll (3)” ==> Joe should reply that he doesn’t know how since the proper
Roll ( ) command should not have a number.
15. “Joe, Roll ( )” ==> Joe should reply with any whole number between 1 and 6.
16. “Mary, Roll ( )” ==> Mary should roll one time.

You are a Calculator (this is a class)

3
When you are asked to Add (Num1, Num2), you will be given two numbers.
Reply (verbally) with their sum. Note that you can only add two numbers – not just one
(or none) and not three or more.

When you are asked to Subtract (Num1, Num2), you will be given two numbers.
Reply (verbally) with their difference. Note that you can only subtract two numbers – not
just one (or none) and not three or more.

When you are asked to Multiply (Num1, Num2), you will be given two numbers.
Reply (verbally) with their product. Note that you can only multiply two numbers – not
just one (or none) and not three or more.
Examples: assume we have created an object (Adam) from the Calculator class.
1. “Adam, Add (3, 5)” ==> Adam should reply verbally with “8”.
2. “Adam, Subtract (10, 4)” ==> Adam should reply verbally with “6”.
3. “Adam, Multiply (5, 3)” ==> Adam should reply verbally with “15”.

You are a LazyCalculator (this is a class)

Your capabilities are:

When you are asked to Add (Num1, Num2), you may be given two numbers.
Find a calculator and ask the calculator to add them. Then reply verbally (to your “boss”)
with the sum.

ALTERNATIVELY,

When you are asked to Add (Num1, Num2, Num3, Num4), you may be given
four numbers. In this case, find a calculator – get it to add the first two; then get it to add
the last two; then get it to add those two sums. Reply (verbally) to your “boss” with the
sum.
Explanations:
Any object of this class will communicate with another object from the Calculator class
by sending them messages to do the calculation and then return values to the boss. It has
two methods Add (Num1, Num2) and Add (Num1, Num2, Num3, Num4) but they have
different number of parameters. The LazyCalculator determines which behavior to use
based on the number of parameters passed. ff
Examples: Assume we have created an object (Krista)
1. “Krista, Add (10, 4)” ==> Krista should send a message for Adam and pass the
two numbers (10, 4) and ask him to calculate the sum and then reply with answer
to the boss.
2. “Krista, Add (2, 4, 6, 8)” ==> Krista should send a message for Adam and pass
the first two numbers (2, 4) and ask him to calculate the sum and ask him to find
the sum for the other two numbers (6, 8), then ask Adam to calculate the sum of
(6, 14) and then reply with answer (20) to the boss.

4
3. “Adam, Add (2, 4, 6, 8)” ==> Adam should reply that he doesn’t know how since
the command Add for Adam does not have four numbers.
4. “Krista, Add (1, 4, 7)” Krista should reply that she doesn’t know how since the
command (operation) Add does not have 3 numbers.

You are a Decider (this is a class)


Your capabilities are:
When you are asked DayOfWeek (Date), you will be given a date. Reply
verbally with the day of the week on which that date falls.

When you are asked to Rate (Name), you will be given a name. Reply verbally
with the rating of that person. The valid “ratings” are given below:

Naser Alatiyah Athletic


Yousef Alqardawi Islamic Scholar
anyone else “I don’t know”
Explanation: Notice the data type for the method DayOfWeek (Date), it is a date, thus
we must provide this object with right data type when we pass a message to it. The other
data type was text for the Rate (Name) method.
Examples: Assume we have created an object (Beth) from the Decider class.
1. “Beth, DayOfWeek (February 1, 2008)” ==> Beth should reply verbally “Friday”.
2. “Beth, Rate (Naser Alatiyah)” ==> Beth should reply verbally “Athletic”.
3. “Beth, Rate (Ismaeel)” ==> Beth should reply “I don’t know”.
4. “Beth, Rate (August 27, 2001)” ==> Beth should reply she does not know how
since the Rate method requires text (character) data type, not date data type.

Final comments:
Note how data is passed
- from Director to object: there may be no data, one datum, or multiple
data (parameters).
- from object back to Director: either no data is returned or only one
datum (e.g., the sum of two numbers).
b) Showing Java syntax for giving commands to (or requesting service from) an
object:
Mary.Clap(3)
Joe.Roll()
Adam.Add(10, 4)
Beth.Rate(“Naser Alatiyah”)
c) Creating method (function) prototypes for the capabilities or even for a class
definition (without implementing any functions)
e.g. void Clap(int n);
int Add(int a, int b);
b and c are beyond the scope of this course since they deal with programming course

Das könnte Ihnen auch gefallen