This homework is due before class on February 4.

There are two parts to this assignment: a paper design of a class hierarchy and an implementation of a program demonstrating basic object-oriented design.


1) [10 points] Design a class hierarchy, similar to the one designed in class, for something of interest to you. Your design need not be complex or elaborate, but it must be well thought out. A design with five to ten classes, three to five of which form a subclass chain, will suffice. Describe some possible methods and instance and class variables for all classes in the hierarchy. Pay particular attention to abstraction boundaries.

2) [40 points] Your task is to design and implement an object-oriented program to operate stop lights.

a) Implement a class of light bulbs. It should contain an on/off state variable and methods to toggle and retrieve this state. Your methods might look like this:
      public boolean isOn()
    
      public void toggle()
    
b) Extend the class of light bulbs to a subclass of color bulbs. Choose a representation for colors; int or String are obvious choices; document your design decision. Write a class constructor to initialize the color upon object construction.

c) Create a place-holder class StopLight as follows:
class StopLight {
}
Design and implement a subclass of StopLight for a unidirectional stoplight (UniLight) that can change from red to green to yellow and back to red. Your design should include methods to change and retrieve the light's state:
      public void change()
    
      public state-type getState()
    
where state-type depends on your encoding of color bulbs from (b). The change() method should change the light's state to the next color; i.e., from red to green in the first call, then from green to yellow in the next call, etc.

d) Design and implement QuadLight, another subclass of StopLight that governs traffic at intersections of two two-way streets. Build it from UniLight objects. Call the four directions N,S,E, and W. Proper operation must insure that when N-S has green or yellow, E-W sees only red, and vice versa. Public methods to change a QuadLight's state and to examine its state should be provided:
      public void change()
    
      public state-type getState(dir-type dir)
    
state-type again depends on your representation for color bulbs and dir-type depends on the representation for directions.

e) Build a small street of QuadLights. Since towns and cities tend to grow or shrink over time, use the existing java.util.Vector class (a flexible type of array) to hold the lights. Write code to change the lights on the entire street through a couple of red-green-yellow cycles and to print the state of each light after a change operation. (Your lights may or may not be synchronized along the street.)
In this program, avoid the use of nested if-then-else-if... constructs. Instead, try to utilize arrays indexed by state counters. Recall that an integer mod (%) N can be used to keep such counters in range of an array of size N.

Hand in: a written description for (1), the complete code and comments for (2), and a script of the output of (2e).
instructions on how to submit