copyright 2008 wanda dann, steve cooper, don slater alice workshop variables & conditions
TRANSCRIPT
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Alice Workshop
Variables & Conditions
Copyright 2008Wanda 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)
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Instance variables
In Alice, an object’s properties are instance variables
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Changing a property
A property can be changed, using set
Copyright 2008Wanda 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.
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
New variables
Properties are built-in variables, but we can create our own variables to track other values
Copyright 2008Wanda 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.
Copyright 2008Wanda 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
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Random distance
World function to generate a random number in a range of 0.25 to 3
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Checking a Condition
How do we know whether junior is in the water?
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Is junior in the water?
Copyright 2008Wanda 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
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Creating a variable
A variable has a name and a type
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Set variable value
Call a function to set the value of d
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Use Math to compute radius
Cascading menus for binary operations
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Conditional Execution
Alice provides an If/Else control structure to check a condition
Copyright 2008Wanda 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
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
If/Else Statement
Create parameter, targetObjPull if/else control into editor
Copyright 2008Wanda 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
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Select placeholder operands
Copyright 2008Wanda 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
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Demo
Implementation of if/else
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Completed Code
Copyright 2008Wanda Dann, Steve Cooper, Don Slater
Text
Chapters 3 & 6 Conditions and If/Else
Chapter 10 Variables