Chapter 6 part 1

Class-level Methods
Chapter 6 part 1
Classes and Objects

Classes
o

In Alice, classes are predefined as 3D models
Objects
o
An object is an instance of a class
Class: Frog (Uppercase first letter)
Objects: frog, frog1, frog2, frog3
(lowercase first letter)
2
Methods

Built-in (predefined) methods
o

Examples: move, turn to face, say
Custom methods
o
o
User created method
Does something that was not predefined in Alice
3
Class-level Method




Is specific to a class of objects
We can give a class new abilities/methods
Only involves this one class level object
Examples
o
o

A person walking
A skater skating
Unlike world class-level methods
o
Which has access to multiple classes
4
Example


Skater object does not have a skate method
To create a skate method for ice skater objects
We need to:
(1) Tell Alice to associate a new
method with iceSkater class
(2) Write a new method to
animate ice skater skating
5
Associate Animation with Skater
• Select iceSkater tile in
Object Tree
• Select methods tab in
details panel
• Click on create new
method button
6
Algorithm for skate Method
skate:
Do together
move skater forward 2 meters
Do in order
slide on left leg
slide on right leg


Each slide action requires several motion
instructions
These actions need to be broken down into
smaller steps using stepwise refinement
7
Stepwise Refinement

Process of breaking problem into smaller tasks
o


Makes large task more manageable
Break each task into simpler steps
Once algorithm is completed write a method for
each task
8
Stepwise Refinement For skate
skate:
Do together
1) move forward 2 meters
2) Do in order
slideLeft
slideRight
Refinement of slideLeft
Do in order
Lift right leg and turn upper body forward
Lower right leg and return body upright
Refinement of slideRight
Do in order
Lift left leg and turn upper body forward
Lower left leg and return body upright
9
Alice Programs for skate
Algorithm

Notice how skate is
preceded by icekater
instead of world like
World.my first method
10
Demonstration

Concepts illustrated
o
o

Method defined for a specific type of object defines
action for that object
A method can call other methods
skate method calls slideRight and slideLeft
11
Class Methods Allow Reuse


Writing methods that make ice skater perform
skating motion is a complex task
Would like to reuse iceSkater skate in other worlds
without writing methods again
12
Creating A New Class
1) Rename iceSkater as
cleverSkater either
a) Double click object
name
b) Or right click name
2) Right click name to
save as a new class
3) Alice saves new class
as CleverSkater.a2c
• Alice v2 Class
13
Importing CleverSkater

An instance of the CleverSkater class can be
added to a new world
o
o
o
Use File | Import
Set File Type to A2C
Choose class file to import
14
Interacting With Other Objects


Suppose you want to write a class-level method
where another object is involved?
Ex: a method to make skater skate around
another object, like the penguin in this scene
o
It can be anything, like lake, cone, etc.
15
Parameters


Built-in methods give flexibility by providing
parameters like distance and direction
Parameters allow values (arguments) to be
passed to methods
o
Example




Parameters: direction, distance, duration
Arguments: up, 0.5 meters, 0.5 seconds
Where duration is optional
Alice provides several kinds of parameters that can be
used in custom methods
o
Number, Boolean, Object, Other
16
Parameters in Class-level Method


Solve skate around object by writing a class-level
method with an object parameter
Allows you to pass a specific object
cleverSkater.skateAround
Parameter: whichObject
Do in order
Do together
cleverSkater turn to face whichObject
cleverSkater lift right leg
cleverSkater move to whichObject
cleverSkater turn around whichObject
17
Parameters Are Placeholders


A value is passed to a method with an argument
Parameters act as placeholders for the argument
o
o

Takes value of argument being passed to method and
uses it in method using parameter’s name
Parameter name is usually different than argument
Example: in move method
o
o
You pass arguments that are specific direction and
distance to move (ex: up, 0.5 meters)
Those specific arguments are passed to the move
method’s parameters (direction and distance)
18
Creating Parameters



Create parameter using create parameter
whichObject is passed as Object parameter
Notice how skate is preceded by iceskater instead of
world like World.my first method
19
Guidelines

To avoid potential misuse of class-level methods
follow these guidelines
o
Avoid references to other objects

o
Avoid calls to world-level methods

o
Use parameters if referencing another object is required
Will not be saved with new class you created
Play a sound only if sound has been imported and
saved out as part of new class
20
Demonstration

Concept illustrated
o
Parameter whichObject is placeholder for the object
value passed to it

Ex: penguin
21
Class-Level Variables as Properties
 A property is a variable that belongs to an object
 Properties can be added to an object through the


creation of class-level variables
When the object is saved as a new class the
variables are saved with it
Common properties are
o
o
o
color
opacity
isShowing
22
Tutorial 6-5: Turn Monitor On/Off


A new property is added to monitor: is monitor on
or off?
Create class-level variable that keeps track of state
of computer monitor: on or off
On
Off
23
Algorithm to Turn Monitor On/Off


If monitor is off then turn it on
If it is on then it will turn it off
turnOnOff
Do in order
If monitor isOn
set screen color to black
set isOn variable to false
Else
set screen color to no color
set isOn variable to true
Endif
24
Algorithm Translated to Alice

Notice how new method is preceded by monitor not world
o
o
o
Means it is a class-level method not world-level
Will be saved if class is saved
If world is saved the class will not be saved for future use in other
programs

Can only use this in this new method in this specific program

notice variable is preceded by monitor
25
Using turnOnOff Method


Variable monitor.isOn is set initially to true
In world.My first method
o
Turn off monitor using turnOnOFF

o
o
Variable monitor.isOn is now set to false
Wait 1 second to see effect
Turn on monitor using turnOnOFF
26
Homework




Read chapter 6 sections 1 - 5
Do tutorials 1 – 5
Do exercise in handout
Due one week after assigned with 1 week grace
27