Classes, Objects and Methods

World-level Classes
Chapter 6 Part 2
Programs Grow



Program code grows larger over time
This makes it more difficult to read and
process the code in our minds
Games and other software can have
thousands, even millions of lines of code
2
Basic Components

Classes, objects, and methods in object-oriented
programming help to
o
o
o
Organize a large program into small modules
Design and think about an intricate program
Find and remove errors (bugs)
3
World Level Method



Method that can reference entire World
Has instructions involving more than one object
(cleverSkater, penguin)
Class-level methods only involve one object
4
Why Write World Level Methods?

No method does what you need
o

Saves time
o

Can call method many times without making your own code to
do same thing
Reduces code size
o

You need new functionality
Call method instead of rewriting code
Allows thinking at a higher level
o
You think of method names instead of what method does

o
Ex: sharkSwims, sharkEatsFish, etc.
Allows abstraction – "thinking at a higher level"
5
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
6
Edit or Read Method


Click on edit button to edit or read method code
Can open method by double clicking on it in method editor
7
Example: A Beetle Band

Create an animation for a bug band as an
advertisement for their next concert
8
Demo


BeetleBand-v1
Concepts illustrated
o
To play a sound, a sound file must first be imported into
Alice

o
o
Alice is not a sound editor
This code is only for georgeBeetle
Three more methods (one for each band member) will
be needed
9
Algorithms: Playing Solo

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
10
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 using parameters
11
Beetles Algorithm


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
12
Place bandMember in Method



Create bandMember parameter as object
Drag bandMember parameter to editor
Menu pops up where you can choose method to use
13
Alice Program for Solo


Notice that bandMember is an object parameter
Music is a sound parameter
14
Drag solo to Editor
Fill in parameters:
15
BeetleBand-v2

New solo method is added for each band member
and music to play
16
Demo


BeetleBand-v2
Concepts illustrated
o
Enter name and select type for each parameter


o
Arguments passed to parameters in called method


o
bandMember is an Object parameter
music is a Sound parameter
bandMember: georgeBeatle, lennonBeatle, paulBeetle,
ringoBeatle
What are the arguments passed into music parameter?
Parameters are placeholders

bandMember and music get new values when called from
arguments
17
Homework


Read chapter 6 section 7
Do chapter 6b lab. Due next class.
18