CSC111 Exercises on Writing Classes

From dftwiki3
Revision as of 08:24, 6 December 2011 by Thiebaut (talk | contribs) (Created page with "--~~~~ ---- =Exercise 1= * Create a class for a traffic light that can be drawn on the graphics window, with the bus and trees. * Your class should allow objects instantiated f...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 07:24, 6 December 2011 (EST)


Exercise 1

  • Create a class for a traffic light that can be drawn on the graphics window, with the bus and trees.
  • Your class should allow objects instantiated from it to
    • Turn the green light on. This will set the other ones off
    • Turn the yellow light on. This will set the other ones off
    • Turn the red light on. This will set the other ones off
  • The default mode is for TrafficLight objects to be created with the green light on.

Exercise 2

  • Same as Exercise 1, but the objects are designed to be part of an animation loop. In the loop objects such as cars, buses or trees move left or right. TrafficLight object may move as well. However, their main function is to slowly change state, going from Green to Yellow, to Red, and back to Green. But they do change state slowly, every couple seconds or so. This is done by having a method called tick(), which is called once every time through the animation loop. Every 10 loops the traffic light changes state.
  • The Traffic Light objects can be probed in the animation loop to see which light is on.
  • Here is an example of an animation loop with a bus, trees, and a traffic light:


position = Point( ..., ... )
light = TrafficLight( position )

while True:
    light.tick()
    
    if light.isGreen():
         bus.move( )