The file Fahrenheit.java is similar to the example program in Listings 6.5 & 6.6 of the text (pages 249 - 251). The program converts temperatures in Fahrenheit to the Celsius equivalent. The user enters a temperature in the text field and when the enter key is pressed the Celsius equivalent is computed and displayed. Pressing the enter key on a text field generates an action event so the program must implement the ActionListener interface. Save the program file to your lab 2 project directory and run it to see how it works. Study the code noting the following:
actionPerformed
method uses the getText
method to get the information out of the text field. That information is returned as a String.In this exercise you will write a similar program to compute a person's Body Mass Index. Body Mass Index (BMI) is a measure of weight that takes height into account. BMI is a simple way to determine if a person is over-weight or under-weight. BMI is calculated as follows for both men and women:
(703 * weight in pounds) / (height in inches)2
The file BMICalculator.java contains a skeleton for a GUI program to calculate BMI. Since there are two input values needed (the height and weight) this program will not respond to the user pressing the enter key on a text field as the Fahrenheit program does. Instead, it will have a button for the user to press to trigger the calculation. So, the user enters his or her height and weight and presses a "Calculate BMI" button and 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. Follow the instructions in the comments of the program.
Run the BMI program to test it. The layout of the program looks a little strange with the calculate button on the same row as the output button. In order to put these onto separate rows, you can add the button to a panel that is too wide to fit anything else on the same row. In the panel constructor, create a new JPanel for the button. Set the button panel's preferred size to the width of the window panel and a height that will fit the button. Then add the button to the button panel, and the button panel to the window panel. Run your program to make sure that it looks good.
The file Dog.java contains a declaration for a Dog class. Save this file to your directory and study it -- notice what instance variables and methods are provided.
The file DogTest.java contains a simple driver program that creates a dog and makes it speak. Study DogTest.java, save it to your directory, and then run it to confirm what it does.
The file Yorkshire.java contain declarations for a class that extends Dog. Save and study this files as well. Notice that the constructor calls the constructor of the Dog class using the reserved word super. Modify DogTest.java to add statements to add and print a Yorkshire (also make it speak).
The file Labrador.java also extends the Dog class. However, note that the Labrador constructor takes two parameters: the name and color of the labrador, both strings. Notice that this class contains an error:
Implicit super constructor Dog() is undefined
Fix the problem and then modify DogTest.java to create and make the Dog, Labrador, and Yorkshire all speak.
Add code to DogTest.java to print the average breed weight for both your Labrador and your Yorkshire. Use the avgBreedWeight method for both. Look at the error that you get. Figure out what is wrong and fix the problem by adding the needed code to the Yorkshire class.
Inheritance can be a useful way of structuing code for reuse. It can also be used to reuse existing code by adding functionality even if you did not write the original code. The file ToggleTest.java contains a program that uses the class ToggleButton.java that extends JButton to display a button that can be toggled between two different states (on or off). When the button is pressed it should change its text to reflect its new state. Complete the class by:
To submit your code: Tar your lab2 directory and submit your code on the course Inquire site.