CPSC 170 Lab 2: GUIs and Inheritance

A Simple GUI

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:

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.

Inheritance

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.

Extending Existing Classes

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:

  1. Writing the costructor for the class. The constructor does not override any constructor in the JButton class. Instead it takes two Strings that represent the text on the button when it is in the on state and when it is in the off state. You will need to store these parameters in instance variables and call the super constructor that takes 1 string to specify the initial text that appears on the button.
  2. Writing the isOn method for the class. This method allows a user of the class to query the state of the button. You should use a boolean instance variable to hold the state of the button.
  3. Writing the ProcessMouseEvent method for the class. This method is an overridden method. It is called anytime there is a mouse event (mouse press, mouse release, mouse click, mouse moved, mouse entered, etc.). By calling the getID method of the MouseEvent parameter you can determine what the current event is. If the current event is MouseEvent.MOUSE_RELEASED, then the button was clicked. In this case, update the button text using the setText method of the JButton class and update the boolean instance variable to reflect the new state of the button.

To submit your code: Tar your lab2 directory and submit your code on the course Inquire site.