07-ListsAndIterationInAlice.ppt: uploaded 1 April 2016 at 4:01 pm

Download Report

Transcript 07-ListsAndIterationInAlice.ppt: uploaded 1 April 2016 at 4:01 pm

Lists and Iteration in Alice
Stephen Cooper
Wanda Dann
Randy Pausch
Barb Ericson
Oct 2009
ListsAndIterationInAlice
1
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
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.
• 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
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 known as a data
structure
– Way to structure our data
ListsAndIterationInAlice
4
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
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.
ListsAndIterationInAlice
6
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
7
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
8
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
9
Iterating through a list
• Iteration can be done in two ways:
– in order (one at a time)
– all together (simultaneously)
ListsAndIterationInAlice
10
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
For all dancers in order
item_from_dancers kickUpRightLeg
Do in order
Do together
whichRockette right thigh turn back
whichRockette right calf turn forward
whichRockette right calf turn back
ListsAndIterationInAlice
11
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
12
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
13
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
14