Introduction to Alice

Programming Concepts
Chapter 4 introduces more advanced OO programming
techniques.
Construction of a programs usually requires:
•Classes
•Objects
•Methods
•World-level
•Class-level
•Parameters
•Inheritance
Fall 2008
ACS-1805
Ron McFadyen
1
Classes
An OO program is organized around the concept of class.
For Alice programming, all classes are pre-defined for us.
We choose the classes our program requires from the gallery.
We need other skills to build classes
If interested, you would want to learn about products such as Maya, Max
Studio, …
• These products are used to construct 3D models
• Constructing or building classes is outside the scope of this
course
Two things that distinguish classes:
• Classes are defined to have properties and methods (including
functions)
Fall 2008
ACS-1805
Ron McFadyen
2
Classes
Each class has properties, methods, functions
We can customize by creating new properties, methods, functions
Fall 2008
ACS-1805
Ron McFadyen
3
Objects
Showing the relationship amongst classes and objects:
Student
Teacher
teacher1
Fall 2008
teacher2
ACS-1805
student1
Ron McFadyen
student2
student2
4
Methods
Each class/object has a collection of methods that define the things an object
from that class can do
• Move, Turn, Roll, etc
• We can’t see the code comprising these. (The Alice creators don’t
want us to change them and they are referred to as primitive methods
We can customize an object by giving it some new methods
• you are able to edit those
OO programmers typically use many many methods where each is defined to
some fairly simple thing
Methods give us a way of organizing the complexity of our creations
Methods become even more useful when they incorporate parameters
Fall 2008
ACS-1805
Ron McFadyen
5
World-level Methods
If we have a complex method, we can break it up into
smaller simpler tasks.
The main idea is to take a group of related instructions in
one method, place them in a separate method, and replace
the original lines with a call to the new method
The principle we are applying is called abstraction – we are
now thinking of the group of instructions as a single
instruction
Fall 2008
ACS-1805
Ron McFadyen
6
Stepwise refinement
When creating an algorithm we can design a solution in
terms of solutions to smaller problems. This approach is one
where we divide and conquer - we break a task down into a
number of sub-tasks, each of which is described by an
algorithm that is smaller and simpler than that for the entire
process.
Each sub-task may require further sub-division until we
have divided up the problem into elementary pieces, each of
which can be tackled in a simple and straightforward way.
Fall 2008
ACS-1805
Ron McFadyen
7
World-level Methods
Original:
Fall 2008
Code is placed in a new method:
ACS-1805
Ron McFadyen
8
World-level Methods
Original:
Revised:
• Original is long and complex
• The revised version is easily
understood
• Note how a method is called
Fall 2008
ACS-1805
Ron McFadyen
9
Parameters
A method is more useful if it can work for different objects
• A robot walks by turning a leg part backward and then
forward
• We can place these two turns into a method with a
parameter – where the parameter is used to specify the part
involved
Fall 2008
ACS-1805
Ron McFadyen
10
Parameters
Some original code:
A method with a parameter:
Fall 2008
ACS-1805
Ron McFadyen
11
Original:
Parameters
What different kinds
of parameters does
Alice allow?
Revised:
parameters
Fall 2008
ACS-1805
Ron McFadyen
12
Example
Consider Project 4, Cleanup Robot
We are told to use 3 methods… to make it easier to
understand a solution… to make it easier to develop the
solution
Put Toys Away (storyboard)
For each toy:
pickup- robot goes and picks up the toy
putInCloset- robot takes the toy to the closet
Fall 2008
ACS-1805
Ron McFadyen
solution is given in
terms of solutions to
2 smaller problems
13
Example
Parameters
pickup- robot goes and picks up the toy
Robot turns to face the toy
Robot moves toward the toy
Robot picks up toy
object; distance to move
putInCloset- robot takes the toy to the closet
Robot turns to face the closet
Closet door opens
Robot moves into the closet
putdown- robot places toy in closet
Robot exits from the closet
Closet door closes
object; distance to move
putdown- robot places toy in closet
Robot lowers his arms
Robot releases the toy
object
Fall 2008
ACS-1805
Ron McFadyen
14
Example
We can represent the overall organization of our methods
(who calls who)
Put toys away
pickup
putInCloset
putdown
Fall 2008
ACS-1805
Ron McFadyen
15