Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Class-level Methods Chapter 6 part 1

2 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

3 Methods Built-in (predefined) methods o Examples: move, turn to face, say Custom methods o User created method o Does something that was not predefined in Alice 3

4 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 walking o A skater skating Unlike world class-level methods o Which has access to multiple classes 4

5 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

6 Associate Animation with Skater Select iceSkater tile in Object Tree Select methods tab in details panel Click on create new method button 6

7 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

8 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

9 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 slideRight Do in order Lift left leg and turn upper body forward Lower left leg and return body upright 9

10 Alice Programs for skate Algorithm Notice how skate is preceded by icekater instead of world like World.my first method 10

11 Demonstration Concepts illustrated o Method defined for a specific type of object defines action for that object o A method can call other methods skate method calls slideRight and slideLeft 11

12 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

13 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

14 Importing CleverSkater An instance of the CleverSkater class can be added to a new world o Use File | Import o Set File Type to A2C o Choose class file to import 14

15 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

16 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

17 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

18 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

19 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

20 Guidelines To avoid potential misuse of class-level methods follow these guidelines o 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

21 Demonstration Concept illustrated o Parameter whichObject is placeholder for the object value passed to it  Ex: penguin 21

22 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 color o opacity o isShowing 22

23 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

24 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

25 Algorithm Translated to Alice Notice how new method is preceded by monitor not world o Means it is a class-level method not world-level o 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

26 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 effect o Turn on monitor using turnOnOFF 26

27 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


Download ppt "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."

Similar presentations


Ads by Google