Download presentation
Presentation is loading. Please wait.
1
World-level Methods with Parameters
Alice
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
Adding spinning wheels would be fun, but complicate our example
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 Programming in Alice © 2006 Dr. Tim Margush
13
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! Programming in Alice © 2006 Dr. Tim Margush
14
Add an Object Parameter
Edit the moveInCircle method and click the create new parameter button Give it a name Choose a type OK? Programming in Alice © 2006 Dr. Tim Margush
15
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 Programming in Alice © 2006 Dr. Tim Margush
16
Click to select the argument
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 Click to select the argument Programming in Alice © 2006 Dr. Tim Margush
17
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! Programming in Alice © 2006 Dr. Tim Margush
18
Adding Parameters Note the Types Number, 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
19
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 Convert radius to circumference Programming in Alice © 2006 Dr. Tim Margush
20
Advantages of Methods Abstraction Code reuse Generality
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
21
Assignment World-level Methods Lab 05 How to create them
How to call them When it is appropriate to use them Lab 05
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.