CompSci 4Chap 4 Sec 2Feb. 10, 2005Prof. Susan RodgerNote: Thanks to Wanda Dann and Steve Cooper for slide ideasAnnouncements• Assignment 3 due today!• Assignment 4 outWhat we will do today• Chapter 4 Sec 2 – Parameters• ClassworkOverview• The need for more flexible methods• Creating methods with parameters• Passing arguments to methods•Demo– Using Alice – methods with parametersA Beetle Band• Create an animation for a bug band as an ad for their next concert• Each band member will perform a short soloStoryboards• Each bug member will perform a soloCode for georgeBeetle• We will need four versions of the code, one for eachband member• This code will only work for georgeBeetleA Better Solution• Four versions of similar code is tedious.• The things that change are– The beetle– The music the beetle plays• Better Solution: write a more flexible methodParameters• Built-in methods provide flexibility by providing parameters such as distance and duration• Parameters allow you to pass in values – The values are arguments• Example:Parameters: distance, durationArguments: 0.5 meters, 0.5 secondsTypes of Parameters• Alice provides several types of parameters that can be used in your methods.The Storyboard• Write one method and use parameters for– Which bandmember to perform– Which music to playSolo:Parameters: bandMember, musicDo togetherDo in orderbandMember move upbandMember move downplay musicCreating a Method Named Solo• Can be used for any band member• Will need– An Object parameter – which band member is to play a solo– A Sound parameter – which music should be playedWorld.solo with parametersCalling the solo method• Note that in each call, arguments must be given for both parametersA Number Parameter• Add a number parameter for height the bandMember jumps up and downDemoClasswork Today• Create worlds– Beetle band duet–Magic
View Full Document