CPTR 124 Fundamentals of Programming
In this lab you will provide alternate implementations of the traffic light class given in Chapter 7.
- Teams
You are encouraged to work with a partner for this lab. You and your partner should begin thinking about the problems and begin writing the code before lab time.
- Preparing your working folder
Make a subfolder named Lab5 within your H:\cptr124 folder.
- Alternate traffic light implementation
Do Exercise #10 in Section 7.6 in your textbook. In your new version you will use three Boolean variables to keep track of a light's state instead on the single integer value used in RYGTrafficLight. Name your new class AlternateTrafficLightYou will need to adjust the constructor and methods to work with this new representation. Your traffic light objects should behave identically to the RYGTrafficLight objects in the book.
- Adding novel behavior
Create a new class named AugmentedTrafficLight. You may use either RYGTrafficLight from the book or your new AlternateTrafficLight as a starting point; that is, you decide how best to represent the state of this new kind of traffic light.
AugmentedTrafficLights behave in an interesting way:
- The arrangement of red, yellow, and green lamps is
exactly the same as in RYGTrafficLight.
- When the red lamp is illuminated all by itself,
traffic facing the light must stop and wait.
This is "stop" state.
[(R) ( ) ( )]
- Just before changing to green, this new kind of traffic
light illuminates its yellow lamp while its red lamp
remains on. This state means "get ready to go, but do
not go yet." This state we will call the "get ready to go"
state.
[(R) (Y) ( )]
- After the "get ready to go" state the green lamp is
illuminated all by itself. This means it should be safe
to proceed. This is the "go" state.
[( ) ( ) (G)]
- Towards the end of the "go" state the yellow lamp is
turned on while the green lamp stays on. This is
the "get ready to stop" state.
[( ) (Y) (G)]
- After the "get ready to stop" state the green lamp turns
off leaving the yellow lamp on all by itself. This is
the "stop is imminent" state.
[( ) (Y) ( )]
- After the "stop is imminent" state the light returns to
"stop" state. The same cycle continues indefinitely.
[(R) ( ) ( )]
- The arrangement of red, yellow, and green lamps is
exactly the same as in RYGTrafficLight.
- Check out
Demonstrate that both of your traffic light objects work as they should and then print out the source code for your AlternateTrafficLight and AugmentedTrafficLight classes. Be sure your name is on each sheet and staple them together. If you worked with a partner, please submit only one set of printouts with both names on it.