Lecture 4: Finishing 4.2 and onto 4.3

29
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 t may not seem like it, but you are building up valuable skil e you text to put on your resume to help you describe it to e

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

Transcript of Lecture 4: Finishing 4.2 and onto 4.3

Page 1: Lecture 4: Finishing 4.2 and onto 4.3

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!

Page 2: Lecture 4: Finishing 4.2 and onto 4.3

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

Page 3: Lecture 4: Finishing 4.2 and onto 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

Page 4: Lecture 4: Finishing 4.2 and onto 4.3

In the World.solo method you created (with the beetles example), what type was the bandMember parameter?

A. BooleanB. NumberC. ObjectD. Other-> StringE. ringoBeetle

Page 5: Lecture 4: Finishing 4.2 and onto 4.3

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

Page 6: Lecture 4: Finishing 4.2 and onto 4.3

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

Page 7: Lecture 4: Finishing 4.2 and onto 4.3

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

Page 8: Lecture 4: Finishing 4.2 and onto 4.3

Just vocabulary: Parameters

• Always come “after” the method name on a tile

Page 9: Lecture 4: Finishing 4.2 and onto 4.3

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); }}

Page 10: Lecture 4: Finishing 4.2 and onto 4.3

What does this do?

Page 11: Lecture 4: Finishing 4.2 and onto 4.3

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

Page 12: Lecture 4: Finishing 4.2 and onto 4.3

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!

Page 13: Lecture 4: Finishing 4.2 and onto 4.3

Any comments for Maria?

Page 14: Lecture 4: Finishing 4.2 and onto 4.3

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

Page 15: Lecture 4: Finishing 4.2 and onto 4.3
Page 16: Lecture 4: Finishing 4.2 and onto 4.3
Page 17: Lecture 4: Finishing 4.2 and onto 4.3
Page 18: Lecture 4: Finishing 4.2 and onto 4.3

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 19: Lecture 4: Finishing 4.2 and onto 4.3

• 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 20: Lecture 4: Finishing 4.2 and onto 4.3

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 21: Lecture 4: Finishing 4.2 and onto 4.3

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 22: Lecture 4: Finishing 4.2 and onto 4.3

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

Page 23: Lecture 4: Finishing 4.2 and onto 4.3

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

Page 24: Lecture 4: Finishing 4.2 and onto 4.3

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

Page 25: Lecture 4: Finishing 4.2 and onto 4.3

What would the method header for a sayTo method look like

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

Page 26: Lecture 4: Finishing 4.2 and onto 4.3

Which of these would be the right method body?

Page 27: Lecture 4: Finishing 4.2 and onto 4.3

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 28: Lecture 4: Finishing 4.2 and onto 4.3

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

What code will make this happen?

Page 29: Lecture 4: Finishing 4.2 and onto 4.3

Which method call will make this happen?

C) Both of those method calls do that same thing