1 / 19

Using Parameters

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

saxon
Download Presentation

Using Parameters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Parameters

  2. 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

  3. Add a second procedure • Actually, the penguin could be pushing any target object, so we will name the procedure pushObject

  4. 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.

  5. Move remaining distance • Penny bumps into the ball … maybe too much?

  6. Next statement penguin push Do in order penguin move forward to bump into the ball ball move forward to bump into the pin

  7. 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)

  8. 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

  9. Algorithm with a parameter pushObject Parameter:target Do in order penguin move forward (to bump into target) target move forward

  10. Click add parameter A parameter has a type (what kind of parameter is it?) and a name (a placeholder name)

  11. 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

  12. 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

  13. Other types • But the bowling ball is from the SSphere class, which is the type we select

  14. Parameter name The parameter type Provide a name for the parameter

  15. Parameter declaration • A parameter declaration consists of • <type> <parameterName> • SSphere target • We say “target is of typeSSphere”

  16. The parameter is now in scope • Select parameter from the instance menu

  17. Create instruction for the parameter

  18. pushObject procedural method

  19. 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.

More Related