Parameters - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Parameters

Description:

Parameters Alice Overview The need for more flexible methods Passing a parameter to a method Demos Using the Alice interface to write code for a parameter object A ... – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 16
Provided by: Wand47
Category:

less

Transcript and Presenter's Notes

Title: Parameters


1
Parameters
  • Alice

2
Overview
  • The need for more flexible methods
  • Passing a parameter to a method
  • Demos
  • Using the Alice interface to write code for a
    parameter object

3
A beetle band
  • Our task is to create an animation for a bug band
    as an advertisement for their next concert.
  • In the animation, each band member will perform
    a short solo.

4
Storyboards
  • Each bug band member will perform a solo.

Do together Do in order
georgeBeetle move up georgeBeetle
move down play sound
Do together Do in order
ringoBeetle move up ringoBeetle move
down play sound
Do together Do in order paulBeetle
move up paulBeetle move down
play sound
Do together Do in order
lennonBeetle move up lennonBeetle
move down play sound
5
Code for georgeBeetle solo
  • This code will only work for georgeBeetle.
  • Since each band member performs a solo,
  • we will need four versions of the code.

6
A Better Solution
  • Four versions of very similar code seems a bit
    tedious. The only things that change are the
    beetle and the music that plays.
  • A better solution is to write a more flexible
    method.

7
Parameters
  • Built-in methods provide flexibility by providing
    parameters such as distance and direction.
  • Parameters allow you to pass in values (we call
    the values arguments).
  • Example
  • Parameters distance, direction
  • Arguments 0.5 meters, 0.5 seconds

8
Kinds of Parameters
  • Alice provides several kinds of parameters that
    can be used in your own methods.

9
The storyboard
Back to our problem We want to write only
one method and use parameters to specify which
band member is to perform and which music
should be played.
  • Solo
  • Parameters bandMember, music
  • Do together
  • Do in order
  • bandMember move up
  • bandMember move down
  • play music

10
Demo
  • Demonstration of creating a method named solo
    that can be used for any band member.
  • We will need
  • an Object parameter -- which band member is to
    play a solo
  • a Sound parameter -- which music should
    be played

11
World.solo with parameters

.
12
Calling the solo method
Note that in each call to the method, arguments
must be given for both parameters.
13
A Number parameter
Let's add a Number parameter to specify the
height the bandMember jumps up and down.
14
Demo
  • Demonstrate the use of a Number parameter to
    specify the height the bandMember is to jump

15
Assignment
  • Read Chapter 4-2
  • Parameters
  • Lab 4-2
Write a Comment
User Comments (0)
About PowerShow.com