chapter 4, part 1 classes, objects, and world-level methods · classes, objects, & methods !...

17
Chapter 4, Part 1 Classes, Objects, and World-level Methods

Upload: others

Post on 24-Aug-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

Chapter 4, Part 1

Classes, Objects, and World-level Methods

Page 2: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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.

Page 3: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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

Page 4: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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)

Page 5: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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)

Page 6: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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)

Page 7: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming
Page 8: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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.

Page 9: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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

Page 10: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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

Page 11: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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

Page 12: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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"

Page 13: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

Abstraction

Page 14: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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

Page 15: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

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

Page 16: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

Work Together:

Page 17: Chapter 4, Part 1 Classes, Objects, and World-level Methods · Classes, Objects, & Methods ! Object-oriented programming uses basic programming components to simplify the programming

Assignment

!  Read Chapter 4, Section1, pgs. 89-100 !  Complete vocabulary worksheet