Name(s)
:
Response Boxes from javabook
Tom Linton, COSC 135 A Spring 2003
You may work in pairs, or individually on this activity. It is due
Monday after break (3-17, before midnight).
The goal of this activity is to gain an understanding of another useful
javabook class, the ResponseBox class. The ResposeBox class simplifies
the process of getting a yes-no answer from the user. However, you do
NOT get a boolean value from a ResposeBox, but you get an int,
similar to interacting with a ListBox. ResponseBoxes are very similar
in functionality to ListBoxes, but a ResponseBox is easier to create and
use. You can think of a ResponseBox as an automated version of a two (or
three) choice ListBox, with buttons like a MessageBox, instead of the menu
you get with a ListBox.
ResponseBox Programming involves many situations
where we require an answer from the user that consists of one choice,
from amongst 2 (or 3) alternatives. For example:
- Would you like to keep playing? (Yes or No)
- Was the phone call residential or business?
- Would you like instructions? (Yes or No)
- Would you like to send a letter or box?
In the past, we've coded these questions with either int answers
from the getInteger method of an InputBox object, like
1 = yes, 2 = no, or by using a ListBox. ResponseBoxes are better
for these situations.
To put a ResponseBox to use, you must declare and create an instance
of the ResponseBox class. Since a ResponseBox must appear
in a window, the normal constructor requires a MainWindow object
as a parameter (actually a Frame will do, but we'll stick to MainWindow
objects). Once you've declared and created a ResponseBox object,
you invoke its prompt method to get a response (so you don't have
to make it visible).
- Using BlueJ, create a new Driver class for this activity. Name
it ResponseActivity.java. Be sure to put your name(s) in the
header comment, and add a description like "For the introduction to responseboxes
class activity", as well as today's date.
- In your newly created Driver class, import javabook (and add
the javabook folder to your project folder if needed) and then add the
following code to the main method (you can view this file on-line, and
use copy and paste, by going to Blackboard-Assignments, or to Tom's homepage
(http://www.central.edu/homepages/lintont), selecting the COSC 135 link
under current courses, and then finding the link to this activity
under either the materials section, or the schedule section
of this classes web-page) :
//Declare and create I-O objects
MainWindow pella = new MainWindow();
pella.show();
OutputBox outy = new OutputBox(pella, "Getting responses");
outy.show();
InputBox inny = new InputBox(pella);
ResponseBox asky = new ResponseBox(pella);
//Declare an int variable, and ask a simple question
int answer;
answer = asky.prompt("Are you ready for spring break?");
//Print the results
outy.printLine("The value of answer is " + answer);
By compiling and running the code, answer each of the questions below
(on this piece of paper). - How many buttons appeared in the ResponseBox?
- What labels were printed on these buttons?
- How would you change the command that invokes asky's prompt
method if you wanted to ask the user if they were taking a trip over spring
break? You needn't make this change in your source code file, just write
the new version of the Java command below.
- What value gets assigned to answer if you:
- click the "yes" button?
- click the "no" button?
- click the "close window icon" (the x in the upper right corner)
of the ResponseBox object named asky?
- Since good programmers define and use constants, the ResponseBox
class has several (they are class constants). Add the following code to
the bottom of your Driver class, and then indicate the values of the 3
class constants displayed (basically copy the output onto this paper).
outy.printLine("The class constant ResponseBox.YES = " + ResponseBox.YES);
outy.printLine("The class constant ResponseBox.NO = " + ResponseBox.NO);
outy.printLine("The class constant ResponseBox.CANCEL = " +
ResponseBox.CANCEL);
Quite frequently you'll get into a situation where the labels "Yes"
and "No" aren't quite right, and you'll want to change the labels on the
buttons. It isn't obvious how we'd ask Java to change the label on the first
button to "Draw" and the second label to "Hold", but such requests are easy
to accomplish, thanks again to some handy class constants. Comment out, or
delete the code added to your Driver class for the last question. Next,
add the code below to your Driver class, and place it in between where
you declare the variable answer, and where you invoke asky's
prompt method, to assign a value to answer. Actually,
you should delete the old "answer = asky.prompt..." line as well.
asky.setLabel(ResponseBox.BUTTON1, "Draw");
asky.setLabel(ResponseBox.BUTTON2, "Hold");
answer = asky.prompt("Would you like another card, or are you
good?");
//The line below needs to be added back if you deleted it earlier
outy.printLine("The value of answer is: " + answer);
- Run the program (2 or 3 times), click each button, and observe
the value of answer in each case. If you change the labels printed
on the buttons, does it have any effect on the values returned by the prompt
method?
The last question should have indicated that regardless of the labels,
the left button always corresponds to the class constant ResponseBox.YES,
and the right button always corresponds to ResponseBox.NO. When
you change labels, tests like "if (answer == ResponseBox.YES)"
can be misleading. They are however correct and slightly better than if
(answer == 1). Fortunately, the constants we used for labels (ResponseBox.BUTTON1
and ResponseBox.BUTTON2) have the same values as ResponseBox.YES
and ResponseBox.NO, and often times, these latter constants
"read" more clearly in tests.
Since ResponseBoxes have only a few choices (you can actually define a
three-button ResponseBox, see the example below for details), processing
the results of a ResponseBox's prompt method is normally best handled with
nested if statements (instead of a switch block). Here's a typical example:
Note, since it is easy to change the labels on a ResponseBox, you
normally only need one instance of the ResponseBox class. However,
you cannot make a two-button ResponseBox into a three-button ResponseBox
(or vice-versa), so sometimes you need two ResponseBox objects.
//same beginning as the original code above
answer = asky.prompt("Are you ready for spring break?");
while (answer == ResponseBox.CANCEL){
answer = asky.prompt("You must click Yes or No. Are
you ready for spring break?");
}
if (answer == ResponseBox.NO) { //Not ready for spring break
asky.setLabel(ResponseBox.BUTTON1, "Long drive home");
asky.setLabel(ResponseBox.BUTTON2, "Too much to do");
answer = asky.prompt("That is too bad, why not?");
if (answer == ResponseBox.BUTTON1) //long drive case
outy.printLine("Yeh, me too,
but it'll be nice to see the family!");
else //too much to do or cancel case
outy.printLine("You better
get busy then!");
}
else { //user is ready for spring break
ResponseBox threeChoice = new ResponseBox(pella,3);
threeChoice.setLabel(ResponseBox.BUTTON1, "Vacation");
threeChoice.setLabel(ResponseBox.BUTTON2, "Going Home");
threeChoice.setLabel(ResponseBox.BUTTON3, "Staying
in Pella");
answer = threeChoice.prompt("What are you doing for break?");
if (answer == ResponseBox.BUTTON1)//taking a trip
outy.printLine("I hope your heading to warmer
weather!");
else if (answer == ResponseBox.BUTTON2)//going home
outy.printLine("Say hi to your family for
me!");
else //Staying in Pella or cancel
outy.printLine("I hope the snow melts here
in town!");
}
- In your ResponseActivity.java file, use a ResponseBox
to ask the user if they'd like to mail a letter or a box. If
they pick letter, use the same ResponseBox to ask if they'd like
next day standard service or next day priority service.
If they pick box, create a three button ResponseBox that offers them
next day priority, next day standard, or two day
service. Print out a summary of their responses (something like "Thanks you
for chosing to send your letter using next day standard
service". Print out a copy of this code and hand it in with your printed
copy of the activity (one per group).