class-level methods chapter 6 part 1. classes and objects classes o in alice, classes are predefined...
TRANSCRIPT
Class-level Methods
Chapter 6 part 1
Classes and Objects
Classes o In Alice, classes are predefined as 3D models
Objectso 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) methodso Examples: move, turn to face, say
Custom methodso User created methodo 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 A person walkingo A skater skating
Unlike world class-level methodso 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
Each slide action requires several motion instructions
These actions need to be broken down into smaller steps using stepwise refinement
skate:Do together move skater forward 2 meters Do in order slide on left leg slide on right leg
7
Stepwise Refinement Process of breaking problem into smaller
taskso 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 Refinement of slideLeft
Do in order Lift right leg and turn upper body forward Lower right leg and return body upright skate:
Do together 1) move forward 2 meters 2) Do in order slideLeft slideRight
Refinement of slideRightDo 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 illustratedo Method defined for a specific type of object
defines action for that objecto 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 Class1) Rename iceSkater as
cleverSkater eithera) Double click object
nameb) 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 worldo Use File | Importo Set File Type to A2Co 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 sceneo 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 methodso 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 methodso 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 Takes value of argument being passed to method and uses it in method using parameter’s name
o Parameter name is usually different than argument Example: in move method
o You pass arguments that are specific direction and distance to move (ex: up, 0.5 meters)
o 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 guidelineso Avoid references to other objects
Use parameters if referencing another object is required
o Avoid calls to world-level methods Will not be saved with new class you created
o 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 coloro opacityo 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 Off23
Algorithm to Turn Monitor On/Off If monitor is off then turn it on If it is on then it will turn it off
24
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
Algorithm Translated to Alice Notice how new method is preceded by monitor not world
o Means it is a class-level method not world-levelo Will be saved if class is saved o 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 Variable monitor.isOn is now set to false
o Wait 1 second to see effecto 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