Upload
woody
View
45
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Lecture 4: Finishing 4.2 and onto 4.3. What have you learned so far? To control a computer by designing and writing a program Creating animations/movies of your own choosing With complex manipulations of objects using methods and variations in control ( doTogether , doInorder ) - PowerPoint PPT Presentation
Citation preview
Lecture 4: Finishing 4.2 and onto 4.3
• What have you learned so far?• To control a computer by designing and writing a
program– Creating animations/movies of your own choosing– With complex manipulations of objects using methods
and variations in control (doTogether, doInorder)• Developed skills in managing complexity in working
with data on a computer– Abstraction by “breaking into pieces”
• Work on them independently and then put back together
It may not seem like it, but you are building up valuable skills.I’ll give you text to put on your resume to help you describe it to employers!
Before we begin
A. I was able to do homework for section 4.3 B. I was able to read section 4.3C. I was able to skim section 4.3D. I haven’t looked at section 4.3
Next Class Homework
• Chapter 4.3 (if you didn’t do it)• Chapter 5.1• Homework Overview due:– Week 4 Monday: 5.2 and 6.1– Week 4 Wed: 6.2 up to 188– Week 5 Monday: Rest of 6.2– Week 5 Wednesday: 7.1
In the World.solo method you created (with the beetles example), what type was the bandMember parameter?
A. BooleanB. NumberC. ObjectD. Other-> StringE. ringoBeetle
Which of the following is the best definition of a class-level method?
A. A method that controls an ice-skaterB. A method that belongs to a particular objectC. A method that takes parameters to control
how it worksD. A method that you need to be sure to save in
order to be able to use it in the future
The somewhat complex steps of having a skater spin around were managed by what technique:
A. Use of parameters to control the actionB. Stepwise-refinement to create multiple levels
of methodsC. Class-level methods to support development
of a new cleverSkater classD. Use of primitive methods pre-built into the
Alice software
Academic Integrity and Social Responsibility
• Computer “programs” bring new complexities to academic integrity– There’s a lot of programs at alice.org
• I am responsible to– You – that your effort be rewarded – Your fellow students – their accomplishments have value
• Social Responsibility in a new situation (4 items)– Please don’t talk, murmur, mumble with others in group during
• Individual vote• Class-wide discussion
– Only FAIR to others that they have a supportive learning environment• It’s a large room, it adds up
Just vocabulary: Parameters
• Always come “after” the method name on a tile
Alice: It’s REALLY close to Java(and most every programming language today)
public class Demo{ public static void main (String[] args) { World w = new World("Chapter04Example"); //x, y, and z coordinates) Skater iceSkater = new Skater(w, 100, 200, 0); iceskater.move("forward", .5, 2); //where 0 is forward, 1: backward, 2:left,etc. //iceskater.move(0, .5, 2); }}
What does this do?
Which best describes what happens when my First Method is run?
A. The helicopter moves down to the rabbit’s location
B. The rabbit moves up to the helicopter’s location
C. The helicopter and the rabbit move to meet each other (halfway between)
D. I don’t know
More parameters:Demo: Let’s make fish fly…
• Preview: – We have a fly method (no parameters) which makes
a fish jump out of the water .5 meters (moving forward .3 meters on the way up and another .3 meters on the way down)
• Maria would like to make it more flexible by parameterizing it to control height and distance– Take a look at what she’s done and see if it looks ok!
Any comments for Maria?
Any comments for Maria?
A. No, but you could make it better if you include a speed parameter
B. No, it’s perfect as it isC. Yes, you have created the wrong parametersD. Yes, you have incorrectly used a parameter
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. PartyB. SwimC. changeColorD. lineUpWithE. 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– Next 4 slides: jumpOnto method
• A class-level method of chicken
If I want a spiderRobot class method which will have him 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 spiderRobot 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• TEST!– Ah! What’s this null thing?
• Change the calling location in myFirstMethod to indicate which Object we want to have the robot face
What would the method header for a sayTo method look like
Most importantly, what’s wrong with the other options!
Which of these would be the right method body?
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!
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