CS 178 Programming with Multimedia Objects

Fall 2004

Recitation Exercise:

Prepared by: Aditya Mathur.

To be completed in class on September 10, 2004.

Learning objectives:
  1. How to handle an ItemEvent.
  2. How to use the if statement.
  3. How to write and use your own methods.
  4. How to divide work amongst group members.
Recitation Exercise Solution
Complete the following set of instructions to develop a simple Java applet shown above. Note that the applet does respond to events.

Applet declaration:

  1. Declare a class ChemicalFormulaEvent that extends class Applet and implements ItemListener.
  2. Declare elementName as an instance of class Choice. Initialize it to be an empty object.
  3. Declare elementSymbol as an instance of class TextField. Make its length equal to 2 columns.
  4. Declare atomicWeight as an instance of class TextField. Make its length equal to 8 columns.
  5. Declare atomicWeightLabel as an instance of Label. Initialize it to "Atomic Weight").

init method:

  1. Declare an init method. It has no parameters, is public and returns void.
  2. Add the following element names to elementName: Carbon, Iron, Silicon, and Uranium.
  3. Set the text in elementSymbol to "C" and the text in atomicWeight to "12.017".
  4. Set the background color of the applet screen to pink.
  5. Add an item listener to elementName (see page 253 of text on how to do this.)
  6. Make elementSymbol and atomicWeight as non-editable (see page 108 of the text and method setEditable(). Use this method.)
  7. Add to the applet the widgets that you have created.
  8. End the init method.

itemStateChanged method:

  1. Declare another method named itemStateChanged. It is public and returns void. It has one parameter of type ItemEvent.
  2. Declare a String object named selectedItem.
  3. Assign to selectedItem the name of the item selected by the user. You can obtain the name of the selected item by using the getItem() method. Note that the item name is a String object, e.g. "Silicon". For an example of how to use the getItem method, refer to page 256 of the text.
  4. Invoke method updateDisplay() with selectedItem as the parameter.
  5. End the itemStateChanged method.

updateDisplay method:

  1. Declare another method named updateDisplay. It is public and returns void. It has one parameter of type String.
  2. Check if the value of the input parameter equals "Carbon". If it does then set the text in elementSymbol to "C" and in atomicWeight to the atomic weight of Carbon.
  3. Check if the value of the input parameter equals "Iron". If it does then set the text in elementSymbol to "Fe" and in atomicWeight to the atomic weight of Iron.
  4. Repeat step 3 for Silicon and Uranium.
  5. End the updateDisplay method.
  6. We are DONE!

Question: Why is there no need to use the repaint() method in this program?

Symbol and atomic weights:

Carbon: C, 12.017. Iron: Fe, 55.845. Silicon: Si, 28.0855. Uranium: U, 238.02891.

Last update: September 9, 2004