Worldlevel Methods with Parameters Alice Larger Programs As
- Slides: 21
World-level Methods with Parameters Alice
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.
Goals of OOP organize a large program into small pieces design and think about an intricate program find and remove errors (bugs)
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.
A Solution A solution to the problem is to define our own method name the new method catch. Attention Then, we can drag-and-drop the catch. Attention method into the edit box just like the built-in methods
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
Create a World Method Choose a meaningful name Edit the method
Demo A demonstration of writing the catch. Attention method
World-level method catch. Attention 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)
Using the catch. Attention method The catch. Attention method is executed by calling (invoking) the method from my first method
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 “catch. Attention" 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"
move. In. Circle Adding spinning wheels would be fun, but complicate our example Programming in Alice © 2006 Dr. Tim Margush Another candidate for a world method is one to move an object in a circle Here a world method does it to a car 12
Using move. In. Circle 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! Programming in Alice © 2006 Dr. Tim Margush 13
Add an Object Parameter Edit the move. In. Circle method and click the create new parameter button Give it a name Choose a type OK? Programming in Alice © 2006 Dr. Tim Margush 14
move. In. Circle(which. Object) The parameter is named which. Object 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 ag dr Programming in Alice © 2006 Dr. Tim Margush 15
Test I got this error message when I tested my program The problem is in the call of this method We added a parameter We did not choose an argument Programming in Alice Click to select the argument © 2006 Dr. Tim Margush 16
Other Arguments Several additional choices come to mind How big of a circle? How long should it take? Which direction should it turn? move. In. Circle(which. Object, radius, duration, direction) Add more parameters! Programming in Alice © 2006 Dr. Tim Margush 17
Adding Parameters Note the Types Number, and Other Under Other, find Direction so you will be able to choose up, down, left, etc. Programming in Alice © 2006 Dr. Tim Margush 18
Adapting the Code The parameters will need to be dragged into the body of the method to replace the specific constants Convert radius to circumference Programming in Alice © 2006 Dr. Tim Margush 19
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 Programming in Alice © 2006 Dr. Tim Margush 20
Assignment World-level Methods How to create them How to call them When it is appropriate to use them Lab 05
- Cpmcd in software engineering
- Motivational methods and programs
- A-wax pattern recognition
- Darwin noticed that many organisms seemed well suited to
- Summerize
- Polynomially larger
- Which is larger
- Enlarged drawings show components larger than their
- What the purpose of the larger outer tube on a thermometer
- Why do cells divide, instead of simply growing larger?
- Polynomially larger
- Smaller to larger
- Why do cells divide? *
- Which is larger
- Why do cells divide instead of growing larger
- Is a mouse big or small
- Which cell is larger plant or animal
- What kind of star is the sun
- Bajo superlative
- Foraminifera
- Used to trim pieces of fabric the two handles are the same
- Make picture larger