1 / 29

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 )

woody
Download Presentation

Lecture 4: Finishing 4.2 and onto 4.3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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!

  2. Before we begin • I was able to do homework for section 4.3 • I was able to read section 4.3 • I was able to skim section 4.3 • I haven’t looked at section 4.3

  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

  4. In the World.solo method you created (with the beetles example), what type was the bandMember parameter? • Boolean • Number • Object • Other-> String • ringoBeetle

  5. Which of the following is the best definition of a class-level method? • A method that controls an ice-skater • A method that belongs to a particular object • A method that takes parameters to control how it works • A method that you need to be sure to save in order to be able to use it in the future

  6. The somewhat complex steps of having a skater spin around were managed by what technique: • Use of parameters to control the action • Stepwise-refinement to create multiple levels of methods • Class-level methods to support development of a new cleverSkater class • Use of primitive methods pre-built into the Alice software

  7. 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

  8. Just vocabulary: Parameters • Always come “after” the method name on a tile

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

  10. What does this do?

  11. Which best describes what happens when my First Method is run? • The helicopter moves down to the rabbit’s location • The rabbit moves up to the helicopter’s location • The helicopter and the rabbit move to meet each other (halfway between) • I don’t know

  12. If time, build the whole world, screw up the jumping by not doing together the up and forward. More parameters:Demo: Let’s make fish fly… SHOW DEFINITELY: how might want to jump high (2 meters) or far forward (.5 meters) • 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!

  13. Any comments for Maria?

  14. Any comments for Maria? • No, but you could make it better if you include a speed parameter • No, it’s perfect as it is • Yes, you have created the wrong parameters • Yes, you have incorrectly used a parameter

  15. 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

  16. 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]

  17. Which of the following would not be a good class level method (for some given class) • Party • Swim • changeColor • lineUpWith • layDown *DISCUSSION: What possible parameters would you want to use with these methods?

  18. 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

  19. 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

  20. If I want a spiderRobot class method which will have him 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 spiderRobot and the object it should turn to face • I don’t know

  21. Demo: How do we do that? • Modify method name tosayHelloTo • 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

  22. What would the method header for a sayTo method look like Most importantly, what’s wrong with the other options!

  23. Which of these would be the right method body?

  24. 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!

  25. Suppose we have the following world with 2 objects of the talkativeSpiderRobot class What code will make this happen?

  26. Which method call will make this happen? C) Both of those method calls do that same thing

More Related