Dawn DuPriest posted an interesting project on her blog - Teaching event-based programming – a traffic light. I like it a lot. It’s a good way to introduce state programming. Dawn did her project in Processing which meant a few limitations. That’s not a bad thing. Working around limitations can be great for learning.
With .NET languages (I use Visual Basic with my freshmen and C# with my sophomores) I have a timer object to work with. That opens a few more options for me and I have been thinking about using the project in a couple of ways. I have created a light class which is used by a traffic signal class. Maybe more complicated then I need but I needed to try some stuff.
I have the timer control call Signal.NextState where Signal is an object of type Traffic Light. The Traffic Light class has a NextState method which checks the current state and changes the light and returns a new timer interval.
I'm thinking I can use the traffic light class more or less as I have it now with my freshmen so they can learn about the timer. I may have my sophomores create the Traffic Light class. The classes I created are written in C#. That means I can use them as the basis for my examples for the sophomores.
Since it is all .NET I can use the C# objects in Visual Basic programs. That means I can hide the complicated stuff from the beginners who are not ready for it. We can stick with just using the timer. Or I can create a version that doesn’t do the state work inside the traffic light control. That way I can talk about state programming and still hide a lot of things.
If I take the later path I can show my students who take the second course how we can encapsulate this sort of thing. We’ll see. I have a lot more thinking to do about this whole thing. But having some code to play with helps me work things out.
Edit: Dan Schellenberg has his own riff on this project and blogged about it at Traffic Light Remix