CS 101 (Fall 2002)
Lab 3: Objects with state: Emote Encounters

Author: Ron K. Cytron with thanks to Amy Sia and James Brodman
Lab Assigned Design Due
(In class)
10 AM
(In Lab)
(In Lab)
Lab Due
(In class)
10 AM
23 Sep None 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 DrawingPanes 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 Before starting:
[[[ Download PC zip ]]]
Zip includes:

Problem statement

In this lab, we create a conversation between two Emotes. The conversation has two components:

  1. 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.
  2. When one Emote speaks to the other, the other Emote listens and reacts to the message by changing its happiness and/or temperature.
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.

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:

  1. Complete a code cover sheet.
  2. Provide a printout of any files you have modified (see approach below).

Suggested order of implementation:

  1. Download the lab, compile it, execute it
  2. Work on the Etoon class:
    1. 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.
    2. 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!

    3. Complete getDrawingPane()
    4. 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.

    5. 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).

  3. 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.

  4. Take a look at Prompter.
    1. Its constructor takes in the two Etoons you have made. The code we have given you already instantiates a Prompter in Startup and passes it the two Etoons.
    2. 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 the message.
    3. 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.
  5. 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 the StringComponent.
  6. Compile and test again, and see that the messages do appear.
  7. 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.
  8. Test!
  9. 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.
  10. Finish things off to make them look nice -- add new text messages if you like.
Good luck!

Last modified 14:26:25 CDT 22 September 2002 by Ron K. Cytron