Classes, Objects, and World-level Methods

Download Report

Transcript Classes, Objects, and World-level Methods

Classes, Objects, and
World-level Methods
Alice
Class / Object
Class
A template describing the characteristics
(properties, methods, and functions) of an
object of a specific type
Object
An instance of a class; each object of one
class has identical characteristics, but
property details may vary
Programming in Alice
© 2006 Dr. Tim Margush
2
Method / Function
Method
A programming unit that can be executed
on demand by using its name;
corresponds to a sequence of actions
Function
A programming unit that can be executed
on demand by using its name; represents
a value
Programming in Alice
© 2006 Dr. Tim Margush
3
Classes, Objects, & Methods
Object-oriented programming uses
classes, objects, and methods as basic
programming components.
These components help you to
design programs
think about an intricate program
test programs
find and remove errors (bugs)
In your programs,
you have been using…
Classes
In Alice, classes are predefined as 3D models
Objects
An object is an instance of a class.
Capitalization is a matter of Style
Classes
Classes: Penguin
(Uppercase name)
Objects
Objects: penguin,
penguin1, penguin2,
opus (lowercase
names)
In your programs,
you have also used…
built-in (predefined) methods
Examples: move, turn to face, say
World.my first method
Example:
In the snowpeople world (Chapter 2), we
wrote program code where the snowman
tried to get the attention of a
snowwoman.
As a reminder, see next slide…
Adding Instructions to world.my
first method
The method should be open in the method editor
window of Alice. (bottom right)
if not select the world from the object tree, the
method tab, and click the edit button next to my first
method
Elements of Visual
Programming
Animation Programs
8
Method Editor Window
Elements of Visual
Programming
Animation Programs
9
Step 1 – Snowman turns
Select the object you want to
perform the object
Select the method / action you
want the object to perform
could use turn or turn to face
often many ways to accomplish
the same task
Click and drag it to the method
editor window
Elements of Visual
Programming
Animation Programs
10
Snowman Turns
Can adjust aspects of how the snowman
turns to face the snowwoman
click the more option
right now duration and style are the only
things you should alter
Elements of Visual
Programming
Animation Programs
11
Step 2 – Combined Action
We want the snowman to say “ahem” and
blink at the same time
actions are normally sequential
to do actions together, at the same time,
use a “Do together” block
Click and drag “Do
together” block into
the method
Elements of Visual
Programming
Animation Programs
12
Affecting subparts
The snowman does not have “blink
eyes” method
Can accomplish a blink by affecting
subparts
Select snowman object from object tree
and expand subparts
expand the head
now we can give commands to individual
parts, in this case the eyes
have eyes move up and down
specify direction and distance of move
Elements of Visual
Programming
Animation Programs
13
Step 2 – First Attempt
world.my first method looks like this
TEST the method
play the movie. Does it do what we want?
Elements of Visual
Programming
Animation Programs
14
Logic Error
The program works, but does not do what
we intended.
This is an example of a logic error
very easy in Alice to see logic errors
the movie does not do what we wanted
What’s the problem?
Elements of Visual
Programming
Animation Programs
15
Do together and Do in order
All commands in the Do together block are
executed simultaneously
So what is the result if you move an eye up .1
meters and down .1 meters at the same time?
Apparently nothing
So while we want the eyes to move together and
to say “ahem” we want the eyes to first move up
and then down
Use a Do in order block inside the Do together
block
Elements of Visual
Programming
Animation Programs
16
Corrected Do Together
Elements of Visual
Programming
Animation Programs
17
Larger Programs
As you become more skilled in writing
programs, you will find that your programs
quickly begin to increase to many, many
lines of code.
Games and other "real world" software
applications can have thousands, even
millions of lines of code.
In this session, we begin to look at
organizing large programs into small
manageable pieces.
Goals of OOP
Organize a large program into
small pieces
Design and think about an
intricate program
Reusable code allows for well
written piece of code to work in
more than one program
Test find and remove errors
(bugs)
Modifying the program
To make the snowpeople animation more
realistic, we might want the snowman to be a
little less shy. In our original program, the
snowman tries to catch the snowwoman's
attention only once. Perhaps he could try to get
her attention more than once.
To make this modification, additional lines
would be added to the code.
This would make the program code longer
and more difficult to read and think about.
A Solution
A solution to the problem is to
define our own method
name the new method catchAttention
Then, we can drag-and-drop the
catchAttention method into the edit box
just like the built-in methods
Demo: The solution
First, to 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
Demo
A demonstration of writing the
catchAttention method
World-level method
catchAttention is a world-level method because it
is defined as a method for World
has instructions that involve more than one object
(snowman, snowwoman, camera)
Using the catchAttention method
The catchAttention method is executed by calling
(invoking) the method from my first method
Why?
Why do we want to write our own methods?
saves time -- we can call the method again and
again without reconstructing code
reduces code size – we call the method rather than
writing the instructions again and again
allows us to "think at a higher level"
can think “catchAttention" instead of
“turn head to face the camera, then say ‘Ahem’ while moving eyes
up and down"
the technical term for "think at a higher level" is
"abstraction"
Assignment
Read Tutorial04
World-level Methods
How to create them
How to call them
When it is appropriate to use them
Lab04