Classes Objects Methods Chapter 4 Object Oriented Programming

Classes / Objects / Methods Chapter 4

Object Oriented Programming n n n Divides a program into Modules/Classes Uses Classes and Objects to simulate real world environments Basic Programming Components • Classes • Objects • Methods

Class n Specifies the definition of a particular kind of object • Frog • Snowman • Cell phone n n Blueprint / design for how to create an object of that type Class names begin with a Capital letter

Class n Properties • Characteristics n Methods • Behaviors

Objects n n Specific Instances of a Class Implementation of the Class Definition • Spot one specific dog • Frosty one specific snowman n All specific objects of the same class share the same definition • Same Properties – can be modified • Same Methods – can be modified n Object names begin with lowercase letter

Dog Object n All Instances of Class Dog Have • Properties n n Name Breed Weight Color • Methods n n n Walk Bark Jump n Each Instance Has • Properties n n Spot Mutt 10 pounds Black • Methods Walk n Bark n Jump (can be modified to fit Spot’s requirements) n

Methods n n n Sequence of Instructions Tasks that can be performed Some are predefined within the Class • Dog has a bark method n n Others are Created or Modified Methods organize the program • Divide the program into manageable pieces

Parameters n n n Pieces of Information that need to be sent to the Method Turn Toward (what) Turn Toward Snowwoman

Levels of Methods n Global Methods (World Level) • Methods that reference more than one object n Class Methods • Methods that define a behavior for one single object

Create New Method n Snowpeople (problem 4 -1) • flips. Hat n n n Raises right arm Grabs Hat Tips toward snowwoman Returns Hat to Head World Method • • • Uses snowman and snowwomen World Method Create New Method Name flips. Hat


Add Method Instructions

Calling a Method n n n New Method must be “Called” Program Instructions must indicate to Execute the new Method In Alice • Drag Method into World. My. First. Method n In Other Languages • Call the Method using Method Name


Parameters n Allows communication with Methods • Communicate Values (Numbers, Colors) • Communicate Object Names

Create Method with Parameter n Create Method called “dance” • Object will jump up and down and spin around n Create Parameters • Parameter 1 n Which object to dance • Parameter 2 n How many times to Spin

Creating Parameter n Which object to Dance • Name Arbitrary n Placeholder – holds place of the actual object the will be used in the method n • Type: Number n Boolean (True/False) n Object n Other (Sound, Color, Etc. ) n

Parameter n n Parameters show in Upper Left of Method Panel When Method is Called (Used) • Object must be sent as an Argument to Method

Use Parameter in Method

Calling Method with Parameter n n n When using Method you must include Parameter Use dance method after flip. Hat Example 1: Requires an object to dance

Multiple Parameters n Add Second Parameter – How many times to Spin • Name: number. To. Spin • Type: Number • Use in the Turn Instruction

Modify Method Call n n Change 2 nd Parameter Pick any 2 different numbers

Create Another Method n Create a Method for Snowman Blinking His Eyes • Is this a World / Global Method • Is this a Class Method n n Why How is it Created?

Homework n Chapter 4, Section 1 (4 -1) • #3 – Gallop and Jump • #4 – Helicopter Flight n Chapter 4, Section 2 (4 -2) • #6 – Frog Escape Bring electronic copy of homework to class Bring printed copy of homework to submit

Class Methods n n n Class Methods are methods that define a behavior for one single class object. Select Horse Object Methods Create New Method gallop

Guidelines for Class Methods n n Create lots of class methods as necessary Do not call world-level methods from within a class-level method Do not use instructions for other objects from within a class-level method You can use parameters to send necessary information instead of referring to other specific objects from within the method.

Inheritance n n n Creating a class on a previously defined class Adding functionality to existing class by defining new methods or editing existing ones Different members of design team can work on different methods for the class

Creating a New Class in ALICE n Save object with new class methods as a New Class • Code Reuse • Galloping Horse n 2 Step Process • Rename existing object in Object Tree • Save As a New Class

Rename Object n n Select the Object in the Object Tree Right-Click Rename Select a New Meaningful Name for Object • Galloping. Horse

Save As New Class n n Select the Object in the Object Tree Right-Click Save Object Navigate to folder where you want to save the object Other objects: AliceRequiredGallery. a 2 c extension Alice 2. 0 class

New Class n n New Class (Galloping. Horse) is now available in Object Gallery All objects of Galloping. Horse • all methods of horse • class-method gallop

Homework n Exercise 4 -3 • #10 • #11 use a say instruction not a sound file so all worlds don’t have to import sound file • Bring electronic copy of homework to class • Bring printed copy of homework to submit
- Slides: 32