Where are we?
Alice is a “toy” language
Fundamentals are exactly like “real” languages
Concepts of parameters, reuse, design are
classic
First half of the semester, we talked about
problems scientists and particularly computer
scientists solve
Second half we look at the fundamental tools
used in solving those problems
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-1
Computer Science – is it for you?
Some of you have a real talent for this. Computer
Science degrees earn $51K starting and over $100K
after a few years.
There are jobs now and only getting more!
Projected to increase by 38 percent over the next ten
years, which is much faster than the average for all
occupations. This occupation will generate about
324,000 new jobs, over the next decade, one of the
largest employment increases of any occupation.
Try CS 1400 next semester
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-2
Chapter 13:
Methods, Functions, and More about Variables
Starting Out with Alice:
A Visual Introduction to Programming
First Edition
by Tony Gaddis
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5.1
Writing Custom Class-Level Methods
If a class does not provide a method, a custom
method can be written. Have it YOUR way. A
VERY IMPORTANT CONCEPT
For example, we could have a method for our
bopper which goes to an item and bops! Write
the pseudocode
Primitive methods are available for various
classes - Move, Turn, Roll, Say, Think, etc.
Some classes have customized methods written
just for them --flap, hop, walk, jump
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-4
Suppose I want to create rollable wheelies –
where as they move forward the wheels move.
How would I get make it so my other programs
could use rollable wheelies?
What is a billboard? Give an instance where
you would want to use one.
What is a pose? Why would you want to use
one?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-5
5.1
Writing Custom Class-Level Methods
Class-Level Methods
– Methods that are part of a class
Create an instance of the class
– Select the instance
– Create new method
– Enter a name for the new method
(remember to use camelCase!)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-6
Try one
Takes any object and makes it jump
What is input?
What is the point of producing methods?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-7
Who “owns” the method?
You can also have ‘world’ methods – that
belong to the world.
The method should be part of the most ‘logical’
group. “I have rights to everything I am
using.”
If you make ‘disappear’ a part of the world,
when the magician is moved to another world,
he/she won’t know how to make objects
disappear.
Reuse – means creating to be used again!
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-8
5.2
Saving an Object to a New Class
Custom methods are for only a single instance of the class. If you “copy” the
instance, the new instance will also have the methods. If you create a new
instance or add to one of two instances, they don’t share methods.
If methods will be needed in other worlds, need to create a new class.
Saving as a new class, adds the new method to that object permanently.
For example, if you get a chicken to walk, you might want to allow the class
to be used by others (and yourself)
Programming becomes very creative.
The programmer, like the poet, works only slightly removed from pure
thought-stuff. He builds his castles in the air, from air, creating by exertion of
the imagination. Few media of creation are so flexible, so easy to polish and
rework, so readily capable of realizing grand conceptual structures ...
Frederic Brooks, Jr
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-9
5.2
Saving an Object to a New Class
IMPORTANT!!!
Right-click and Rename
Make
sure in
that
an object becomes a new class
Right-click
and
Save
the object
thewhen
object
(and
can be
used
in other
Object…
The
class
ends
has worlds) that is it independent
tree new
of
world
thethe
new
extension:
From
File,
choose .a2c
.a2w
like
Alicethe in the world and you use it in
Import…
If(NOT
it depends
onpress
anything
Select
file and
worlds
and
programs!)
the
new button
world,
it may not work since it can’t “find”
Import
that old world!
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-10
5.2
Inheritance
New methods were added and a new class was created.
The new class still contains the methods that had
previously been associated with the original object and
class (such as Move, Turn, Say)
The new class also contains the properties that had
previously been associated with the original object and
class (such as Color, Opacity, Vehicle)
The new class inherited all the original methods and
properties from the original class
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-11
5.3
Stepwise Refinement
Better to start out with general idea and then develop the
detail
Refined to have more detail added to it
Algorithm becomes several
methods
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-12
5.3
Stepwise Refine Example
Note the reuse
Original Algorithm:
Raise the right leg
Loop 10 times:
Do together:
Lower the right leg
Raise the left leg
End Do together
Do together
Lower the left leg
Raise the right leg
End Do together
End Loop
Lower the right leg
Modified Algorithm:
Call rightLegUp(+1) method
Loop 10 times:
Do together:
call rightLegUp(-1) method
call the leftLegUp (+1) method
End Do together
Do together
call leftLegUp(-1) method
call rightLegUp (+1) method
End Do together
End Loop
call rightLegUp(-1)_ method
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-13
5.3
Stepwise Refinement
The algorithm is not necessarily incorrect, it just needs
more detail
Development and use of new methods
means that code can sometimes be reused
Development and use of new
methods reduces the amount
of code written
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-14
5.4
Passing Arguments
Methods are written to accept arguments
Arguments are values that are passed
Example of arguments:
– Direction, distance traveled
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-15
5.4
Passing Arguments
Passing arguments requires the use of
parameters
Parameters
are variables that hold an argument that will be
passed (or used) into the method
Once created, the argument is required anytime
the method is called or used
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-16
Functions return a value where methods
don’t. For the following identify inputs and
possibly outputs:
Solo for beetle
Wheely moving around and turning near edge
Checking if wheelies have crashed
Making an object levitate
Making all objects disappear
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-17
Using the class parameters
It “real” languages, you are able to specify the
type of the parameter. That allows you to use
methods that all objects don’t necessarily have.
Not being able to do that is limiting
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-18
5.5
Using Class-Level Variables as Properties
Properties can be added to an object through the
creation of class-level variables
When the object is saved as a new class, the
variables are saved with it
Common properties are:
– color
– opacity
– isShowing
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-19
5.5
Using Class-Level Variables as Properties
Property is a variable that belongs to an object
Properties hold data about
that object and are referred
to as class-level variables
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-20
5.6
Writing Class-Level Functions
Function is a method that returns a value back to the
instruction
– such as: distance to
Additional data is required by the function to return
back a value
– for example…ExerciseGirl can runInPlace
forever, but she’ll get tired
– a function can be created…but it needs to
know how many repetitions it will take
before she gets tired AND how many
repetitions she has already completed
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-21
5.7
World-Level Methods and Variables
Rather than creating one long method…several
smaller world-level methods can be created
Each smaller method performs a specific part of
the overall animation
Can also create world-level variables
– These variables are available to all methods in the
world.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-22
5.7
World-Level Methods
Class-level methods give unique behaviors to
an object
World-level methods are methods created for
the object world
Worlds can have many methods
Commonly used to break a large complex
algorithm into small manageable pieces
– This is the divide and conquer approach
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-23
5.7
World-Level Methods
Meant to simplify the program
Once a method has been written, it can be used
many times
– Known as code reuse because the code is written
once and used many times to perform the task
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-24
5.7
World-Level Methods Example
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-25
5.7
World-Level Methods Example
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-26
5.7
World-Level Variables
Variable that belongs to
the world object
World-level variables act
as properties for the
world
Exist as long as the
world is running
(playing)
Available to all methods
in the world
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-27
5.8
Using Clipboards
Clipboards are
Only one clipboard appears
in Alice…the brown one is
locations for
what an empty clipboard
looks like and the white
storing a copy
version has information
stored in it.
of something
Use the clipboard in Alice to copy an instruction
from one method to another
– Drag the instruction tile to the clipboard
– Grab the clipboard and drag back to another method
– Very flakey – doesn’t work well with parameters
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-28
5.8
Need More Clipboards?
Increase the number of
clipboards from Edit Preferences
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-29
5.9
Tips for Visual Effects and Animation
Billboards
–
–
–
–
Graphical images that have been inserted into the world
Can insert images that are JPEG, GIF, TIF
Known as “Billboard”
Boy are you
Images are flat, 2D
shallow, you’re
with height and width
only 2D!
no depth
– Can be used as backgrounds
or scenery or used to give
instructions to the viewer!
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-30
5.9
Tips for Visual Effects and Animation
Fog
– Alice has a look of “mist”
to the world
– The entire world becomes
less visible
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-31
5.9
Tips for Visual Effects and Animation
Once an object has been
put into a position or pose,
the pose can be restored
during an animation
–Pose is a property of an objec
–setPose is a method of an object
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-32
5.9
Tips for Visual Effects and Animation
Programming the Camera
– Camera can be programmed just like other objects
– All the same primitive methods and functions are
available for it:
• Point at, Move, Turn, etc.
– Camera can also be a
vehicle for other objects!
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-33
5.9
Tips for Visual Effects and Animation
Creating Dummy Objects
– Dummy objects are invisible objects that are
placed in the world
– Camera then moves to the invisible object to
get different perspectives
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
5-34
Descargar

Chapter 5