The goal for the lab is to reinforce routines, parameters, and accessing parts of an object. In addition we will improve the GUI (graphical user interface) a bit and make the conversation into more of a dialog.

  1. Currently the only way to exit the system is to push Microsoft's little x button. This is a bit unsophisticated. We should have our own Exit button as part of the application.

  2. So we will add one. You know all about adding buttons so the only trick to this is that you need to call the routine System.exit(0) when the button is pressed. (The 0 indicates we are exiting by choice and not because of any error that may have occurred.)

  3. Right now, our Rulers are forever saying Hello, but no one ever responds. Today we'll add code so that if Abe greets Joan, there is one chance in three that Joan will respond with Same to you, President Abraham Lincoln. Or something similar. See below for the steps.

  4. As usual, do this in small steps. First you should slow down the timer so you can better see what's happening.

  5. Then add a routine, respond(), to Ruler as illustrated in class. For now the response should just be Same to you, or some such, and it should show up in the talkLabel.

  6. Then call this routine from sayHelloTo() in Ruler. Something like otherRuler.respond() will work.

  7. At this point, every greeting should generate a response, but it's just a generic response. That is, just Same to you as opposed to Same to you, Abraham Lincoln. To personalize the response we need to know to whom to respond.

  8. As in class, add a parameter of type Ruler to the declaration of respond(). You'll have something like public void respond(Ruler greeter). Use the parameter in your response. It's a Ruler so you can do things like greeter.getIdentity().

  9. Now change the call to otherRuler.respond(this). The word this is a special Java word that refers to the current object. It's like handing someone your phone number and saying, this is my number. You've given the other person a way to call you back. It's the same with objects.

  10. Add in html tags so that you're sure to see the entire response.

  11.  At this point, you should see a personalized response every time. This is still unrealistic as not everyone will call you back.

  12.  On average, we want the new routine to be called every third time someone says hello. To do this you'll need a random number generator in Ruler. Talk to your partner about how to use the Random number generator in conjunction with an if statement so that you only call respond() one third of the time. Then do it.

  13. Make sure your partner is up to date with all files. E-mail me Conversation and Ruler.