Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lists and Iteration in Alice

Similar presentations


Presentation on theme: "Lists and Iteration in Alice"— Presentation transcript:

1 Lists and Iteration in Alice
Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 ListsAndIterationInAlice

2 ListsAndIterationInAlice
Learning Goals Introduce the concept of a collection And the need for one Introduce the concept of a list Introduce iteration Show how to create a variable Show how to create a list Show how to iterate over a list ListsAndIterationInAlice

3 ListsAndIterationInAlice
Collections In some animations, several objects must perform the same actions Example: A marching band, where the band members are performing the same marching steps. You could tell each object to perform each action Your program will get very long very quickly In such situations, it is convenient to collect all the objects into a group ListsAndIterationInAlice

4 ListsAndIterationInAlice
One way to organize objects into a collection is to use a list. In our daily lives, we use lists to help us organize things. For example, assignments list shopping list In programming, a list is known as a data structure Way to structure our data ListsAndIterationInAlice

5 ListsAndIterationInAlice
Types of Lists In Alice, a list can be a list of numbers or a list of objects, colors, or a specified type …. In this session, we will use an example of a list of objects. ListsAndIterationInAlice

6 ListsAndIterationInAlice
Example The famous Rockettes® are preparing for their winter holiday show. You have been hired to create an animation for the holiday show that will be used on a web site where people can purchase tickets for the show. ListsAndIterationInAlice

7 ListsAndIterationInAlice
Creating a list: Step 1 Create the initial world and add five rockette objects to the world. Create a new variable in the properties panel for the world Why should the world keep track of the list of rockettes? ListsAndIterationInAlice

8 ListsAndIterationInAlice
Creating a list: Step 2 In the popup dialog box, type in a name select Object type check “make a List” box click new item button 5 times to select each of the 5 rockettes, one at a time ListsAndIterationInAlice

9 Programming with a list
Now that a list has been created, how do we use the list in a program? One of the most useful operations with a list is to repeatedly perform some action with each item in the list. We call this iteration or "iterating through a list." ListsAndIterationInAlice

10 Iterating through a list
Iteration can be done in two ways: in order (one at a time) all together (simultaneously) ListsAndIterationInAlice

11 Example: Iteration in Order
A typical chorus line dance involves each dancer (one after the other) kicking up one leg. Possible storyboard: kickUpRightLeg Parameter: whichRockette Do in order Do together whichRockette right thigh turn back whichRockette right calf turn forward whichRockette right calf turn back For all dancers in order item_from_dancers kickUpRightLeg ListsAndIterationInAlice

12 ListsAndIterationInAlice
Challenge Modify 07Rockettes.a2w Have all of the dancers put down their legs one at a time And then have them all lift them at the same time And then put them all down at the same time ListsAndIterationInAlice

13 ListsAndIterationInAlice
Things to Notice The method that lifts up the rockettes leg is defined in the world class not in the rockette class And it takes as a parameter of which rockette This is due to an a problem in the current version of Alice Methods don’t automatically apply to the object the method was called on Like in Java with the implicit parameter “this” It should be a method on the rockette class! And it can be, but it must take a parameter of which rockette ListsAndIterationInAlice

14 ListsAndIterationInAlice
Summary You can create a collection of objects A list You can repeat a series of steps for each object in the list Iterate over the list You can have the items in the list do the action one at a time Or at the same time ListsAndIterationInAlice


Download ppt "Lists and Iteration in Alice"

Similar presentations


Ads by Google