CSC241 Assignment 2

Many UI-based programs use change-notification techniques of the following form: This assignment asks you to implement and exercise a particular form of this design, narrowing it just a little to deal only with subjects and observers that represent state as a single float. This kind of design requires three kinds of interfaces, one for Subjects, one for Observers, and one for the kinds of lists that each subject will need to record its observers. Here are the suggested declarations:
public interface Subject {
  /** Add x to the list of observers **/
  public void attach(Observer x);

  /** Remove x from the list of observers **/  
  public void detach(Observer x);

  /** Report the current represented value **/
  public float currentValue();
}

public interface Observer {
  /** Deal with the fact that Subject s has changed state **/
  public void changeNotification(Subject s);
}

public interface ObserverList {
  /** Standard list insert **/
  public void add(Observer x);

  /** Standard list remove **/
  public void remove(Observer x);

  /** For each element e, of the list, send e.changeNotification(s) **/
  public void reportChange(Subject s);
}

The assignment is to:
  1. Write a class that implements ObserverList using a linked list.

  2. Write a Temperature class that implements Subject, using the float to represent the current temperature, an instance of your ObserverList implementation to maintain the list, and warmBy and/or similar methods that use the list to do change notifications upon each update.

  3. Implement a WeatherSimulator class based on threads, as discussed in class. Depending on the exact details of your Temperature class, the WeatherSimulator will look something like:
          public class WeatherSimulator implements Runnable {
            static final long period = 500;   // update period in millisecs
            static final float offset = 0.5f; // bias for random numbers
            static final float scale = 1.0f;   // scale factor for random numbers
            protected Temperature temp_;      // the temperature object to change
          
            public void run() {
              for (;;) {
                float delta = (float)((Math.random() - offset) * scale);
                temp_.warmBy(delta);
                try { Thread.sleep(period); }
                catch (InterruptedException ex) {}
              }
            }
          
            public WeatherSimulator(Temperature t) {
              temp_ = t;
              new Thread(this).start();
            }
          
          }
          
  4. Write Three different kinds of classes that implement Observer. Here are the suggested classes, but you can substitute others:
    1. A Gauge class that looks at least vaguely like a thermometer.
    2. A DigitalDisplay class that displays the value as a number (for example using a textfield).
    3. A HeaterLight, that displays itself as on if the value is less than a desired temperature (as indicated in a constructor).

  5. Write an Applet that includes (in any reasonable way):
    1. At least one instance each of the three Observer implementations.
    2. A Temperature object to which the Observers can attach and detach.
    3. Simple buttons or whatever to attach and detach observers.
    4. A start method that creates a WeatherSimulator.

Doug Lea
Last modified: Wed Oct 23 19:09:54 EDT