Download presentation
Presentation is loading. Please wait.
Published byBarrie Ross Modified over 9 years ago
1
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions
2
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Recall: Variable A variable is an item or value that is likely to change in value. Variables can belong to: a specific method (local to that method) a class (instance, available to all objects of that class) the world (global, available anywhere in the program)
3
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Instance variables In Alice, an object’s properties are instance variables
4
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Changing a property A property can be changed, using set
5
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Condition The set of of variable values describe the condition of objects in an animation program. As an animation program is running, each statement is likely to change the value of at least one property of an object. Therefore, conditions change as a program runs.
6
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater New variables Properties are built-in variables, but we can create our own variables to track other values
7
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Example Problem A penguin (joe) is watching his son (junior) slide across the ice. A hole in the ice is nearby where junior could fall in. Create an animation where junior slides a random distance toward the water. If junior lands in the water, have him sink under the water and then joe dives in after him.
8
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Textual Storyboard Do in order junior faces the water junior slides forward a random distance if junior is in the water junior goes down under the surface joe goes in after him Do together joe and junior pop back up to the surface
9
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Random distance World function to generate a random number in a range of 0.25 to 3
10
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Checking a Condition How do we know whether junior is in the water?
11
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Is junior in the water?
12
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Problem Solving Variables can be used to represent values Let d be the distance of junior to the water Let r be the radius of the water
13
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Creating a variable A variable has a name and a type
14
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Set variable value Call a function to set the value of d
15
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Use Math to compute radius Cascading menus for binary operations
16
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Conditional Execution Alice provides an If/Else control structure to check a condition
17
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Boolean Condition Is junior’s distance to the center of the water less than the radius of the water? Must be either true or false
18
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater If/Else Statement Create parameter, targetObj Pull if/else control into editor
19
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Relational operations are defined in World’s functions Alice operators ◦ == means “is equal to” ◦ != means “is not equal to” Operators
20
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Select placeholder operands
21
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Placeholders In this example, the operands selected from the cascading menus are just placeholders. Need to replace with calls to functions to obtain distance of penguin to center of water
22
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Demo Implementation of if/else
23
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Completed Code
24
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Text Chapters 3 & 6 Conditions and If/Else Chapter 10 Variables
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.