300 likes | 432 Views
Alice in Action with Java. Chapter 5 Lists and Arrays. Objectives. Learn the list and array data structures in Alice Use a list or an array to store multiple items Use Alice’s forAllInOrder and forAllTogether statements Use random numbers to vary the behavior of a program.
E N D
Alice in Action with Java Chapter 5 Lists and Arrays
Objectives • Learn the list and array data structures in Alice • Use a list or an array to store multiple items • Use Alice’s forAllInOrderand forAllTogetherstatements • Use random numbers to vary the behavior of a program Alice in Action with Java
Lists and Arrays • Some variables only store a single value • Number, Boolean, String, Object, Sound, Color • Variables that can store multiple values • list: container that can dynamically grow (add items) and shrink (remove items) • array: container whose size remains fixed at run-time Alice in Action with Java
Lists and Arrays • Benefits of using variables that store multiple values • Reduce the numbers of variables • e.g. 1 list variable for bees instead of 12 individual variables, one for each bee • Reduces amount of code that needs to be written • Makes a program more readable and compact Alice in Action with Java
List Example 1: Flight of the Bumble Bees • Scene 2: a dozen bees rise to defend queen’s honor • Set up the scene by adding 12 bees and 1 queenBee • Add 12 bees to a list simplifies implementation Object[]bees = bee, bee2,... bee12; Alice in Action with Java
List Example 1: Flight of the Bumble Bees • Define bees list variable in playScene2() • Open Create New Local Variable dialog box • Select Object type and then make a List • Click new item and add the first bee to the list • Continue adding new items until the list holds 12 bees Alice in Action with Java
List Example 1: Flight of the Bumble Bees • Processing list entries • Drag the forAllInOrder statement to editing area • Drop forAllInOrder, select expressionsbees • Construct move()using bee2 as a placeholder • Drag item_from_beesonto the placeholder and drop • Send turnToFace()message to the queenBee • Send say()message with command to queenBee Alice in Action with Java
List Example 1: Flight of the Bumble Bees • Simulated behavior • queenBeeturns to each beeand orders it to rise • Each beeresponds by rising upward 5 meters Alice in Action with Java
Example 1 Variant • forAllInOrder: sequential actions on list items • forAllTogether: simultaneous actions on list items Alice in Action with Java
List Operations • List items have indices, with the first index being 0 • Messages can affect an entire list or some list items • Example 1: aList.clear();(removes all items) • Example 2: aList.add(0, 1);(inserts 1 at list head) Alice in Action with Java
List Operations (continued) • A few example list functions • aList.size(): the number of items in aList • aList[i]:the value item iin aList • aList.getRandomItem(): random value in aList • General procedure for using list operations • Place an appropriate item into the editing area as a placeholder • Drag a list definition into the editing area and drop it onto the placeholder • Alice will display a list of messages to choose from Alice in Action with Java
List Example 2: Buying Tickets • Scene 3: line of people waiting to buy tickets • Animating the scene using the list data structure • Create a new world with a ticket box office • Line up five people in front of the ticket window • Add five people to the personList • Use nested looping to advance the line • After an outer loop iteration, the line is reduced by one • Testing the animation • Compare positions of each person after each iteration Alice in Action with Java
List Example 2: Buying Tickets (continued) Alice in Action with Java
List Example 2: Buying Tickets (continued) Alice in Action with Java
The Array Structure • Arrays: Fixed-size containers • Data items are accessed by indices (as in a list) • Values of data items can be changed at run-time • The capacity of an array cannot change at run-time • Two reasons for using an array instead of a list • An array is more memory-efficient than a list • An array is more time-efficient for accessing an item Alice in Action with Java
Array Example 1: The Ants Go Marching • User story: a soldier ant sings a song while marching • Analyze the user story • The ant song is 10 verses long ( a for loop) • In each verse, • Most of the lines are repeated • the ant number changes (from one to ten) • The action by the little ant changes (such his thumb, tie his shoe…) Alice in Action with Java
Array Example 1: The Ants Go Marching • For the two changing parts in the verses, use two parallel arrays • One String array stores the verse-specific numbers • Another String array stores verse-specific ant actions • Values at ithindex of both arrays are accessed with each loop Alice in Action with Java
Array Operations • Array items are accessed by indices (list lists) • Predefined operations for arrays • anArray[i] = val; (the subscript operation) • val.set(value, anArray[i]); • anArray.length • Two versions of the subscript operation • Write version: changes value of item i in the array • Read version: retrieves the value of item i in the array • Traverse an array using a for loop or while loop • forAllInOrder and forAllTogethernot available Alice in Action with Java
Array Operations (continued) Alice in Action with Java
Array Example 2: Random Access • Scene: left castle door tells jokes to right castle door • Elements used to program random knock-knock jokes • Castle interior with camera positioned before doors • Parallel arrays for accessing name and punchline • Dialog including name and punchline arrays • Use of Random.nextDouble()to generate index • Random.nextDouble()details • Accessed in the functions pane of world’s details area • Set integerOnly attribute to true • Define a range of random values for indexing the array Alice in Action with Java
Array Example 2: Random Access (continued) Alice in Action with Java
Array Example 2: Random Access (continued) Alice in Action with Java
Array Example 2: Random Access (continued) Alice in Action with Java
Alice Tip: Using the partNamed() Function • Scene: fairy queen proceeds to her throne • One way to structure the playScene1()method • A fairy announces, “Her majesty, the Queen!” • As queen enters court, courtiers turn to the camera • Three actions then occur in parallel • The queen moves 13 meters forward • The queen’s wings flap • As queen passes, courtiers turn and bow in sequence • Queen turns ½ revolution to face her courtiers • The queen says, “Please rise.” • The courtiers simultaneously turn to queen and rise Alice in Action with Java
Alice Tip: Using the partNamed() Function (continued) Alice in Action with Java
Alice Tip: Using the partNamed() Function (continued) Alice in Action with Java
Alice Tip: Using the partNamed() Function (continued) • Components of an Alice Object are Objects • partNamed(component): retrieves an object part • null:value denotes that an object does not exist • If a part is not present, partNamed()returns null • Attempt to get part xyz from OliveWaterBlossom • Function returns null because part does not exist • If method is sent to null part, error message displays • Error message due to null often displays as <None> Alice in Action with Java
Alice Tip: Using the partNamed() Function (continued) Alice in Action with Java
Summary • Data structure: structure storing a group of values • List: dynamic container that holds a group of values, of changeable group size at runtime • Array: static container that holds a group of values, of a fixed group size at runtim • Item: individual variable stored by index in an array or list • forAllInOrder statement: loops through list items in sequence Alice in Action with Java
Summary (continued) • forAllTogether statement: applies instructions to list items in parallel • Random.nextDouble():generates random numbers • Components of an Alice Object are Objects • partNamed(component): retrieves a part of an object • Alice usually displays <None>to indicate that an object value is null Alice in Action with Java