class-level methods chapter 6 part 1. classes and objects classes o in alice, classes are predefined...
TRANSCRIPT
![Page 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/1.jpg)
Class-level Methods
Chapter 6 part 1
![Page 2: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/2.jpg)
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
![Page 3: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/3.jpg)
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
![Page 4: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/4.jpg)
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
![Page 5: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/5.jpg)
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
![Page 6: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/6.jpg)
Associate Animation with Skater
• Select iceSkater tile in Object Tree
• Select methods tab in details panel
• Click on create new method button
6
![Page 7: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/7.jpg)
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
![Page 8: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/8.jpg)
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
![Page 9: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/9.jpg)
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
![Page 10: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/10.jpg)
Alice Programs for skate Algorithm
Notice how skate is preceded by icekater instead of world like World.my first method
10
![Page 11: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/11.jpg)
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
![Page 12: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/12.jpg)
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
![Page 13: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/13.jpg)
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
![Page 14: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/14.jpg)
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
![Page 15: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/15.jpg)
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
![Page 16: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/16.jpg)
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
![Page 17: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/17.jpg)
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
![Page 18: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/18.jpg)
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
![Page 19: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/19.jpg)
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
![Page 20: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/20.jpg)
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
![Page 21: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/21.jpg)
Demonstration Concept illustrated
o Parameter whichObject is placeholder for the object value passed to it
Ex: penguin
21
![Page 22: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/22.jpg)
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
![Page 23: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/23.jpg)
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
![Page 24: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/24.jpg)
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
![Page 25: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/25.jpg)
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
![Page 26: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/26.jpg)
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
![Page 27: 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](https://reader035.vdocuments.us/reader035/viewer/2022062321/56649e7a5503460f94b7abfe/html5/thumbnails/27.jpg)
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