MCS-170 The Nature of Computer Science
Alice Lecture 2
1. Object-Oriented Programming in Alice
- Overview
- Classes, Objects, Methods
- Program Code
- Functions
- Expressions
- Classes, Objects, Methods
- Complex programs can have thousands, even millions of lines of
code - How can a programmer possibly manage this? -- They break the
program into manageable sub-units or pieces.
- In Alice, these pieces are called Classes, Objects, and Methods
- A Class
is a category of objects. For example, we have the Bunny class or the Frog class. Classes appear in
the Gallery at the bottom of the screen. A Class might be thought
of as a potential object, or the blueprint for an object.
- An Object
is a particular instance of a Class. When we click
on the Bunny
class and a Bunny appears in the video editing screen, we are creating
a particular Bunny Object in memory and on the screen. We call
this instantiating
the Class.
- Alice Example:
Create three different Penguin classes, with different attributes.
- Note: All Objects within a
Class share the same set of basic attributes, methods, etc, that are
defined for that class. We can also define special attributes for a
particular object.
- A Method
represents an instructions (or set of instructions) that an Object can
carry out.
- All Alice objects share a
basic set of methods: move, turn, etc.
- These methods can be
grouped into other methods to help organize tasks.
- Alice Example:
For one penguin, create a new method called JumpGlide, which has
penguin jump into the air and then glide forward.
- World-Level
Method
- World is a
unique Class - it represents the entire world of your animation. Only
one instance is ever created of the World.
- Unique method -- World.my first
method This method is executed when you start
playing the scene. (Look in Events window) To create action, we
modify this method.
- Alice Example:
In our example, above, call the new penguin method from World.my first
method, then play scene.
- Program Code - code used to
control action
- In Alice, we control characters on the screen through a
combination of several types of code:
- Instructions:
Statements that make objects carry out specific actions
- Control
Structures: Statements that control blocks of instructions
- Do in Order, Do Together, If/Then, Loops, etc
- Functions:
Statements that ask questions about an object or compute values
- Object is above ____, Object distance to ________
- Expressions:
Math operations on numbers or other values
- Functions
Every
object has a list of instructions (methods) it can carry out, and a
list of functions that it can use to gain information on its
relationship to other objects.
Functions for an object are listed
in the lower left window of Alice (one of the three tabbed panels in
this window -- the other two showing methods and properties)
- Example: suppose we
have a kangaroo named "joey" and a chair in our scene and we want to
move the kangaroo to the chair. Typically, we will not know
precisely where these objects are situated. But, we can use a function
to compute the distance between these two objects.
- "joey move" "amount = joey distance to chair" toward "chair"
- Alice
Implementation
Expressions
- Expressions are mathematical combinations of values, created
using "+","-","*", and "/".
- Example:
In the preceding Alice example, joey passed into the chair, which is
not too realistic. Suppose we want to move him so that he is just
beside the chair back? We can move him toward the chair back, but
subtract a bit from the motion.
- "joey move" "(amount = joey distance to chair) - 0.5" toward
"chair.back"
- Alice
Implementation