The built-in distanceto function determines the distance from the center
of one object to the center of another object
The function call (use of a function) may be placed where a value is expected
not where a statement (action) can appear
the Alice editor will not let you place a function call in a
statement position
most often, as in this demo, the value returned by a function call is a
method argument
Solving a collision problem
The mummy collides with the pharaoh because the distance between two
objects is measured center-to-center
One way to avoid a collision is to subtract a suitable amount from the
distance using a math expression
Demo: subtracting a literal value
We first try moving the mummy forward by an amount that is computed by
subtracting from the distance to the pharaoh a literal amount (2) given in
the program
Now the expression gives a close estimate of the distance to move
Help -- This is getting complicated!
Problem
I need a way to organize what I'm learning about a programming language,
so I can better remember it.
The Big Picture:
Knowledge about programming languages is organized into:
Syntax: specifies what forms programs can take, so you can
know what kinds of programs are possible
avoid writing illegal programs
have terms for talking about the parts of programs
broken down into syntactic elements, linke the "parts of speach" you
learned in English classes
Semantics: determines the meaning (or behavior) of legal programs
each syntactic element has an associated semantics
Major kinds of syntactic elements
Statements, which perform actions
your Alice text uses the less common, but more expressive, term instruction
Expressions, which return values
Most computer languages have similar kinds of syntactic elements with
similar meanings
Kinds of Alice statements used so far
Do in order
Do together
method calls, such as pinkpenguinsay"Yikes!"
comments (do nothing)
More kinds (control statements) coming this week
Kinds of Alice expressions used so far
literals, which indicate a particular value directly in the program text
for example, numbers, strings (such as "Yikes!") and option choices (such as forward)
function calls
numeric operations, with the following numeric operators,
addition +
subtraction -
multiplication *
division /
Operators
Most operators have both left and right sub-expressions whose values are
the operators arguments
In Alice, to introduce a numeric operator, select math from the argument value
pull-down options, with the appropriate right-hand sub-expression
usually you need to later change the default sub-expression to the left
of the operator
Simple Control Structures in Alice
Control statements
Control statements control the order in which statements are executed
seen so far: Doinorder and Dotogether
Next we introduce control statements for
conditional execution
repetition
Conditional execution
In conditional execution a condition is checked that determines whether a
block of statements will be executed
This allows programs to be smart about what they do, for example in
games
simulations
real-time controls, e.g. robot systems
Example
As a simple example of conditional execution, let's revisit the Egyptian scene
We want to check if the mummy is taller than the pharaoh, performing one
action if the condition is true, and another if it is false
We also want to do something only if the mummy is taller than a specified amount
If/Else
In Alice, an If/Else control statement is used to check a condition and
make a decision
Flow diagrams, such as this one, are helpful to visualize flow of control
Storyboard
In our Egypt scene, we can demonstrate which object is the tallest by
having that object turn around, and then have the mummy jump if it is
taller than 2 meters
We can design this sort of program with a textual "storyboard"
note that indentation is used critically to separate the program elements
In this program the condition expression of the first If/Else statement
is a function call that returns a Boolean (true or false) value
The conditional expression of the second If/Else is a relational operator
that compares two values
DoNothing in the Else part of the statement means that if the
condition is false, Alice will skip this part and go to the next
instruction (if any)
Relational operators in Alice
Relational operators are found in the World's built-in functions
When you drag an operator into a program, pop-up menus provide options of
the left (a) and right (b) sub-expressions
if no option is what you want, pick one and replace it later with
another drag-and-drop
Type checking
Only certain types of values make sense in most circumstances
for example
a conditional expression must have a Boolean value
a distance argument must be a number
if the type of a value makes no sense, there is said to be a type error
In Alice, the editor prevents most type errors by not allowing you to
drop an expression where it would have the wrong type (or where no
expression is allowed)
the box around a dragged program element only turns green when it is
over a place where it may be dropped
So what kind of types do I need to know about in Alice?
Common types of Alice values, with examples
number: 1.52
boolean: true and false
string: "Oh,Yeah!"
object: penguin, helicopter, etc.
General If/Else syntax
If/Else is a kind of statement
it can be used wherever a statement is allowed
The conditional part of an If/Else must be an expression of type Boolean
The then and Else parts of an If/Else may be any block of statements
the then part is between the If and Else lines
blocks are Do in order, except just inside a Do together
In Alice actions are performed by
statements
expressions
relational operators
A and B
all of the above
Answer: A
A literal is used to
test if a condition is always true
print program values
introduce comments in a program
indicate a value particular value in the program text
Answer: D
Which of the following yield a value?
statement
method call
expression
function call
C and D
all of the above
Answer: E
Monster steps
Now let's write code to make the mummy "walk"—a somewhat stilted
motion like you would see in an old Hollywood horror film
The code will be more complex because we need to
alternate left and right leg motions
coordinate leg motions with the body moving forward
The loop statement is a control structure that provides for repeatedly
executing its body (a statement or block of statements)
The Alice loop statement repeats its body a fixed number of times
soon we will learn about the Alice while statement, which is a more advanced
kind of loop that executes its body as long as its condition is true
Organizing larger programs
Structure of Object-oriented programs (review)
Textually, object-oriented programs are composed of classes, which
contain methods
At runtime, object-oriented programs manipulate objects that are
instances of classes
Classes and objects in Alice programs
Alice classes represent 3D models, such as the class Frog
class names begin with uppercase letters
Objects, such as frog, frog1, frog2, frog3
object names begin with lowercase letters
Alice methods we have used so far
Built-in (predefined) methods
examples: move, turntoface, say
World.myfirstmethod
all code we have written so far has been in the body of the method
named my first method in the class World
this method is called automatically when you start (Play) an Alice program
Managing large programs
As program functionality increases, so does the size of the code
"Real world" software applications often have many millions of lines of code
programs written by non-professional programmers may contain thousands
of lines
Problem: a method over about a page long are difficult to grasp
example: last week's penguin program
Solution: break it down into chunks that can be written and understood independently
The BIG picture of the BIG problem of BIG programs
The biggest problem of programming is managing so much complexity!
The most important technique for managing complexity is divide and
conquer: reduce a large problem to a number of smaller problems that can
be solved individually
Divide and conquer
Problem: computer programs are too complex to get your head around all at once!
Solution: break down each major task into simpler parts to solve them
independently
this is called divide and conquer
example: scenes in a storyboard
Using the divide and conquer strategy repeatedly to break a problem down
into larger tasks and then breaking each task down into simpler ones is
called stepwise refinement
example: we write a method for each scene in the last storyboard
Refactoring
Sometimes we don't discover that things are too complicated for one
method until they already are!
Then it's time to modify the program (even though it already works) so
that it is more readable.
FYI: the fancy name for this is refactoring
Example of need for refactoring
Recall the penguins.a2w program from last week. It works, but the style is
not good for it is too complicated for one method.
New methods are define, named jumponwhitepenguin and diveoff
Each new method is a world-level method because it is defined as a method
for World and has instructions that involve more than one object
(white penguin, pink penguin, camera)
The new methods are executed by calling (invoking) them in World.myfirstmethod
Exercise: try to abstract the sinking code in its own method
Creating a new method in Alice
To associate the new method with the "World", select World in
the object browser
Select the methods tab in the details area
Click on the create new method button
TWO reasons for defining methods
Methods are used for
abstraction, which improves program readability and maintainability
as in our last example
code reuse, which eliminates code duplication, which makes programs
smaller (and thereby easier to read, and usually more efficient)
coming soon...
These reasons for using methods apply as well to functions and procedures
Abstraction
We have seen that defining methods allows us to practice stepwise
refinement and think at a higher level
can think diveoff instead of "pitch forward (rotating about the
white penguin's feet), while simultaniously raising wings and facing the camera"
The technical term for "think at a higher level" is abstraction
Code reuse
When some code need to be performed at more than one place in a program,
abstract the code by putting it in a method
then you call, or reuse, the method from each place in the program
where the action is needed
This reduces code size and makes programs easier to understand and
maintain
Extensive libraries of reusable code can be constructed in this way
If the code needed at various points in a program is not the same, but is
"similar enough", it can still be abstracted using method parameters
(arguments)
The turn-step pattern in zombieWalk.a2w is repetitious, but the
repeated patterns are not identical: they differ by the amount and
direction of the turns
A better solution is to abstract the repetition
but this cannot be done using a simple method of the kind we saw last week
We can abstract this repetition by abstracting the difference (the
direction or amount of turn) along with the code, by introducing a method parameter
We extend the functionality by also adding parameters for sound and
turning to specified objects
Creating an Alice parameter
Create the new method
Click the create new parameter button in the method editor tab (upper right)
In the dialog that appears, name the parameter and select its type
Terminology: Parameter and arguments
A parameter is a variable declared in a method or function definition
An argument is a value that is passed to a method or function when it
is called and bound to (put in) the corresponding paramter
Though it is important to understand the difference between a variable and the value
bound to it, in practice these terms are often used interchangably
A function or method call subexpression (a piece of the program) that
evalusates to an argument (a runtime value) are not the same, but again
they are so closely related that the term argument is often used for both
A parameter object as an argument
To use a parameter object as an argument, select it in the expressions
option of the drop-down argument menu
Alice sounds
Sounds may be stored in any Alice object
some Alice objects come with sounds
look for them at the bottom of the properties object details tab
Alice comes with a small set of sounds in the subdirectory
\Required\Sounds of the directory in which the Alice system is installed
You can record sounds using Alice
the computer must have a mic and the right audio setup
You can import into Alice objects sounds recorded or (legally!) obtained
elsewhere
Alice parameter types
The Create New Parameter dialog allows you a number of options for the
parameter type:
Number,Boolean,Object,Sound,Color,TextureMap,Pose,Position,OrientationPointOfView,ReferencePframe,Transformable,Light, and
Direction
or List or Array of any of these types, but that's an advanced feature
The type of the parameter determines the contexts into which it can be
used (dropped after a drag)
Parameter concepts and terminology
Parameters must be declared at the beginning of a method
when a parameter is declared it is given a name and a type, or just a
name in languages that ignore types until runtime
When parameters are referenced (referred to) in statements they act as
"placeholders" for values supplied as arguments when the method is called
Arguments are the values of subexpressions in method and function
calls that are associated with parameters
we say the argument is passed to the method and bound to the
corresponding parameter
In Alice, questions are asked primarily using elements in which object detail tab?
methods
functions
properties
any of the above
Answer: B
The best Alice control construct for performing some actions a fixed number of times is
Repeat
While
For
Loop
Answer: D
Two common reasons for defining more than one method in a program are