CSC 241- Assignment 1 (Due September 15, 1999)

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. 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:


import java.awt.*;

public class Light {
/** 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.  No exceptions handling
    is required.
 **/
  boolean on_;
  Color onColor_;
  Color offColor_;
  public Light (boolean on, Color onColor, Color offColor) 
  /** Constructor that is paramaterized to set the fields of a 
      Light object.
   **/
  public void switchLight()
  /** If light is on, turn it off, otherwise turn it on **/
  public boolean on()
  /** return on/off state, ture if on and false otherwise. **/
  public Color color()
  /** return current color, depending on the light being on or off **/
}

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

  1. Copy testLight.java by click on its link above to bring it up in netscape on a SUN workstation and then using the Save As... option in the File pull-down menu. Save the file in your traffic directory.
  2. Compile both your Light class and the test program for it.
  3. Run testLight. 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 the color green). You must understand the test program and what it tries to do and 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 (status, change, and paint):


import java.awt.*;

public class trafficLight extends Canvas {
/** 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 traffice light changes state and gets redrawn.
    The color sequence is red to green to yellow and back to red.
 **/

  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 Color status ()
  /** Determine which light is on and return its color (red,yellow, or green)
   **/

  public int height ()
  /** Return the height of the trafficLight **/

  /*  Private methods used for determining dimensions when drawing the 
      traffic light */
  int width () { return height()/3;}
  int widthSeparation () { return width()/20;}
  int radius () { return (width()-widthSeparation()*2)/2;}
  int heightSeparation () { return (height_-radius()*6)/4;}

  public void change ()
  /** Change light depending on which light current_ references. 
      if current_ == red_  
         turn off red_, turn on green_, and have current_ reference green_
      if current_ == green_  
         turn off green_, turn on yellow_, and have current_ reference yellow_
      if current_ == yellow_  
         turn off yellow_, turn on red_, and have current_ reference red_
      repaint(); to redraw the traffic light  
   **/
  public void paint(Graphics g)
  /** Draw the traffic light, use the methods such as width for 
      determining dimensions **/
}

Use traffic traffic2 to test your trafficLight class.

  1. Save traffic2.html and traffic2.java in your account the same way as you saved testLight. Make sure they are placed in your traffic directory also.
  2. Compile both your trafficLight and traffic2.
  3. Add a link to your csc241 page for traffic2.html.
  4. Make your traffic2.html, traffic2.class, trafficLight.class, and Light.class public.
  5. This part is completed if traffic2 loads correctly in a web browser and behaves the way mine does. 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. traffic2.java should make clear how trafficLight objects are constructed and changed. Add a link to your csc241 page for this traffic light applet.

What/How to turn in Assignment

Email your .java files (Light.java, trafficLight.java, and your applet) as attachments to me.

Remember that your .class file and the .html file and the path to them must be public, but DO NOT make your .java files public, in fact, perform a chmod 700 *.java in the traffic directory to make sure. Visit Lab #1 to see how you make files public.