MCS-170 The Nature of Computer Science
Alice Lecture 7
1. Creating User-defined Classes and Recursion
- Overview
- Creating User-defined Classes
- Recursive Methods
- Creating User-defined Classes
- Suppose
that we have created a character on the screen with many special
methods. Also, suppose we wish to create multiple copies of this
character, but preserving the new methods we have defined. We can
accomplish this by creating a new class that will store these new
methods.
- Example: In this example (squash2.a2w) we have created a monkey along with 8 special methods. In order to create a new monkey class, we do the following:
- 1) Rename the Class. We have to give the class a new name (different than monkey). In our example, we have called the new monkey a "trainedMonkey."
- 2) Save the Class. Right-click on the class in the Object tree (upper right corner) and select save object. In the box that pops-up go to the directory where you wish to save the class and click Save.
The class will be automatically saved with the new name, only
capitalized, and given the extension ".a2c" (for Alice version 2.0
Class)
- Once a new Class is created it can be used in a new world by selecting Import from the File menu. (Demo this)
- Note 1: The new Class (TrainedMonkey) will inherit all of the methods from Monkey.
- Note 2: Be careful when creating new Classes that you do not
define Class methods that depend on other specific objects in a scene.
For example, if we had defined a method forTrainedMonkey that used one of the bunnies, then this method might not work in other Alice worlds.
- Recursive Methods
- Indefinite Looping -- Recall that if we don't know beforehand how many times we want to repeat an action, then we can use a While
statement to test if some condition is met to stop the repetition of
the action. Today, we will look at a second technique for
handling indefinite looping -- that of recursive programming.
- Recursive Programming
is our first example of a programming idea that is not listed as one of
the tabs at the bottom of the Alice screen (Do in order, If/Else, etc).
This is because recursion is not a labeled programming statement, but
is a programming methodology, a way of creating a method that calls itself!
- Example: We have two bunnies in a garden. We want them to hop toward each other as follows:
- bunny and bunny2 turn to face each other
- bunny and bunny2, repeatedly hop towards each other at
the
same time as long as they are > 3 meters apart. Once they are closer than 3 meters, bunny says "hello"
- Here is a world
(bunnyGarden.a2w) with the bunnies in place and the hopping towards each other assumed to take place in the World.BunniesMeet method.
- Recursive Construction of World.BunniesMeet:
- If bunny distance to bunny2 > 3
- Do in order
- Do Together
- World.BunniesMeet
- bunny.say "hello"
- Implement this and test it out
- Example: Horse Race example from Chap 8 reading (horseRace.a2w)
- Note the top-down programming style -- have high-level methods that are repeatedly broken down into lower-level methods.
- Class Work:
Suppose you have a scene with a butterfly and a white rabbit. The
butterfly should move to a random location (within 0.2 meters). Each
time the butterfly moves, have the rabbit turn to face the butterfly
and move 0.5 meters toward the butterfly. When the rabbit gets
close enough, say "Gotcha!"