190 likes | 331 Views
Using Parameters. Decompose. Do in order penguin move forward (toward the ball) penguin swagger penguin push the ball. How does the penguin “ push ” the ball?. penguin push Do in order penguin move forward to bump into the ball
E N D
Decompose Do in order penguin move forward (toward the ball) penguin swagger penguin push the ball • How does the penguin “push” the ball? penguin push Do in order penguin move forward to bump into the ball ball move forward to bump into the pin
Add a second procedure • Actually, the penguin could be pushing any target object, so we will name the procedure pushObject
Implement penguin pushObject Do in order this (penguin) move forward to bump into the <target> <target>move forward to bump into the pin The ball was originally 1 meter in front of penny. In myFirstMethod, penny moved forward 0.75 meters.
Move remaining distance • Penny bumps into the ball … maybe too much?
Next statement penguin push Do in order penguin move forward to bump into the ball ball move forward to bump into the pin
Problem: Where is the ball? Only reference is to this penguin We are working in the Penguin class We say the ball is out-of-scope (unavailable to be used in the penguin class)
A parameter is used to send information to a procedure when the procedure is called (invoked) • Most built-in Alice statements use parameters • move: direction, distance • turn: direction, angleInRotations
Algorithm with a parameter pushObject Parameter:target Do in order penguin move forward (to bump into target) target move forward
Click add parameter A parameter has a type (what kind of parameter is it?) and a name (a placeholder name)
Select Type • Available types are listed in a drop-down menu • Specifies the kind of information the parameter will represent numeric boolean string of text Gallery Classes
Inheritance Hierarchy • The inheritance hierarchy shows the parent classes of our objects • We see that Penguin is a Flyer • BowlingPin is a Prop • We could use these any of these classes (parents or children) as types for our parameter
Other types • But the bowling ball is from the SSphere class, which is the type we select
Parameter name The parameter type Provide a name for the parameter
Parameter declaration • A parameter declaration consists of • <type> <parameterName> • SSphere target • We say “target is of typeSSphere”
The parameter is now in scope • Select parameter from the instance menu
Scene class: Test pushObject • Drag the pushObject tile into the editor • Select an argument (actual parameter) that will be sent to the parameter (formal parameter). We see that we can only choose ball, as it is the only instance of the Ssphereclass we have in our program.