Chapter 4, Part 1
Classes, Objects, and World-level Methods
Larger Programs
! As you become more skilled in writing programs, you will find that your code can quickly grow and multiply.
! You might have also noticed that you often use certain blocks of code (or variations of the code) over and over.
What’s a good strategy for solving a large, complex
problem?
! Break it down into smaller chunks (Divide and conquer)
! Object-oriented programming allows you to do this
Classes, Objects, & Methods ! Object-oriented programming uses basic
programming components to simplify the programming process: ! Classes, objects, methods and properties
! These components help to ! organize a large program and make it easier
to understand ! think about an intricate program in
more general terms (abstraction) ! find and remove errors (bugs)
In our programs, we have been using…
! Classes ! In Alice, classes are predefined 3D models
(templates) ! Objects
! An object is an instance of a class. Class: Frog (Uppercase name) Objects: frog, frog2, frog3, frog4 (lowercase names)
We have also used…
! Built-in methods (primitive) ! Examples: move, turn, roll, turn to face, say
! World.my first method ! Example: Snowman & Snowwomen Program - we wrote code
where the snowman tried to get the attention of a snowwoman.
All of the program code was written in this one method (see next slide)
Potential Problem with Snow People program:
! Code just continues to grow and grow. ! If we continue in this fashion, the
program will become longer and more difficult to read and maintain.
Solution
! Organize the instructions into smaller methods.
! A possible storyboard for program:
Do in order
catchAttention – snowman tries to get the attention of the snowwoman
blinkEyes – snowwoman turns to look and the snowman blinks his eyes
react – snowwoman blushes and turns away and the snowman
is disappointed
Next Step
! Each major task must be broken down into simpler steps. ! Example:
catchAttention
Do in order
snowman waves his hands
snowman says "Ahem"
snowwoman turns to face snowman
Stepwise Refinement
! The process of breaking a problem down into large tasks and then breaking those tasks into simpler steps and refining them is called stepwise refinement.
! Once the storyboard (or design) is complete, we write our own method for each task. ! You use the simple, primitive
methods to create a more sophisticated method
Why Write Our Own Methods? ! It saves time -- we can call the method over and
over without reconstructing code ! It reduces code size – we call the method
rather than copying multiple lines of code over and over
! It allows us to "think at a higher level" ! We can think catchAttention instead of “wave arms, then say ‘Ahem’"
! The technical term for "think at a higher level” is "abstraction"
Abstraction
How to Create a New Method:
Select the World tile in the Object Tree (associates the new method with the World)
Select the methods tab in the details area
• Click on the "create new method" button
Work Together: ! Create a method that has the snowman tip
his hat to the snowwoman, and then the snowwoman tips her hat to the snowman
! Name of method: tipHats ! It’s a world-level method because it
involves more than one object (snowman and snowwoman)
! The tipHats method is executed by calling (invoking) the method
Work Together:
Assignment
! Read Chapter 4, Section1, pgs. 89-100 ! Complete vocabulary worksheet