World-level Methods with Parameters - PowerPoint PPT Presentation

alice l.
Skip this Video
Loading SlideShow in 5 Seconds..
World-level Methods with Parameters PowerPoint Presentation
Download Presentation
World-level Methods with Parameters

play fullscreen
1 / 21
Download Presentation
Presentation Description
Download Presentation

World-level Methods with Parameters

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Alice World-level Methodswith Parameters

  2. Larger Programs • As you become more skilled in writing programs, you will find that your programs quickly begin to increase to many, many lines of code. • Games and other "real world" software applications can have thousands, even millions of lines of code. • In this session, we begin to look at organizing large programs into small manageable pieces.

  3. Goals of OOP • organize a large program into small pieces • design and think about an intricate program • find and remove errors (bugs)

  4. Modifying the program • To make the snowpeople animation more realistic, we might want the snowman to be a little less shy. In our original program, the snowman tries to catch the snowwoman's attention only once. Perhaps he could try to get her attention more than once. • To make this modification, additional lines would be added to the code. • This would make the program code longer and more difficult to read and think about.

  5. A Solution • A solution to the problem is to • define our own method • name the new method catchAttention • Then, we can drag-and-drop the catchAttention method into the edit box just like the built-in methods

  6. Demo: The solution • First, to associate the new method with the World • select the World tile in the Object Tree • select the methods tab in the details area • click on the "create new method" button

  7. Create a World Method • Choose a meaningful name • Edit the method

  8. Demo • A demonstration of writing the catchAttention method

  9. World-level method • catchAttention is a world-level method because it • is defined as a method for World • has instructions that involve more than one object (snowman, snowwoman, camera)

  10. Using the catchAttention method The catchAttention method is executed by calling (invoking) the method from my first method

  11. Why? • Why do we want to write our own methods? • saves time -- we can call the method again and again without reconstructing code • reduces code size – we call the method rather than writing the instructions again and again • allows us to "think at a higher level" • can think “catchAttention" instead of “turn head to face the camera, then say ‘Ahem’ while moving eyes up and down" • the technical term for "think at a higher level" is "abstraction"

  12. © 2006 Dr. Tim Margush moveInCircle • Another candidate for a world method is one to move an object in a circle • Here a world method does it to a car Adding spinning wheels would be fun, but complicate our example

  13. © 2006 Dr. Tim Margush Using moveInCircle • Here we just do the maneuver twice • What about moving different objects in a circle? • Create a method for each???? • No! That's what parameters are for!

  14. © 2006 Dr. Tim Margush Add an Object Parameter • Edit the moveInCircle method and click the create new parameter button • Give it a name • Choose a type • OK?

  15. © 2006 Dr. Tim Margush moveInCircle(whichObject) • The parameter is named whichObject • It holds the place of a real object that will be substituted later • Replace all occurrences of car (in the method) with the parameter • Just drag it into place drag

  16. © 2006 Dr. Tim Margush Test • I got this error message when I tested my program • The problem is in thecall of this method • We added a parameter • We did not choose an argument Click to select the argument

  17. © 2006 Dr. Tim Margush Other Arguments • Several additional choices come to mind • How big of a circle? • How long should it take? • Which direction should it turn? • moveInCircle(whichObject, radius, duration, direction) • Add more parameters!

  18. © 2006 Dr. Tim Margush Adding Parameters • Note the Types • Number, Number, and Other • Under Other, find Direction so you will be able to choose up, down, left, etc.

  19. © 2006 Dr. Tim Margush Convert radius to circumference Adapting the Code • The parameters will need to be dragged into the body of the method to replace the specific constants

  20. © 2006 Dr. Tim Margush Advantages of Methods • Abstraction • Methods allow the programmer to organize details of a task under the method's name • Code reuse • Methods can be called several times without having to copy the details of the code • Generality • Parameters allow the same actions to be applied to a variety of objects • Debugging and maintenance • Fixing or changing part of a program may only require modification of a single method

  21. Assignment • World-level Methods • How to create them • How to call them • When it is appropriate to use them • Lab 05