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):
- JLabels have already been created to identify the height and weight
textfields. You need to create a JLabel for the output, and one to hold the
actual BMI. (NOTE: Use the variables that have already
been declared.)
- Create JTextFields to hold the person's height in inches and weight in pounds.
- Create an appropriately labeled JButton for the user to press to calculate the BMI.
- Create a BMIListener and make it listen for the button to be pressed.
Note that the listener is added to the button, not
the textfields, so the BMI is computed only when the button is pressed,
not when the
user presses Enter on the textfields.
- Add the height label and textfield to the content pane
(the content pane has already been
created for you). Note that the components will appear on the content
pane in the order
in which you add them (left to right, top to bottom).
- Add the weight label and textfield to the content pane.
- Add the button to the content pane.
- Add the label identifying the result and the label holding the result to
the content pane.
In the actionPerformed method of BMIListener:
- Get the text from the height and weight textfields and store
it in the String variables provided.
- Use Integer.parseInt to convert the text to integer values, and store
them in the int values provided.
- Calculate the BMI from the height and weight values.
- Use Double.toString to convert the BMI to a string, and store the string in
the result label.
Compile the BMI applet and run it in the applet viewer. You may use the
html file BMI.html to run the applet.