Download presentation
Presentation is loading. Please wait.
Published byGriselda Wilkinson Modified over 9 years ago
1
Alice in Action with Java Chapter 5 Lists and Arrays
2
Alice in Action with Java2 Objectives Use a list to store multiple items Use Alice’s forAllInOrder and forAllTogether statements Use random numbers to vary the behavior of a program Use an array to store multiple items
3
Alice in Action with Java3 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 and shrink –array: container whose size remains fixed at run-time Benefits of using variables that store multiple values –Reduces amount of code that needs to be written –Makes a program more readable Example: a variable playList that holds 12 songs –Pass all 12 songs to a method using one parameter
4
Alice in Action with Java4 Lists and Arrays (continued)
5
Alice in Action with Java5 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 bee s and 1 queenBee Manipulating 12 individual bees leads to inefficiencies Adding 12 bees to a list simplifies implementation –Form: Object[]bees = bee, bee2,... bee12; Defining 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
6
Alice in Action with Java6 List Example 1: Flight of the Bumble Bees (continued)
7
Alice in Action with Java7 List Example 1: Flight of the Bumble Bees (continued)
8
Alice in Action with Java8 List Example 1: Flight of the Bumble Bees (continued) Processing list entries –Drag the forAllInOrder statement to editing area –Drop forAllInOrder, select expressions bees –Construct move() using bee2 as a placeholder –Drag item_from_bees onto the placeholder and drop –Send turnToFace() message to the queenBee –Send say() message with command to queenBee Simulated behavior –queenBee turns to each bee and orders it to rise –Each bee responds by rising upward 5 meters
9
Alice in Action with Java9 List Example 1: Flight of the Bumble Bees (continued)
10
Alice in Action with Java10 List Example 1: Flight of the Bumble Bees (continued)
11
Alice in Action with Java11 List Operations forAllInOrder statement –Used to sequentially perform actions on list items forAllTogether statement –Used to simultaneously perform actions on list items –Example: cause all bee objects to take off at once Each item in list is accessed by index (position) Messages can be sent to an entire list or to list items –Example 1: aList.clear(); (removes all items) –Example 2: aList.add(0, 1); (inserts 1 at list head)
12
Alice in Action with Java12 List Operations (continued)
13
Alice in Action with Java13 List Operations (continued)
14
Alice in Action with Java14 List Operations (continued)
15
Alice in Action with Java15 List Operations (continued) Alice provides functions for performing list operations A few list functions –aList.size() : the number of items in aList –aList[i] : the value item i in aList –aList.getRandomItem() : random value in aList Using list functions –Drag a list definition into the editing area –Drop list onto placeholder with function’s return type –Alice will display a list of messages to choose from
16
Alice in Action with Java16 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
17
Alice in Action with Java17 List Example 2: Buying Tickets (continued)
18
Alice in Action with Java18 List Example 2: Buying Tickets (continued)
19
Alice in Action with Java19 The Array Structure Fixed-size container that can store a group of values –Data items are accessed by position (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 –The array is more memory-efficient than a list –An array is more time-efficient for accessing an item
20
Alice in Action with Java20 Array Example 1: The Ants Go Marching User story: soldier ant sings a song while marching View the story as a counting problem –The ant song is 10 verses long –Most of the lines are repeated –A for loop can be used to iterate through 10 verses Managing the differences in lines using parallel arrays –One String array stores the verse-specific numbers –Another String array stores verse-specific ant actions –Values at i th index of array are accessed with each loop Enhance the program by adding a marching method
21
Alice in Action with Java21 Array Example 1: The Ants Go Marching (continued)
22
Alice in Action with Java22 Array Example 1: The Ants Go Marching
23
Alice in Action with Java23 Array Operations Array items are accessed by index (like 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 –forAllInOrde r and forAllTogether not available
24
Alice in Action with Java24 Array Operations (continued)
25
Alice in Action with Java25 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
26
Alice in Action with Java26 Array Example 2: Random Access (continued)
27
Alice in Action with Java27 Array Example 2: Random Access (continued)
28
Alice in Action with Java28 Array Example 2: Random Access (continued)
29
Alice in Action with Java29 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
30
Alice in Action with Java30 Alice Tip: Using the partNamed() Function (continued)
31
Alice in Action with Java31 Alice Tip: Using the partNamed() Function (continued)
32
Alice in Action with Java32 Alice Tip: Using the partNamed() Function (continued) Components of an Alice Object are Object s 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
33
Alice in Action with Java33 Alice Tip: Using the partNamed() Function (continued)
34
Alice in Action with Java34 Summary Data structure: structure storing a group of values List: dynamic container that holds a group of values Array: static container that holds a group of values Item: individual variable stored by index in an array or list forAllInOrder statement: loops through list items in sequence
35
Alice in Action with Java35 Summary (continued) forAllTogether statement: applies instructions to list items in parallel Random.nextDouble() : generates random numbers Components of an Alice Object are Object s partNamed(component) : retrieves a part of an object Alice usually displays to indicate that an object value is null
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.