CS 178 Programming with Multimedia Objects

Fall 2004

Recitation Exercise:

Prepared by: Aditya Mathur.

To be completed in class on September 3, 2004.

Learning objectives:
  1. How to write an applet.
  2. How to create simple text widgets.
  3. How to add a (simple) applet to your web site.

Complete the following set of instructions to develop a simple Java applet shown above. Note that the applet does not respond to events.
  1. Add an import statement to import java.applet.* (What happens if the import statement is missing?)
  2. Declare a class ChemicalFormula that extends class Applet.
  3. Declare elementName as an instance of class Choice. Initialize it to be an empty object.
  4. Declare elementSymbol as an instance of class TextField. Make its length equal to 2 columns. (What happens if the length is not specified during initialization?)
  5. Declare atomicWeight as an instance of class TextField. Make its length equal to 8 columns.
  6. Declare atomicWeightLabel as an instance of Label. Initialize it to "Atomic Weight").
  7. Declare an init method. It has no parameters, is public and returns void.
  8. Add the following element names to elementName: Carbon, Iron, Silicon, and Uranium. (Does the sequence matter ?)
  9. Set the text in elementSymbol to "C".
  10. Set the text in atomicWeight to "12.017". (What happens if the text is set after the widget has been added to the screen?)
  11. Set the background color of the applet screen to orange.
  12. Add to the applet the widgets that you have created.
  13. End the init method. (Why is it that we do not need a paint method in this applet?)
Last update: September 2, 2004