CSC 241- Assignment 1 (Due February 12, 1998)

The goal of this assignment is to develop a traffic light applet. The applet will consist of a traffic light and a switch that controls it. In order to develop this applet, you will first develop two other classes and test them. Create a directory off of your public-html/classes/csc241 directory named traffic, make it public. Be aware that there is no packaging for any of the classes developed here, but all .java, .class, and .html files for this assignment must reside in this directory.


Light class

The Light class gives us objects that represents simple lights with on or off states. Lights are NOT graphically represented in this class, we only wish for each light to know its current state (on/off) and know its own color depending on that state. Here is the specification:

/**	This is a light class with two states on or off.  The light color is
    determined at construction for each state. Whether the light starts
    as on or off is also determined at construction.
 **/
import java.awt.*;

public class Light {
  public Light (boolean on, Color onColor, Color offColor)
  public void switchLight()//if light on, turn it off, otherwise turn it on
  public boolean on()
  public Color color()
}

Develop Light.java in the traffic directory and test it with testLight.java. To copy testLight.java ....

  1. Click on its link above to bring it up in netscape.
  2. Use the Save As... option in the File pull-down menu and save the file in your traffic directory.

Compile both your Light class and the test program for it. When executing the test program, you will notice that it outputs the color as a string, such as, java.awt.Color[r=0,g=255,b=0] ([r=0,g=255,b=0] is the code for green). Change the test program to display the name of the color (green or lightGray). The test program only needs to know about these two colors and display their names, no other colors need to be dealt with here. You must make sure that the Light class is working correctly before continuing.

trafficLight class

trafficLight is where we graphically represent a traffic light. A trafficLight has three lights where only one of the lights may be on at any point in time. To find the right Graphics methods, follow the link to Graphics class. Also, check out hello3.java for hints on drawing things. Here is the specification of the trafficLight class, there are only three methods that are left undefined:

import java.awt.*;

/** Traffic light class graphically represents a traffic light as a 
    rectangle with three internal circles.  The height of the
    the rectangle is determined at construction.  360 pixels will be the
    default value for the height, unless specified differently at
    construction.  The other dimensions in the traffic light are dependent
    on the height.  A traffic light must start as red (i.e. only
    the red light should be on).  Each time the change method is
    invoked the color changes and a repaint() call is issued.
    The color sequence is red to green to yellow and back to red.
 **/

public class trafficLight extends Canvas {
  protected Light red_ = new Light (true,Color.red,Color.lightGray);
  protected Light green_ = new Light (false,Color.green,Color.lightGray);;
  protected Light yellow_ = new Light (false,Color.yellow,Color.lightGray);;
  protected Light current_ = red_;//references the traffic light object that is on
  protected int height_;

  public trafficLight () {
    height_ = 360;
    resize( height_+4, height_+4 );
  }

  public trafficLight (int h) {
    height_ = h;
    resize( height_+4, height_+4 );
  }

  public int height () { return height_;}
  public int width () { return height()/3;}
  public int widthSeparation () { return width()/20;}
  public int radius () { return (width()-widthSeparation()*2)/2;}
  public int heightSeparation () { return (height_-radius()*6)/4;}

  //return the color of the light that is on (red,yellow, or green)
  public Color status () {...}

  //change light depending on the current state. Issue repaint(); once
  //changes are done.
  public void change (){...}

  // Draw the traffic light
  public void paint(Graphics g) {...}
}

Use traffic traffic2 to test your trafficLight class. The steps for copying traffic2.html and traffic2.java are similar to those for testLight.java, make sure they are placed in your traffic directory also. add a link to your csc241 page for traffic2.html. Make your traffic2.html, traffic2.class, trafficLight.class, and Light.class public.

Be aware that the traffic light applet that you will develop at the end of this assignment is very different than the traffic2 applet.

traffic light applet

You will develop your first applet, a traffic light that is controlled by a switch button. To see what your light should look like follow the link for Traffic Light . You might as well use my html file for this applet. Use hello3.java for hints on creating a button and writing an action method.