Java Reference
In-Depth Information
Code 5.2
continued
The Responder
source code
public Responder()
{
}
/**
* Generate a response.
* @return A string that should be displayed as the
* response
*/
public String generateResponse()
{
return "That sounds interesting. Tell me more..." ;
}
}
Looking at Code 5.2, we see that the Responder class is trivial. It has only one method, and
that always returns the same string. This is something we shall improve later. For now, we will
concentrate on the SupportSystem class.
SupportSystem declares two instance fields to hold an InputReader and a Responder ob-
ject, and it assigns those two objects in its constructor.
At the end, it has two methods called printWelcome and printGoodbye . These simply print
out some text—a welcome message and a good-bye message, respectively.
The most interesting piece of code is the method in the middle: start . We will discuss this
method in some more detail.
Toward the top of the method is a call to printWelcome , and at the end is a call to print-
Goodbye . These two calls take care of printing out these sections of text at the appropriate
times. The rest of this method consists of a declaration of a boolean variable and a while loop.
The structure is
boolean finished = false;
while(!finished) {
do something
if( exit condition ) {
finished = true;
}
else {
do something more
}
}
This code pattern is a variation of the while-loop idiom discussed in Section 4.10. We use
finished as a flag that becomes true when we want to end the loop (and with it, the whole
 
Search WWH ::




Custom Search