CS 101 (Fall 2002)
Author: Ron K. Cytron
with thanks to Amy Sia and James Brodman
Lab 3: Objects with state: Emote Encounters
| || 23 ||Sep
|| 24-25 ||Sep
|| 1-2 ||Oct
|| 4 ||Oct
Two Emotes meet at a club. Unsure of themselves,
they approach each other hesitantly. Can this chance meeting lead to
positively-valued invocations of their
adjustHappiness method? Or are they simply two
passing in the night?
In this lab, you get to decide because you become the director of this
rendezvous, scripting their conversation.
You do that by clicking a button with the line you wish delivered.
By the end of this lab, you should
- Further understand systems of objects and the has-a relationship
- Gain greater facility with graphical objects and conditional execution
- Become comfortable introducing state into objects
- Appreciate the value of helper methods
- Know how to provide meaningful comments for code
[[[ Download PC zip ]]]
- Read over this entire document before you start.
- Take a look at the JavaDoc documentation.
- Take a look as needed at the canvas documentation
- Study the examples in class and in the notes.
- Run the sample solution.
- If you need help, please ask.
- Classes you do not modify:
- Classes you do modify:
In this lab, we create a conversation between two Emotes.
The conversation has two components:
To accomplish this, we invent a new object Etoon
that "has-a" Emote and the ability to display text as well. This is
a very common thing to do in object-oriented programming: make a new object
through composition of previously developed objects.
- When one Emote speaks to another, we can see what the speaking Emote
is saying because the text appears over the speaking Emote's head.
- When one Emote speaks to the other, the other Emote listens and reacts
to the message by changing its happiness and/or temperature.
We want the Emotes to take turns speaking to each other. For that
purpose, we have a class Prompter that feeds
the line to an Emote to speak. That class must keep track of who spoke
last so as to alternate speakers and listeners between the two Emotes.
You are given a class Phrase that
creates a clickable button with a message on it. It is the line you wish
for the speaking Emote to deliver ot the other Emote. You are free
to change these messages as you like.
What to turn in:
- Complete a code cover sheet.
- Provide a printout of any files you have modified (see approach below).
Suggested order of implementation:
- Download the lab, compile it, execute it
- Work on the Etoon class:
- Create a DrawingPane for the class. It's width and height
are specified by the Etoon's constructor message. Should this be an instance
variable or a local variable in the constructor? You decide
by seeing if the DrawingPane must live between the methods. If
some other method needs access to the DrawingPane, then it must
be an instance variable.
- Create an Emote. How big should it be? It has to fit inside
the DrawingPane you just made, and you have to leave room for text
above the Emote's head. It's up to you, but I devoted 2/3 of the
height of the DrawingPane to the Emote and the rest for the text.
Once you've instantiated the Emote, get its DrawingPane, and add
that to the DrawingPane you made in the step above.
Notice how you can add a DrawingPane just like you can add
an Ellipse or a Rect. This lets you build bigger pictures out of
smaller ones -- the principle of composition!
- Complete getDrawingPane()
- Now go back to Startup(), make one Etoon, and add it to the
the ViewFrame already made for the Etoons.
You can't add an Etoon directly; you have
to obtain each's DrawingPane by the getDrawingPane() method, and add that
to the ViewFrame.
Be sure to test your
code at this point -- you should see an Emote showing up with
room above for text.
- Complete the hear method, and for now have it affect the Emote
by making it really hot or really happy or some extreme you can see.
We'll refine this behavior later.
To test at this point, send your Etoon a message from Startup and watch that it changes its
happiness or temperature in the way you expected. This message is for testing only, and you'll
want to remove it once you get the Prompter going (see below).
- Go back to Startup and have it
add two Etoons to the ViewFrame. You can't add the Etoon directly; you have
to obtain each's DrawingPane by the getDrawingPane() method.
If you don't see two, it may be the
case that they are on top of each other. So move one to a better
spot in the ViewFrame.
- Take a look at Prompter.
constructor takes in the two Etoons you have made. The code we have given you already
a Prompter in Startup and passes it the two Etoons.
- Eventually we want sendPhrase to alternate between the two Etoons,
but for now pick one Etoon to say the message and the other one to hear
- Compile and test your program by clicking on phrases and watching
the hearing Emote become very happy, very hot -- however you arranged for
it to react.
- Now go back to Etoon and place a StringComponent above the
Emote's head. Arrange things so that when the Emote says something
the text is put in the StringComonent via setText(String) call to
- Compile and test again, and see that the messages do appear.
- Go back to Prompter and get it to alternate between the two Etoons
in terms of who speaks and listens. Be sure to comment well your approach here.
- Put code in the Etoon to react properly to messages. Comment well your approach. Use the
indexOf(String) method on a String to see if a given
word is contained in the message. React as you wish to words that
make your Etoons happier, sadder, hotter, colder.
- Finish things off to make them look nice -- add new text messages
if you like.
Last modified 14:26:25 CDT 22 September 2002
by Ron K. Cytron