Download presentation
Presentation is loading. Please wait.
Published byColin Charles Modified over 9 years ago
1
Lists Alice
2
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. In such situations, it is convenient to collect all the objects into a group (a collection) Major benefit -- we can write program code for all the objects in the group (rather than separate code for each object).
3
List 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 a collection of objects or information. We call the organizing structure a data structure.
4
Types of Lists In Alice, a list can be a list of numbers or a list of objects, colors, or other type…. In this session, we will use an example of a list of objects.
5
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.
6
Initial World First, create an initial world with five Rockette objects. The chorus line performs many dance movements in coordination. This is an animation where a list of objects would be useful.
7
Creating a list: Step 1 After the objects have been added to the initial world, a list can be created. The first step in creating a list is to create a new world- level variable. (Why should it be world-level rather than class-level?)
8
Creating a list: Step 2 In the dialog box, type in a name select Object type check “make a List” box click new item button 5 times to specify each of the 5 rockettes (each item we wish to include in the list must be added to the list)
9
Programming with a list Now that a list has been created, how do we make use of it 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." Iteration can be done in order (one at a time) all together (simultaneously)
10
Iterating through a list The two iterative loops: in order (one at a time) all together (simultaneously)
11
Example: Iteration in Order A typical chorus line dance move involves each dancer (one after the other) kicking up their right leg. Possible storyboard: For all dancers in order item_from_dancers kickUpRightLeg kickUpRightLeg Parameter: whichRockette Do in order Do together whichRockette right thigh turn back whichRockette right calf turn forward whichRockette right calf turn back
12
kickUpRightLeg The whichRockette parameter allows you to pass in the specific rockette who is performing the action.
13
Demo: Calling the method with a list What would happen if rockette4 had been first in the list? Try it out!
14
Demo: Iterating all together Note that the kickUpRightLeg method is still the same. All we did was call the method from within a For all together iterative loop.
15
Lists wrap-up What are the differences between For all in order and For all together? What are examples of cases where you might want to use For all in order? What are examples of cases where you might want to use For all together?
16
Assignment Read Chapter 9-1, Lists Lab 9-1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.