CompSci 4Chap 10 Nov 22, 2005Prof. Susan RodgerNote: thanks to Wanda Dann and Steve Cooper for slide ideasAnnouncements• We will continue to work with arrays next week.• Assignment 7– Storyboard due today– World is due Thursday next week.Research Study Continued• Your choice whether or not to participate• You will be emailed your id number from Camelia Pearson Eaves (LSRC D230)• I do not know your number or whether you choose to participate• Benefit – Take a survey (test) that may help in preparing you for the final exam.What we will do today• Lecture on Chap 10– Review of variables– Introduce arrays of objects• Classwork – practice with arraysReview: Properties•Where is the class?•Where is the object?• A class defines properties• When an object is created it receives is own set of propertiesState and Changing State• State of object –each property stores info about the object–Example:• vehicle• isShowing• State changeClass-level Variables• New variables can be added to the properties of an object– class-level• The value of the variable can be changed– The variable is mutable.– Can be used to track state changes.Inheritance• If an object (and its new variable) are saved out and given a new name, a new class is created.– This is inheritance!– The new class inherits all properties and methods of original class.• Did this before by adding new methods to a class and saving it out.Examples in book• Switch– Add property “isOn”– Boolean type property• Steerable Car– Add property “direction” for amount of turn on front wheels, from –10 to 10– Number type propertyWhat is an array?•An array is a collection of objects or information organized in a specific order• The individual components (elements) are of the same type (all object or all number, etc.)• Analogy – Music CD– Collection of songs listed in order– CD player allows you to• Play songs in order• Play songs by specifying its number• Play songs in random orderArrays in Alice• In Alice, array is a data structure to organize objects or information• An array is not visible, it is a way of organizing• But….– Alice has a 3D model to help you “see” the arrayExample – Create a visualization of an array of people• Add 5 people to the world• Add an array visualization• Not an array yet, must add people to the array• Positions in array numbered starting with 0Initialize array - Add Alice to Array in position 0• Alice automatically moves to the 0 position!Add Soldier to the Array• Soldier moves automatically to position 1 (which is the 2cd position)!Add RandomGuy, Skater and Rockette• The array initialization is complete!• Set isVisible for arrayVisualization to false– Array not seenSetting elements in array• Objects in an array are called elements• Use “let” to set a position in an array• Using “let”.Accessing elements in an array• Can specify an element at a particular location in the arrayRepeat for all items in the array –in order• Use “loop” – complicated version• Note: index in loop is used in body• What does this do?• What does this do?Array vs. List• Array– Elements are ordered– Can access a particular element – 3– Use “Loop” - loop over elements – one at a time, OR every second element, etc•List– Elements are not ordered– Use “For all in order”, “For all together” – does something to each element in the list – just don’t know the order this occursClasswork Today• Introduction to
View Full Document