Alice in Action with Java Chapter 5 Lists and Arrays.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Events Chapter 7. Interactivity The real world is interactive User determines order of actions instead of programmer.
Events Chapter 7. Interactive Real world is interactive User determines order of actions instead of programmer.
Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.
Repetition Structures
Chapter 10 Introduction to Arrays
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Alice in Action with Java
Alice in Action with Java
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Alice in Action with Java
Chapter 9 Introduction to Arrays
Alice in Action with Java
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running o Stored in.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Programming Logic Program Design. Objectives Steps in program development Algorithms and Pseudocode Data Activity: Alice program.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Alice in Action with Java Chapter 6 Events. Alice in Action with Java2 Objectives Create new events in Alice Create handler methods for Alice events Use.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
By Melissa Dalis Professor Susan Rodger Duke University June 2011 Multiplication Table.
Chapter 6: The Repetition Structure
Flow Control: Repetition with For Loops (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Flow Control: boolean expressions, “if” selection statements (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September.
Mathematical Expressions, Conditional Statements, Control Structures
Events (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Flow Control: Repetition with While Loops (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Alice in Action with Java Chapter 4 Flow Control.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture August 2012.
Alice in Action with Java Chapter 4 Flow Control.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Alice in Action with Java Chapter 1 Getting Started with Alice.
Functions (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Alice in Action with Java Chapter 2 Methods. Alice in Action with Java2 Objectives Build world-level methods to help organize a story into scenes and.
Repetition Control Structures
Alice in Action with Java
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Alice in Action with Java
Alice in Action with Java
Dynamic Data Structures and Generics
Object Oriented Programming in java
MSIS 655 Advanced Business Applications Programming
An Introduction to Programming Using Alice 2.2, Second Edition
Arrays.
Presentation transcript:

Alice in Action with Java Chapter 5 Lists and Arrays

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

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

Alice in Action with Java4 Lists and Arrays (continued)

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

Alice in Action with Java6 List Example 1: Flight of the Bumble Bees (continued)

Alice in Action with Java7 List Example 1: Flight of the Bumble Bees (continued)

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

Alice in Action with Java9 List Example 1: Flight of the Bumble Bees (continued)

Alice in Action with Java10 List Example 1: Flight of the Bumble Bees (continued)

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)

Alice in Action with Java12 List Operations (continued)

Alice in Action with Java13 List Operations (continued)

Alice in Action with Java14 List Operations (continued)

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

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

Alice in Action with Java17 List Example 2: Buying Tickets (continued)

Alice in Action with Java18 List Example 2: Buying Tickets (continued)

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

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

Alice in Action with Java21 Array Example 1: The Ants Go Marching (continued)

Alice in Action with Java22 Array Example 1: The Ants Go Marching

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

Alice in Action with Java24 Array Operations (continued)

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

Alice in Action with Java26 Array Example 2: Random Access (continued)

Alice in Action with Java27 Array Example 2: Random Access (continued)

Alice in Action with Java28 Array Example 2: Random Access (continued)

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

Alice in Action with Java30 Alice Tip: Using the partNamed() Function (continued)

Alice in Action with Java31 Alice Tip: Using the partNamed() Function (continued)

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

Alice in Action with Java33 Alice Tip: Using the partNamed() Function (continued)

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

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