Design Patterns  «Prev  Next»

Abstract Traffic Signal - Exercise

Course Project, Part 2

Objective: Write the classes for the traffic signals. As the first part of the course project, begin by creating the traffic signals.
Start with an abstract TrafficSignal superclass. This class should:
  1. Define three states of a signal which are named constants, STOP, CAUTION, and GO.
    At any one time a signal is in exactly one of these states. Furthermore, the state always changes in the order STOP-GO-CAUTION-STOP. It cannot get from GO to STOP without first going through CAUTION.
  2. The TrafficSignal has a passageRequested attribute which is true if a car or pedestrian has asked to cross the intersection. A pedestrian might ask by pressing a button on a light-post. A car might ask by triggering a weight-sensitive sensor when it approaches the intersection.
This class is shown in this class diagram:
UML diagram of the TrafficSignal class
UML diagram of the TrafficSignal class

One final note: when state is changed to GO, passageRequested should be automatically set to false. The state can only be set to GO.
  1. Next, derive two concrete subclasses of TrafficSignal, TrafficLight and WalkSign that implement the getMessage() method.

In the text area below, type your answer.
Click the Submit button to submit your answer.