chapter 4.3 class level methods (versus previous world-level methods)

29
Chapter 4.3 Class level methods (versus previous world-level methods)

Upload: alden

Post on 25-Feb-2016

57 views

Category:

Documents


2 download

DESCRIPTION

Chapter 4.3 Class level methods (versus previous world-level methods). Which of the following is the best explanation of what makes a good parameter. It’s something that supports common variation in how the method is done It’s got a meaningful name It can be either an Object or a number - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 4.3 Class level methods (versus previous world-level methods)

Chapter 4.3 Class level methods(versus previous world-level methods)

Page 2: Chapter 4.3 Class level methods (versus previous world-level methods)

Which of the following is the best explanation of what makes a good parameter

A. It’s something that supports common variation in how the method is done

B. It’s got a meaningful nameC. It can be either an Object or a numberD. It helps manage complexity in large programs

Page 3: Chapter 4.3 Class level methods (versus previous world-level methods)

If we want to create a world-level levitate method that is controllable in which thing it levitates and for how long: What number and types of parameters would you design it to use?

A. One ObjectB. One NumberC. One Object and One NumberD. Two ObjectsE. Two Numbers

Page 4: Chapter 4.3 Class level methods (versus previous world-level methods)

What are the correct names for the underlined terms?

1 2A Calling Object ParameterB Parameter Calling object

Page 5: Chapter 4.3 Class level methods (versus previous world-level methods)

Chapter 4.3 Class level methods(versus previous world-level methods)

• Define actions that inherently belong to a certain class of object– Dogs can “beg”– Skaters can “spin”– Penguins can “bellySlide”

• To create them you – click on an object of that type– Click on create method in the details pane (lower left)– After you write the method, you may want to save this out

as a “new” class with your augmented abilities.• Change name of object, right click and click save as – creates

an .a2c file – a new Alice class file

Page 6: Chapter 4.3 Class level methods (versus previous world-level methods)

• World-level methods:– Belong to no “one specific” object (or class or

objects) [Hint: starts with World.]• Any objects, information you need to modify how it works

should be passed as a parameter

• Class-level methods:– Are actions that belong to a specific “class” of objects

[Hint: starts with an object name]

Page 7: Chapter 4.3 Class level methods (versus previous world-level methods)

Which of the following would not be a good class level method (for some given class)

A. PartyB. SwimC. changeColorD. lineUpWithE. layDown

*DISCUSSION: What possible parameters would you want to use with these methods?

Page 8: Chapter 4.3 Class level methods (versus previous world-level methods)

Making our own class level method and creating a customized class

• Demo: Making a class level method for the spiderRobot class to allow any spiderRobot to say Hello to an Alien– Click on spiderRobot before clicking on make new method

• This makes it a class level method, not world level– Drag in the code to have him face the alien and then say

hello– If we add another spiderRobot – he can’t say Hello, so save

off that class as talkativeSpiderRobot• Be sure to save it in the Gallery!• Create 2 of those objects

Page 9: Chapter 4.3 Class level methods (versus previous world-level methods)

How to parameterize a method

• In what ways would we want to be able to control/vary what this method does so that– We can re-use it in more situations– Does it make sense that someone would WANT to

be able to control this method’s actions• 2 Examples:– Next 6 slides: sayHelloToAlien -> sayTo• A class-level method of spiderRobot

Page 10: Chapter 4.3 Class level methods (versus previous world-level methods)

What would you do so the talkativespiderRobot class can say hello to anything

(object) A. Create a method that is a class method of the object

you want him to say hello to (e.g. alien)B. Create a method which takes one parameter: which

object to turn to faceC. Create a method which takes one parameter: a

direction in which to faceD. Create method that takes two parameters: the

talkativeSpiderRobot and the object it should turn to face

E. I don’t know

Page 11: Chapter 4.3 Class level methods (versus previous world-level methods)

Demo: How do we do that?

• Modify method name to sayHelloTo• Create a parameter which will control what Object

we want to greet• Modify the class-level method to turnToFace the

parameter object• Go back to myfirstMethod and put in a valid

parameter for who you want to face– If not, you will get a “null” error

• TEST!– Try a different object! (ground)

Page 12: Chapter 4.3 Class level methods (versus previous world-level methods)

Let’s go one step further

• What if we want to make a method called “sayTo”– Just from that name, what do you think it will be?

Page 13: Chapter 4.3 Class level methods (versus previous world-level methods)

What would the method header for a sayTo method look like

