Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "Chapter 4.3 Class level methods (versus previous world-level methods)"— Presentation transcript:

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

2 It’s got a meaningful name It can be either an Object or a number
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 It helps manage complexity in large programs Really, this is a review of 4.2 But it’s a hard thing, so we ask it again. Correct Answer: Is the BEST – as we saw in the fly method from last lecture, we made parameters to allow the fly method to vary in how high and how far forward the fish would fly Incorrect Answers and why students might think so: B is something it should have -- it’s true methods should have names that are meaningful C is true (but it can also be other things) D is true of methods –( and kind of also parameters), but A is best

3 One Object and One Number Two Objects Two Numbers
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? One Object One Number One Object and One Number Two Objects Two Numbers Big Idea: A method can be controlled in how it performs by the parameters it is passed. Correct Answer and Why: C) One parameter (an Object) would be for the thing it levitates and one parameter (a Number) would be for the time (how long) it should levitate Students might think one object just because they only read which thing Students might think one number because they think the calling object is the thing which is levitated – but this is a world-level method, not a class level method D) They might not realize the difference between types for parameters E) They might not realize the difference between types for parameters

4 What are the correct names for the underlined terms?
1 2 A Calling Object Parameter B Calling object Big Idea: The nomenclature/vocabulary in computing is extensive. And the difference between parameters and calling objects is tricky if students have really understood our explanation of why we have methods (to support variation in method behavior). (Note: yes, there are only two options, we just want to reinforce vocabulary). Correct Answer and Why: This is an example of a call to a class level method (previously we had things like which was a world level method). Class-level methods are methods that BELONG to a specific object. That object is the MAIN actor in that method. In this case skateAround is a class level method of the iceSkater class. So the iceSkater object is the primary object in the method – the one doing the acting. We call this the calling object. However, we need to specify a SECOND object – that thing the iceSkater should skateAround – in this case the penguin. To specify this “helper” object that controls how the method happens we use a parameter. REMEMBER: the calling object goes to the left of the method name, the parameters to the right of the method name. Wrong Answers/Questions Students will Ask: Students will say that BOTH iceSkater and penguin should be parameters, since they both control how the method is done. However, that would only be true if it was a World level method. Modern computer programming languages are called “object-oriented”. That means they organize things around classes of objects, and defined beahviors that “belong” to those objects. skateAround is a method that can belong to an iceSkater. It’s something she can do. Students have used these before – sometimes they have the same name in different classes. For example eskimo.move up 1meter is a class level method of iceSkater called move (and it has a parameter direction and a parameter howFar). But spiderRobot also has a move method in it’s class as well.

5 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 CS instructors: We are not going into the (lame) interpretation of inheritance in Alice (it’s in the book if you like). It’s NOT IMPORTANT for the goals of this class. And it’s really challenging to explain without saying “just believe me, it’s a good thing”. Read the slide. <Note: the last bit – saving out the new class with the new method is actually inheritance in Alice. It’s creating a new class. But it’s not that important. Really.>

6 World-level methods: Class-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] On this slide we compare and contrast world level methods and class level methods students have seen in the textbook. The solo method (with the beetles in chapter 4.2) was a World level method. It wasn’t an action that “belongs” to any one specific object. For this kind of method, any information you need to control how the method acts must be passed in as a parameter. In contrast, in this section 4.3 we’re looking at class level methods – methods that have a “main actor” – that they are an action on. The spin method you used in 4.3 is something a skate does. It is an action that belongs to a skater. If we have a method called spinHowManyTimes that is a class level method of the skater class, then to control how many times she spins, we would have to pass a parameter. iceSkater is not a parameter, it’s a calling object – in the example instructions above.

7 Which of the following would not be a good class level method (for some given class)
Party Swim changeColor lineUpWith layDown This is a “design” question. There could be arguments for several answers. Big Idea: Class-level methods are methods that “belong” to a specific object -- one object that really does it. World level methods don’t have any specific object they belong to Correct Answer and Why: Party – the verb, requires more than one person. It’s not something “one object” of a class can do. Discussing the Other Answers: It should be noted that the other class methods, might differ in how they carry out that method. How a “book” lays down, might be different than how a person lays down. That’s why they are class level, each class can have it’s own “implementation” (e.g. set of code statements) to define how that method works for that class. *DISCUSSION: What possible parameters would you want to use with these methods?

8 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 Two videos: Chap 4.3 CreateClassLevelMethod (uses world ClassLevelMethodStarter.a2w) Chap 4.3 CreateTalkativeSpiderRobotClass (uses same world as above – after finished) Walk through adding a class-level one (start with ClassLevelMethodStarter)– see that you have to click on spiderRobot, then MakeNewMethod, drag in his methods: make it turn to Face the alien and then say hello. show how it becomes something a spiderRobot can do (don’t get into if we had 2 spiderRobots, it would only work on this one) <Beth – I don’t care about saving it off as another class, but eh>

