World-level Classes and Methods Chapter 6
Programs Grow Code seems to grow and grow Makes program more difficult to read and process in our minds Games and other software can have thousands, even millions of lines of code
Basic Components Classes, objects, and methods in object-oriented programming These components help to o Organize a large program into small modules o Design and think about an intricate program o Find and remove errors (bugs)
Classes and Objects Classes o In Alice, classes are predefined as 3D models Objects o An object is an instance of a class Class: Frog (Uppercase first letter) Objects: frog, frog1, frog2, frog3 (lowercase first letter)
Methods Built-in (predefined) methods o Examples: move, turn to face, say Custom methods o User creates own method
Stepwise Refinement Process of breaking problem into smaller tasks o Then break each task into simpler steps Once storyboard completed write method for each task
Lunar Robot Example
Lunar Robot Using Methods Organize instructions into smaller methods A possible storyboard for Lunar Robot example Do in order surprise spiderrobot and alienonwheels surprise each other investigate spiderrobot gets a closer look at alienonwheels react alienonwheels hides and spiderrobot sends message
surprise Method Break down each major task into simpler steps o Example for surprise method: surprise Do in order alienonwheels moves up alienonwheels says "Slithy toves?" spiderrobot's head turns around
Demo: Starting a new method First 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
World.surprise Code Editor Click on tabs to switch between methods
World Level Method Method for entire World Has instructions involving more than one object (spiderrobot, alienonwheels)
Demo First Encounter Concepts illustrated in this example world: o surprise is a world-level method Executed by calling (invoking) the method
Starting Method Can run surprise by self by clicking down arrow to right of method in Events
Method investigate Instructions that moves robot and legs one meter at time
Method react Alien moves down, Robot informs Houston
Edit or Read Method Click on edit button to edit or read method code
Combine all Methods 2 instances of investigate used to move 2 meters Can use loop to move more than 2 meters
Deleting Methods Delete all calls to method Close its edit window o Right click edit window and select close Drag name to trash bin
Why Write Own Methods? No method does what you need o You need new functionality Saves time o Can call method many times without making your own code to do same thing Reduces code size o Call method instead of rewriting code Allows thinking at a higher level o o Can think surprise instead of The alien moves up and says Slithy toves? and then the robot's head turns around. Abstraction "thinking at a higher level"
Example: A Beetle Band Create an animation for a bug band as an advertisement for their next concert
Demo BeetleBand-v1 Concepts illustrated o To play a sound, a sound file must first be imported into Alice Alice is not a sound editor o This code is only for georgebeetle o Three more methods (one for each band member) will be needed
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
A Better Solution Creating four versions of similar code is tedious Only things that change are band member and music played Better solution o Write more flexible method
Beetles Storyboard One method can be used Use parameters to specify Which band member will perform Which music should be played solo Parameters: bandmember, music Do together Do in order bandmember move up bandmember move down play music
Parameters Built-in methods give flexibility by providing parameters like distance and direction Parameters allow values (arguments) to be passed to methods o Example Parameters: direction, distance, duration Arguments: up, 0.5 meters, 0.5 seconds Where duration is optional
Kinds of Parameters Alice provides several kinds of parameters that can be used in custom methods o Number, Boolean, Object, Other Create parameter using Create parameter bandmember is passed as Object parameter
Place bandmember in Method Drag bandmember to editor
Create More Parameters Click on create new parameter
Drag bandmember to Editor Fill in parameters:
Placeholders A value is passed to a method with an argument Parameters act as placeholders for the argument o Takes value of argument being passed and uses it in method o Parameter name is likely to be different than argument Example: in move method o You pass arguments that are specific direction and distance to move (ex: up, 0.5 meters) o Those specific arguments are passed to the move method s parameters (direction and distance)
BeetleBand-v2 New solo method is added for each band member and music to play
Demo BeetleBand-v2 Concepts illustrated o Enter name and select type for each parameter bandmember is an Object parameter music is a Sound parameter o Arguments passed to parameters in called method bandmember: georgebeatle, lennonbeatle, paulbeetle, ringobeatle What are the arguments passed into music parameter? o Parameters are placeholders bandmember and music get new values when called
Homework Read chapter 6 sections 3,4,6,7 Answer questions in handout Do lab assignments after handing in answered questions Due one week after assigned with 1 week grace