Lab 7: Thursday August 11th

Finger Exercises

Enter the following statements in the Interactions window, be sure that you are reflecting on what each one is doing.

int x = 3;
x = x + 2;
x

int y = 1;
int z = 1;
y = z - 1;
z = y + 1;
y * z

Now enter the following program into the definitions window

class Position {
  int x;
  int y;
  Position( int x, int y ) {
    this.x = x;
    this.y = y;
  }

  void swap() {
     int temp = this.x;
     this.x = y;
     this.y = temp;
  }
  Position swapTwo() {
     return new Position( this.y, this.x );
  }
}

And in the interactions window, enter the following statements
Position p = new Position(3,4);
p.swap()
p
p.swapTwo()

Traffic light


As we all know (and hopefully all obey) a traffic light has three light bulbs -- red, yellow, and green -- only one of which is on at a given time.

Develop the data to represent a traffic light.

Develop a method to change which light bulb is on for the traffic light, turning the lights off and on in the proper sequence.
i.e.
\\ Traffic light starts on red
trafficLight.change()
\\ Traffic light changes to green
trafficLight.change()
\\ Traffic light changes to yellow
trafficLight.change()
\\ Traffic light changes back to red

In some parts of Britain, two lights will be on at once, to indicate an upcoming change; the cycle is
Red
Red & Yellow
Green
Green & Yellow
Yellow
Red

Add a method to your traffic light to turn the lights on and off following a British sequence

If you have the time (and inclination) add a graphical element to your program using the drawing library seen last time, such that the traffic light has a display method, to open a window showing the current status of the lights, and every call of the method to change the lights also modifies the display.