World-level Methods with Parameters Alice. Larger Programs As you become more skilled in writing...

21
World-level Methods with Parameters Alice

Transcript of World-level Methods with Parameters Alice. Larger Programs As you become more skilled in writing...

World-level Methodswith Parameters

Alice

Larger ProgramsAs 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 programTo 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 catchAttention

Then, we can drag-and-drop the catchAttention 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 catchAttention method

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)

Using the catchAttention method

The catchAttention 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 “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"

Programming in Alice © 2006 Dr. Tim Margush 12

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 whichObjectIt 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 placedrag

Programming in Alice © 2006 Dr. Tim Margush 16

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

Programming in Alice © 2006 Dr. Tim Margush 17

Other Arguments

Several additional choices come to mindHow 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 TypesNumber, 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

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

AbstractionMethods allow the programmer to organize details of a task under the method's name

Code reuseMethods can be called several times without having to copy the details of the code

GeneralityParameters allow the same actions to be applied to a variety of objects

Debugging and maintenanceFixing or changing part of a program may only require modification of a single method

Assignment

World-level MethodsHow to create them

How to call them

When it is appropriate to use them

Lab 05