CPTR 124 Fundamentals of Programming


In this lab you will design a class from which objects will be instantiated. Your class has a very precise specification upon which other objects depend. You must support the interface (methods) expected by the clients, or the program will not work correctly. Your class must also properly define an instance variable so that a traffic light object has a notion of its state (red, yellow, or green).
  1. You should work with a partner on this lab. You may work alone, if you insist. Please submit one Lab Sheet per team.

  2. Create a project named Lab6. This is where you will do your work for this lab. Create a class named TrafficLight. It is crucial that you use this name and name your methods exactly, or the overall program will not work.

  3. Copy the following files into the default package of your project:

    These files contain the code the creates and controls four graphical traffic light objects. In order for these graphical traffic lights to work correctly you must create an appropriate TrafficLight class.

    These classes appear to contain errors, but this is because you have yet to provide the TrafficLight class that they require.

  4. This lab has two parts:
    1. Develop the class named TrafficLight and verify that it runs correctly with the client code provided
    2. Copy and modify one of the client classes so that it uses your TrafficLight objects in a new way.

  5. TrafficLight class.

    • Your job is to develop a class named TrafficLight. The traffic controller classes will create and exercise instances of your TrafficLight class. The GraphicalTrafficLight class is used to create visual objects that "wrap" your TrafficLight objects for an interesting visualization.

      Traffic light picture

      Important issues:

      • Your TrafficLight class must be perfectly compatible with the sample client code, but your class will also be checked against another client that tests all its required behavior. Be sure to provide all the functionality, not just the functionality required by Intersection.

      • A TrafficLight object should maintain information about its current color. What is a good way to do this? The Color class in the java.awt package contains a number of constants that we can use to represent a light's color:
        • Color.RED
        • Color.GREEN
        • Color.YELLOW
        • Color.BLACK
        In order to use these constants (which actually refer to Color instances) you will need to import java.awt.Color. Use a Color instance variable to maintain a light's color. All the methods described below interact with this instance variable in some way.

      • A TrafficLight object must respond to the following messages:

        • setColor() changes the light's state to a particular color. It accepts a single java.awt.Color parameter representing the desired new color and returns the light's previous color (a java.awt.Color). The only acceptable colors are red, yellow, green, and black; any attempt to set a light's color to any color other than these should be ignored.

          Currently the client code ignores the setColor() method's return value. The client code you write in Part 2 will also ignore it. Future clients, however, may want to take advantage of the return value, so that is why we want to make provision for it here.

        • getColor() accepts no parameters but returns a reference to a java.awt.Color object corresponding to the light's current color.

        • nextColor() changes the light's color to a new color in what is considered its "normal" cycle pattern:
          • Red --> Green
          • Green --> Yellow
          • Yellow --> Red
          It accepts no parameters but returns a reference to a java.awt.Color object corresponding to the light's previous color

      • The TrafficLight class should have a constructor that sets the light's initial color to black. Recall that a constructor
        • looks like a method (it is really not a method, though, strictly speaking)
        • has the same name as the class itself
        • has no return type--not even void
        • executes when an instance is created using new.

      • This is the first exercise in which you develop code that is not in control. Your TrafficLight class must passively respond to requests from code over which you have no control. This requires a different way of thinking about programming. Much of software development today involves building software components that must correctly operate within the context of a larger software system. Your TrafficLight objects are examples of such components within a graphical interface that make up the complete program. The system (program) has a set of expectations about the capabilities of trafficlight objects. This means that TrafficLight objects must be able to respond to various messages (method calls).

      • Your TrafficLight class should not have a main() method. Remember, it is not in control. (The main() method is in another class.)

      • Remember, do not touch the other classes. You are free to look at the client files that you imported into this project, but do not touch them or things may not work correctly.

      • If any of the methods in your TrafficLight class exceed a few of lines of code, then you are probably doing something wrong!

      • The main() method is in Intersection, so your launch configuration should take this into account.
  6. Flashers.
    • Copy the Intersection class into a new class named FlasherIntersection. Modify FlasherIntersection's doLightChange() method so that it makes the north/south lights flash red and the east/west lights flash yellow. When the north and south lights are red, the east and west lights should be off. When the north and south lights are off, the east and west lights should be yellow.

      You should also modify the four lines in the main() method that set the initial colors of the lights. Make the north/south lights initially on and the east/west lights initially off. You should not have to make any other changes to the new client code. You should not touch the GraphicalTrafficLight code at all.

    • Important! You must not modify the TrafficLight class you used with Part 1. The exact same TrafficLight class must work with both the original Intersection and your new FlasherIntersection classes.

      If everything works correctly, you have successfully created a reusable software component (TrafficLight) that can be utilized in other related applications.

  7. Consult the Sun code convention website (http://java.sun.com/docs/codeconv/index.html), and make your code conforms to these guidelines. Reformating your code using Eclipse's automatic formating feature will meet these standards.

  8. When you believe the program works correctly:
    1. recheck your style to make sure it is similar to the Sun guidelines,
    2. let me check and review your lab with you,
    3. get a Lab Report (one per team),
    4. close your project, exit Eclipse, and log out from your workstation.