Search Wiki:
Resource Page Description

In the code we introduce classes for the first time to the students. If you don't want to begin the introduction with an employee or student class, here is a suggestion.

Problem to solve

A farmer goes to town and he takes with him a fox, a chicken and a bag of corn. On the way he must cross the river, but he only has a small boat on which he can carry only one of the objects. If he leaves on the same side of the river the fox and the chicken, while he takes the corn on the other side, the fox will eat the chicken. The same thing happens when he leaves on the same side the chicken and the corn. How can he cross the river to have all three objects on the other side?

Solution

To solve this problem we design a class which represents all of the items. The fox, the chicken and the corn are the objects which the farmer must take across the river. We describe our objects with a name, a side, a code (and later with a picture of the object). This are the instance variables. Each of the objects can be taken or released or can cross the river. This are the methods. We also have a class variable last, in which we save the code of the object which was the last that has crossed the river.

I'm used to solve the problem first and then design the user interface, so I first design a very simple interface to solve the problem. (In the file Solution.cs). There is implemented the logic of solving this problem. The solution of the problem is very similar to the pencil and paper method of solving problems.

When we are done with solution, we can think about how make this problem like a simple game. I thought about two listview controls which represents the left and right side of the river. (In the starting programming courses we don't use listview control very often, but it's useful.) All we have to do is to code the event handler for draging items from one listview to another.


Last edited Aug 5 2008 at 4:47 PM  by barbarap, version 7
Updating...
Page view tracker