Most importantly, what’s wrong with the other options!

Page 14: Chapter 4.3 Class level methods (versus previous world-level methods)

Which of these would be the right method body?

Page 15: Chapter 4.3 Class level methods (versus previous world-level methods)

Demo: How do we do that? (if time)

• Rename the method from sayHelloTo to sayTo• Add a new parameter named whatToSay– It doesn’t matter if this is the first or second parameter to the

sayTo method• Modify the method body (definition) to make whatToSay

be the parameter to the talkativeSpiderRobot.say method• In myFirstMethod, where you call the sayTo method,

change the whatToSay parameter to be whatever you want the robot to say

• TEST!

Page 16: Chapter 4.3 Class level methods (versus previous world-level methods)

Parameterization Done!

• Very specific class-level method with no parameters– Class-level – owned by talkativeSpiderRobot class– An action that applies to talkativeSpidertRobots

• Added 2 parameters to make it more flexible/control how it behaves– Object: Who to talk to– String: What to say

• Next: Deeper analysis of things people get confused about with class-level methods

Page 17: Chapter 4.3 Class level methods (versus previous world-level methods)

Suppose we have the following world with 2 objects of the talkativeSpiderRobot class

What code will make this happen?

Page 18: Chapter 4.3 Class level methods (versus previous world-level methods)

Which method call will make this happen?

C) Both of those method calls do that same thing

Page 19: Chapter 4.3 Class level methods (versus previous world-level methods)

Class-level methods (e.g. skater methods, talkativeSpiderRobot methods) can have an

object type parameterA. False, they have to only act on the object of

the class they belong to (e.g. skater, tSR)B. False, class level methods can’t have any

parametersC. True, they can take as parameters another

object they may interact withD. True, they must always take an object

parameter of the class they belong to (e.g. skater,tSR)

Page 20: Chapter 4.3 Class level methods (versus previous world-level methods)

Small variation: Make both objects turn to face each other

Page 21: Chapter 4.3 Class level methods (versus previous world-level methods)

In lab, Maria says: “What I did was I used the parameter WhoTo and made it the calling object for a call to the turnToFace method”

Page 22: Chapter 4.3 Class level methods (versus previous world-level methods)

Frank is trying to follow along: “Wait, you said whoTo was a parameter here, why did you call it a calling

object here?”

Page 23: Chapter 4.3 Class level methods (versus previous world-level methods)

Maria should say…

A. Sorry, I mis-spoke, whoTo is a parameter, and that’s what we should call it

B. Because names can depend on the context in which they are used (e.g. the sayTo method or the turnToFace method)

C. Because names are determined by location: one place it comes after the method name and in one place it comes before the method name

Page 24: Chapter 4.3 Class level methods (versus previous world-level methods)

Yes, one “thing” can have two names:It depends on the CONTEXT

Page 25: Chapter 4.3 Class level methods (versus previous world-level methods)

Why do we care what these things are named and why do we name them different things in different places?

• After this class: calling object vs parameter– Probably not important

• What is important– Recognizing the specificity that can be critical in

communicating about technical issues– Taking care to interact effectively with others

when discussing technical issues• Especially when they are LESS or MORE familiar with

the given technology than you

Page 26: Chapter 4.3 Class level methods (versus previous world-level methods)

Have you ever had an experience where miscommunication regarding a technical issue caused problems?

(maybe fixing a problem,trying to get software to work, etc.)

A. YesB. NoC. I don’t know what you are talking about

Page 27: Chapter 4.3 Class level methods (versus previous world-level methods)

Example:

• Not you: Beth’s mom– Says “my password to log in to the machine is

XXX”• Oh but sometimes it won’t take it and it makes me

enter YYY• Not true – it’s a login for ANOTHER program

– Level of understanding of specificity doesn’t extend to difference between laptop login versus website login

Page 28: Chapter 4.3 Class level methods (versus previous world-level methods)

Group Discussion:Practice for the “Real World”!

• Restate what your partner said– Asking “did I get that right?” at the end

• Ask clarifying questions– When you said THIS did you mean

THAT?• Practice using different words– So another way to explain it would be…

Is that the same?

Page 29: Chapter 4.3 Class level methods (versus previous world-level methods)

Opacity and Is Showing

• Properties of objects, useful for nice effects and to make things “appear” later in animations

• No existing methods in method list– Click on properties tab and drag that property

pane into program, choose a value and it will call a “set to” method on that property

– Every property can be set with a “set to” method in your program