Lecture notes (4-1)

Download Report

Transcript Lecture notes (4-1)

CS320n –Visual Programming
Classes, Objects, and
World- Level Methods
Mike Scott
(Slides 4-1)
What We Will Do Today
• Look at ways of doing the fish problem
from last time
• Define Classes, Objects, and Methods in
Alice
• Define and create world level methods
• more complicated snowman movie
Visual Programming
Classes, Objects, and World Level
Methods
2
Fish Circling Around Island
Smooth
Jagged
Play Movie
Visual Programming
Classes, Objects, and World Level
Methods
3
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.
– Fish program about 30 already
– How many things can you keep in short term memory?
• Games and other "real world" software
applications can have thousands, even millions of
lines of code.
• Want to organize large programs into small
manageable pieces
– big rocks into little rocks
Visual Programming
Classes, Objects, and World Level
Methods
4
Classes, Objects, and Methods
• Alice uses classes, objects, and methods
as basic programming components.
– it is just one way to think about programming
• object oriented
– there are many others
• These components help you to
– organize a large program into small pieces
– design and think about an intricate program
– find and remove errors (bugs)
Visual Programming
Classes, Objects, and World Level
Methods
5
•
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.
Class: Fish (Uppercase name)
Objects: fish, fish1, fish2, fish3
(lowercase names)
Visual Programming
Classes, Objects, and World Level
Methods
6
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…
Visual Programming
Classes, Objects, and World Level
Methods
7
Snowman Program
Visual Programming
Classes, Objects, and World Level
Methods
8
Snowman Program
Visual Programming
Classes, Objects, and World Level
Methods
9
More on the Snowman Program
• Snowwoman
blushes and then
turns back to her
friend
• Snowman turns
away and shakes
his head
• Show Movie
Visual Programming
Classes, Objects, and World Level
Methods
10
Modifying the program
• Modify program to get snowman to try to
get snowwoman’s attention twice
• To make this modification must add more
lines of code
– program getting longer, more complicated,
more difficult to think about and understand,
harder to make changes
– program is suffering a “code explosion”
• Show movie of trying to get attention
twice
Visual Programming
Classes, Objects, and World Level
Methods
11
Try twice to get attention
• Notice the wait
command in
between the two
attempts to get
the
snowwoman’s
attention
– wait is one of the
commands at
the bottom of the
method editor
Visual Programming
Classes, Objects, and World Level
Methods
12
Why is this “Bad”?
• Not hard to copy and paste code, right?
• Easy to simply say “make copy” of the Do
together block and insert a wait
• … but, getting a little harder to maneuver
around the program with all that code
• … and what if we want to change
something about the getting attention
behavior?
– blink eyes twice or change duration of blink
Visual Programming
Classes, Objects, and World Level
Methods
13
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
Visual Programming
Classes, Objects, and World Level
Methods
14
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
Visual Programming
Classes, Objects, and World Level
Methods
15
Demo
• A demonstration of writing the
catchAttention method
Visual Programming
Classes, Objects, and World Level
Methods
16
World-level method
• catchAttention is a world-level method
because it
-is defined as a method for World
• if the method has instructions that involve
more than one object it must be a world
level method
• This method only involves the snowman so
we could have made it a method for the
snowman
Visual Programming
Classes, Objects, and World Level
Methods
17
Using the catchAttention method
The catchAttention method is executed by calling
(invoking) the method from my first method
Visual Programming
Classes, Objects, and World Level
Methods
18
Testing Method Independently
• Can test method by adding event to the
program
• disable other methods
Visual Programming
Classes, Objects, and World Level
Methods
19
Why?
• Why do we want to write our own methods?
– saves time -- we can call the method again and again
without reconstructing code
– Easy to make changes to behavior
• only have to change in one place
– 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
“say “Toot” moving eyes up and down"
• the technical term for "think at a higher level" is
"abstraction"
Visual Programming
Classes, Objects, and World Level
Methods
20
Exercise for Class
• Do problem Chapter 4, exercise 2,
Confused Kanga on page 110 of the book
– see web for description
Visual Programming
Classes, Objects, and World Level
Methods
21