9 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 Read first bullet In the next few slides we’re going to work through modifying our sayHelloToAlien class level method of talktaiveSpiderRobot to be more flexible – to be useful in more situations. First, we’ll remove the restriction on who the robot can talk to. We want him to be able to talk to any object – not just the alien. The, we’ll remove the other restriction – so he’ll be able to say things other than Hello.

10 What would you do so the talkativespiderRobot class can say hello to anything (object)
Create a method that is a class method of the object you want him to say hello to (e.g. alien) Create a method which takes one parameter: which object to turn to face Create a method which takes one parameter: a direction in which to face Create method that takes two parameters: the talkativeSpiderRobot and the object it should turn to face I don’t know Big Idea: To remove a restriction like this on a method, create a parameter. What would you do to make it so you could have the spiderRobot say hello to anything? B. You have the sayHelloToAlien method change to take a parameter – that is which object to face (not always the alien) C is not as good, because it would be harder to figure out a direction (it might be right, but it depends on how the initial world is set up, so it’s not as robust) A is wacky – it’s not an action that “belongs” to some other object – it’s still the talkativeSpiderRobot acting. D: For class level methods, you do not need to include the “calling object” as a parameter (which is the talkativeSpiderRobot)

11 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) Demo with video: chap4.3_SayHelloTo.mp4 To demo on your own: load world SayHelloToStarter.a2w (has the unparameterized version – with sayHelloToAlien) Rename to make it be sayHelloTo and add object parameter for who to talk to.

12 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?

13 What would the method header for a sayTo method look like
Big Idea: How do methods change if we add parameters to them. Correct Answer and Why: We need two parameters to cover both who and what to say We’ve made the method one more level of “flexible” We had already made it so you could say Hello to any object (whoTo). Now that we change the method name to “sayTo” so we’ll need to add another parameter. We’ll need a String whatToSay is what we called it here. And we also still need to be able to control who we want to talk to so we also need a second parameter which is an Object whoTo Incorrect Answers and Why Students Might pick them: C and D are identical – the name of the parameter could be anything… They both have an Object as a parameter. Is what to Say an Object? No – probably a String (text) -- which is B A. Has no parameters so we wouldn’t be able to control it’s behavior at all. Most importantly, what’s wrong with the other options!

14 Which of these would be the right method body?
Big Idea: Sometimes when people are changing a method from a specialized version to one that uses a parameter, they forget to modify the code of the method to use those parameter names. Correct Answer and Why: Once we write the method header, we need to be sure to remember to CHANGE THE CODE to use the parameter names. A uses the parameters called a and b. BTW. Those are BAD choices for parameter names. See how it makes it harder to read the code when the parameters don’t have names that reflect what they do? You will lose points if you code that way. I did it here to make the question harder. Incorrect Answer and Why students might pick it. B) This answer has comfortable names (World and Hello) – but that’s the original hard-coded or “unparameterized” version. It will ALWAYS face the world and ALWAYS say hello – no matter what parameters are passed for a and b.

15 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! Video: Chap 4.3 SayTo.mp4 Load SayToStarter.a2w has sayHelloTo (Object). Add the String parameter on whatToSay

16 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

17 What code will make this happen?
Suppose we have the following world with 2 objects of the talkativeSpiderRobot class Load Video: Chap 4.3 Slide 17 TSR2 SayTo TSR.mp4 (the Alice world with this code in it is TSR2 SayTo TSR Example.a2w) Next I will ask you to pick a line of code that would make the following thing happen. I’m going to play the code (or a video of it) and you watch carefully. The world is set up as you see above. There are two talkativeSpiderRobot objects – on the left is TsR and the one highlighted here is the one on the right (see the yellow box) is tSR2. Play the video where talkativeSpiderRobot2 (on right) calls method sayTo talkativeSpiderRobot… then next have them pick the right method call on the next slide Lecture5TSR2CallingObjVsParam What code will make this happen?

18 Which method call will make this happen?
Big Idea: It can be confusing to students when two objects of the same type are involved and one is the calling object and the other is a parameter. The calling object is the primary actor. Correct Answer and Why: A) The calling object is tSR2 (the spider on the right in our example) – so he is the primary actor, the tSR PARAMETER is more of a side player describing variation in who you can “SayTo” -- it could have been any other object – not just a tSR. Incorrect Answers and Why students might think they are right: B) The opposite – they either didn’t watch the video closely enough or don’t get the idea of paying attention to which object is which – exactly. C) We can go back and review the code. But the “actor” who turns is the calling object. The parameter object is the thing the calling object turns to face C) Both of those method calls do that same thing

