Presentation on theme: "Chapter 4.3 Class level methods (versus previous world-level methods)"— Presentation transcript:
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 name C.It can be either an Object or a number D.It helps manage complexity in large programs
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 Object B.One Number C.One Object and One Number D.Two Objects E.Two Numbers
What are the correct names for the underlined terms? 12 ACalling ObjectParameter B Calling object
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
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]
Which of the following would not be a good class level method (for some given class) A.Party B.Swim C.changeColor D.lineUpWith E.layDown *DISCUSSION: What possible parameters would you want to use with these 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
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
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 face C.Create a method which takes one parameter: a direction in which to face D.Create method that takes two parameters: the talkativeSpiderRobot and the object it should turn to face E.I don’t know
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)
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?
What would the method header for a sayTo method look like Most importantly, what’s wrong with the other options!
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!
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
Suppose we have the following world with 2 objects of the talkativeSpiderRobot class What code will make this happen?
Which method call will make this happen? C) Both of those method calls do that same thing
Class-level methods (e.g. skater methods, talkativeSpiderRobot methods) can have an object type parameter A.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 parameters C.True, they can take as parameters another object they may interact with D.True, they must always take an object parameter of the class they belong to (e.g. skater,tSR)
Small variation: Make both objects turn to face each other
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”
Frank is trying to follow along: “Wait, you said whoTo was a parameter here, why did you call it a calling object here?”
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
Yes, one “thing” can have two names: It depends on the CONTEXT
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
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.Yes B.No C.I don’t know what you are talking about
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
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?
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