Lab 12 In-Class: Graphical User Interfaces

Vote Counter - Using Buttons

The file VoteCounter.java contains a slightly modified version of PushCounter.java in Listing 5.13 of the text (pages 305-306). As in the text the program counts the number of times the button is pushed; however, it assumes ("pretends") each push is a vote for Joe so the button and variables have been renamed appropriately.
  1. Compile the program, then use the appletviewer to see how it works (you may use the file Vote.html).

  2. Modify the program so that there are two candidates to vote for -- Joe and Sam. To do this you need to do the following:
    1. Add instance variables for Sam -- a vote counter, a button, and a label. Initialize these variables in the init method.
    2. Add a new inner class named SamButtonListener to listen for clicks on the button for Sam. This will be just like the JoeButtonListener class except it will modify the variables that hold Sam's information instead of Joe's. Create an instance of this class when adding the ActionListener to the button for Sam.
    3. Add the button and label for Sam to the Container object.

  3. Compile and run the program.

Greetings - Listening for Events on a Text Field

The file Greetings.java contains an applet with a text field. When the user enters a name in the text field and presses ENTER the applet displays a greeting (there are four different greetings that can be displayed).
  1. Save the program to your directory, compile it and run it in the appletviewer (you may use the file Greetings.html). Press ENTER several times in the text field; change the name at least once.
  2. The same listener in a program can listen for more than one event. Modify the program by adding a button that, when pressed, will display a new greeting just as pressing enter on the text field does. The listener that is already defined should also listen for the button push. That is, one listener will listen for two different events -- it will respond to either event by displaying a new greeting. All you need to do is the following:
  3. Compile and run the applet. You may see that the components no longer fit in the applet window (you may have seen this earlier if you typed in a long name.) If so, change the applet size -- try various sizes until you find something that looks good.
  4. Test the program by both pressing enter on the text field and by pressing the button. Either event should cause a change in the message. Also try typing in a new name (but don't press enter), then clicking on the button. You will see that the new name is displayed (because the actionPerformed method gets the name from the text field each time it is executed -- the new name is there even though enter was never pressed).
  5. Adding Some Color Modify the applet so that it changes colors when an action event occurs. Do the following:

Computing Body Mass Index

Body Mass Index (BMI) is measure of weight that takes height into account. Generally, a BMI above 25 is considered high, that is, likely to indicate that an individual is overweight. BMI is calculated as follows for both men and women:
           (703 * weight in pounds) / (height in inches)2

  1. File BMI.java contains a skeleton for a GUI to calculate BMI.The user enters his or her height and weight and presses "Calculate BMI"; the program then displays the user's BMI. Much of the framework has been done for you, but you will need to fill in code. (Note that this program is much like the applet version of the temperature conversion program handed out in class. The application version of that program is FahrenheitGUI.java in Listing 5.15 of the text.) Do the following (follow the instructions and the comments in the program):

    In the init method of the applet you need to do the following (put your code for each immediately after the corresponding comment in the code):

    In the actionPerformed method of BMIListener:

    Compile the BMI applet and run it in the applet viewer. You may use the html file BMI.html to run the applet.

  2. (Optional) Add code to the BMI applet to let the user have some idea of what their BMI means. You need to either modify the JLabel that displays the BMI or add a new JLabel. Your program should include a message based on the following: a BMI less than 19 indicates the person is underweight, a BMI between 19 and 25 (inclusive) means a healthy weight, a BMI between 26 and 30 (inclusive) indicates the person is overweight, and a BMI over 30 indicates the person is very overweight.

HAND IN

Hand in printouts for VoteCounter.java, Greetings.java, and BMI.java. Tar your directory and email the tar file to your instructor with a subject of cpsc120 lab12