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 directory and run it to see how it works. Study the code noting the following:

In this exercise you will write a GUI 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 label. 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 main method, create a new JPanel

Mouse Events

The file MouseTest.java contains the program from class that prints mouse events. The file Draw.java contains the outline of a graphical program. For this section of the lab you will create a program that allows the user to draw on the window using the mouse. Do the following:

  1. Add mouse motion events to the Draw program. You don't need to add mouse click events as the MouseTest program does. Add a print statement to the mouseDragged method and run the program to test that the mouseDragged method is being called when you click and drag across the program's window.
  2. Create instance variables to represent the x and y coordinates of the mouse. Add code to the mouseDragged method to update the x and y instance variables to the current location of the mouse. Also add code to the paint component method to draw a small filled oval that is centered over the location specified by the mouse coordinate instance variables. The size of the oval should be stored in a static final instance variable. Also add a call to the repaint method at the end of the mouseDragged method so that the paintComponent method is called every time the mouse is dragged. Run the program, it should draw an oval underneath the mouse when it is dragged.
  3. In order to get the program to draw lines, each oval that is drawn needs to persist. This is easy to accomplish by removing the code that clears the window every time the paintComponent method is called. The first line of code, super.paintComponent(g), calls the paintComponent method of the class that this class extends, which is JPanel. The only thing that this method does is draw a filled rectangle over the entire window that is the color of the background. So, if you delete this line of code, previously drawn ovals will not be erased and dragging the mouse should draw lines. Be sure to test your program.

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 contains 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

This is because the first line to every constructor must be a call the the constructor of the super class. If you do not explicitly make this call, the java compiler will insert an implicit call to the constructor of the super class assuming that there are no parameters. However, in the case of the Dog class there is no constructor with no parameters. Fix the problem by adding an explicit call to to the Dog class constructor. 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 structuring 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 constructor 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.

Submission

Submit a zip file of your code on the course Inquire site that uses your last names as the file name.