First Summer 2006


Lecture Notes Five: Wrapping Up Methods (Chapter 4) and Introducing Events (Chapter 5, Alice text)
First we define introduction methods as in the text

First we define introduction methods as in the text.

 

 

The individual definitions have three parameters, and their content is simple:

 

 

We can change this to make the introductions differ more from character to character:

 

 

Now our main program looks like this:

 

 

Then we have to discuss once again the organization in the queen problem with pre- and post-conditions for the methods.

 

 

In what you see above bring kids down assumes the kids are up and the weight is down, then the kids would be down and the weight up.

 

The bring the queen down assumes the queen is up, kids are down and weight is up. At the end the queen and weight are down and kids are up.

 

You see then that the preconditions for bringing the kids down are once again satisfied, so we can call this procedure again, and the problem is solved.

 

In the very beginning the situation was such that the weight was up so that precondition can and has to be satisfied by dropping the weight down.

 

Now let’s see if we can make the entire program work with the rules we have set thus far.

 

 

Bringing the queen down is obviously made out of primitive actions, that is, actions that don’t require further explanations.

 

But notice how the state of the system changes after each instructions, hence the need for pre- and post-conditions.

 

We can easily inspect the code above and realize this procedure is working correctly according to our set of rules thus far.

 

 

Bring kids down is further split into components. Pre-conditions (you remember) are that kids are up and weight is down.

 

So this precondition must apply to the bring prince down problem. Let’s look at it:

 

 

That’s how it is! The program then drops the weight and calls the method that brings the princess down. So the precondition for that would be: weight and prince down, princess up.

 

Let’s see the bring princess down method. We already know the preconditions for it are: princess up, weight and prince on the ground.

 

 

 

It is easy to see that the preconditions for this method are: penguin  up, microwave and chicken down.

 

It is also easy to see that the result of running this method (the post-condition, the situation at the end) is that the penguin is down and the weight and the prince (chicken) are up.

 

Recall that this is just a step in the process of bringing the kids down. The next step would be to bring the prince down. But we can’t do exactly that, because bringing the prince down relies on having the weight down on the ground already. So we need to throw the weight down, then apply our method of bringing the prince down, which results in the weight being up, the prince on the ground. Together with the princess being on the ground we have the following post-condition for the entire method: kids on the ground, wight in the air.

 

Now recall the queen is still in the air at this point.

 

That’s why we can bring the queen down, with the weight, and take the kids back up to the tower, resulting in a state that matches the preconditions of bringing the kids down: weight is down and the kids are in the tower. So the entire situation can be summarized as follows: we can bring the kids down (without interfering with the queen). The post-conditions to that match the preconditions for bringing the queen down. When the queen is down the post-conditions for bringing the kids down are once again met and we can run that procedure again and finish that way.

 

So that’s the end of our problem.

 

The last thing we will do today is a bit from Chapter 5 (event-handling).

 

We’d like the methods defined for the beetles to actually be run not from the main method, and not in that order; we want them to be available to us whenever we decide to run them, and in any order.

 

 

So with this situation, we start create events. They should be for the introductions of George Beetle, Ringo Beetle, Paul Beetle and John Beetle.

 

 

First we set an event for the mouse.

 

 

So we get this. We can add two more, by dragging the methods from the world’s my first method into the newly created events.

 

 

So we modify the one statement (instruction) left behind, and with the exception of the first one all other beetles will respond to mouse clicks, in the order in which we want them to.

 

 

So we can play the world now.

 

 

There are other ways of adding events, but we can stop here now.

 

 


Last updated: May 14, 2006 by Adrian German for A201/A597