19 False, class level methods can’t have any parameters
Class-level methods (e.g. skater methods, talkativeSpiderRobot methods) can have an object type parameter False, they have to only act on the object of the class they belong to (e.g. skater, tSR) False, class level methods can’t have any parameters True, they can take as parameters another object they may interact with True, they must always take an object parameter of the class they belong to (e.g. skater,tSR) Big Idea: A class-level method of some class (like TSR) can have an object parameter of that same type Correct Answer and Why: C (e.g. the penguin on pages that the cleverSkater can skate around). This is a rationale question – or a “what’s the general rule” based on the previous example. You can take parameters of ANY type, -- object and any kind of object (TSR or AlienOnWheels, etc.) Incorrect Answers and Why students might pick them: They might think that only the calling object can be invovled B) <No real reason one might pick this, its just the simple option) D) There’s not a restriction that the method has to take objects of the same type. The talkativeSpiderRobot can sayTo ANY object (a penguin, a bunny).

20 Small variation: Make both objects turn to face each other
Video to Load: Chap 4.3 Slide 20 TSR2 Modify Code Solution code in TSR2 Modify Code Colution.a2w SAY: We’re going to describe a new scenario – a modification to our sayTo method. Hand Animate with ink – let’s have calling object turn first (the one on the right), then have the parameter (tsR in this case) turn to face calling object, then calling object says “brother!” BIG IDEA: Students have trouble with the use of vocabulary calling object and parameter. They sometimes thing that a specific object IS either a calling object or a parameter. But in fact the name we use for an object depends on the situation (e.g. exact line of code) that we are talking about.

21 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” SAY: Maria was coding this change in the lab, this is what she wrote. You can see the second line in the method that she added. As she describes what she did she says “What I did was I used the parameter sayTo and made it the calling object for a call to the turnToFace method.

22 Frank is trying to follow along: “Wait, you said whoTo was a parameter here, why did you call it a calling object here?” SAY: But Frank is trying to follow along and he says “Wait, you said whoTo was a parameter here (in the method header), why did you call it a calling object here (in the second instruction).

23 Maria should say… Sorry, I mis-spoke, whoTo is a parameter, and that’s what we should call it Because names can depend on the context in which they are used (e.g. the sayTo method or the turnToFace method) Because names are determined by location: one place it comes after the method name and in one place it comes before the method name SAY: What should Maria say back to Frank? Correct Answer and Why: B. The whoTo object is a parameter in the CONTEXT of the method header for sayTo. However, in the CONTEXT of the instruction that calls the turnToFace method it is a calling object. It is the OBJECT that is moving when we call turnToFace Incorrect (sort of) Answer – but it’s true as well. C). You can determine the name to use (parameter or calling object) based on location in the instruction. In an instruction that calls a method, the calling object comes before (to the left) of the method name, and parameters are in a list after (to the right of) the method name.

24 Yes, one “thing” can have two names: It depends on the CONTEXT
SAY: Wait, that seems hard! Well, it isn’t. Think about this. Here we have a picture of a family. We have the youngest one in the middle the middle aged one in the left and the oldest one on the right. If you are the youngest one, you call the guy in white “dad”. In the CONTEXT of being the youngest one, the guy in white is called “dad”. However, if you are the oldest one on the right, then you call the guy in white “son”. In the CONTEXT of being the oldest one, the guy in white is called a different name “son” not “dad”. See someone (object) can be called a different name based on the CONTEXT or situation.

25 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 After you get done with this class, is it important to know this information? Probably not unless you take more programming courses. However, …. Read the rest of the slide.

26 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.) Yes No I don’t know what you are talking about This is just a chance for them to think about when this has happened to them.

27 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 This wouldn’t be you of course… but Beth was trying to help her mom and needed to log into her laptop. So she asked her mom, what’s the password to your machine? She said its XXXXX, oh but sometimes it won’t take that and makes me enter YYYYYY. Now you know that can’t be true. Your computer password is one thing. It’s not randomly one thing one time and something else another time. She was confusing her computer password and her gmail password (she has different ones for each – which is good!). However, her lack of understanding of how computers work and what you have to pay attention to in level of detail and specificity showed in how she couldn’t distinguish between the password to the computer and the password for her login.

28 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? Remember, this is why we have group discussion of clicker questions – so you can practice your new specificity skills for the real world. Use these three techniques….

29 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 Extra stuff from the “Tips and Techniques” at end of Chapter 4. It’s mostly just animation “goodies” but it’s useful for them to know. To make something disappear, we suggest setting the opacity to 0. Then to make it reappear set opacity to 100. If students want to have a world where something suddenly SHOWS UP, build the world setup with the object there, but opactiy set to 0. then to make it appear, change opactiy to 100. Also, if one wants to make objects be able to “come together” in the world and not have to worry about challenges of depth, etc. You can put an “anchor” object someplace in the world (something you never use – like a bunny or whatever) and set it’s opacity to 0. Then, to make two other objects “come together” you can make them both moveTo the “hidden bunny”.

Download ppt "Chapter 4.3 Class level methods (versus previous world-level methods)"

Similar presentations

Ads by Google