classes / objects / methods chapter 4. object oriented programming divides a program into...
TRANSCRIPT
Classes / Objects / Classes / Objects / MethodsMethods
Chapter 4Chapter 4
Object Oriented ProgrammingObject Oriented Programming
Divides a program into Divides a program into Modules/ClassesModules/Classes
Uses Classes and Objects to simulate Uses Classes and Objects to simulate real world environmentsreal world environments
Basic Programming ComponentsBasic Programming Components• ClassesClasses• ObjectsObjects• MethodsMethods
ClassClass
Specifies the definition of a particular Specifies the definition of a particular kind of objectkind of object• FrogFrog• SnowmanSnowman• Cell phoneCell phone
Blueprint / design for how to create Blueprint / design for how to create an object of that typean object of that type
Class names begin with a Capital Class names begin with a Capital letterletter
ClassClass
PropertiesProperties• CharacteristicsCharacteristics
MethodsMethods• BehaviorsBehaviors
ObjectsObjects
Specific Instances of a ClassSpecific Instances of a Class Implementation of the Class DefinitionImplementation of the Class Definition
• Spot Spot one specific dog one specific dog• Frosty Frosty one specific snowman one specific snowman
All specific objects of the same class share All specific objects of the same class share the same definitionthe same definition• Same Properties – can be modifiedSame Properties – can be modified• Same Methods – can be modifiedSame Methods – can be modified
Object names begin with lowercase letterObject names begin with lowercase letter
Dog ObjectDog Object All Instances of Class All Instances of Class
Dog HaveDog Have• PropertiesProperties
NameName
BreedBreed WeightWeight ColorColor
• MethodsMethods WalkWalk BarkBark JumpJump
Each Instance HasEach Instance Has
• PropertiesProperties SpotSpot MuttMutt 10 pounds10 pounds BlackBlack
• MethodsMethods WalkWalk BarkBark JumpJump(can be modified to fit (can be modified to fit
Spot’s requirements)Spot’s requirements)
MethodsMethods
Sequence of InstructionsSequence of Instructions Tasks that can be performedTasks that can be performed Some are predefined within the ClassSome are predefined within the Class
• Dog has a bark methodDog has a bark method Others are Created or ModifiedOthers are Created or Modified Methods organize the programMethods organize the program
• Divide the program into manageable Divide the program into manageable piecespieces
ParametersParameters
Pieces of Information that need to be Pieces of Information that need to be sent to the Method sent to the Method
Turn Toward (what)Turn Toward (what) Turn Toward SnowwomanTurn Toward Snowwoman
Levels of MethodsLevels of Methods
Global Methods (World Level)Global Methods (World Level)• Methods that reference more than one Methods that reference more than one
objectobject Class MethodsClass Methods
• Methods that define a behavior for one Methods that define a behavior for one single objectsingle object
Create New MethodCreate New Method Snowpeople (problem 4-1)Snowpeople (problem 4-1)
• flipsHatflipsHat Raises right armRaises right arm Grabs HatGrabs Hat Tips toward snowwomanTips toward snowwoman Returns Hat to HeadReturns Hat to Head
World Method World Method • Uses snowman and snowwomenUses snowman and snowwomen• WorldWorld• MethodMethod• Create New MethodCreate New Method• Name Name flipsHat flipsHat
Add Method InstructionsAdd Method Instructions
Calling a MethodCalling a Method
New Method must be “Called”New Method must be “Called” Program Instructions must indicate to Program Instructions must indicate to
Execute the new MethodExecute the new Method In AliceIn Alice
• Drag Method into World.MyFirstMethodDrag Method into World.MyFirstMethod In Other LanguagesIn Other Languages
• Call the Method using Method NameCall the Method using Method Name
ParametersParameters
Allows communication with MethodsAllows communication with Methods
• Communicate Values (Numbers, Colors)Communicate Values (Numbers, Colors)
• Communicate Object NamesCommunicate Object Names
Create Method with ParameterCreate Method with Parameter
Create Method called “dance”Create Method called “dance”• Object will jump up and down and spin aroundObject will jump up and down and spin around
Create ParametersCreate Parameters• Parameter 1Parameter 1
Which object to danceWhich object to dance
• Parameter 2Parameter 2 How many times to SpinHow many times to Spin
Creating ParameterCreating Parameter
Which object to DanceWhich object to Dance• NameName
Arbitrary Arbitrary Placeholder – holds place of the actual Placeholder – holds place of the actual
object the will be used in the method object the will be used in the method
• Type:Type: NumberNumber Boolean (True/False)Boolean (True/False) ObjectObject Other (Sound, Color, Etc.)Other (Sound, Color, Etc.)
ParameterParameter
Parameters show in Upper Left of Method Parameters show in Upper Left of Method PanelPanel
When Method is Called (Used)When Method is Called (Used)• Object must be sent as an Argument to Method Object must be sent as an Argument to Method
Use Parameter in MethodUse Parameter in Method
Calling Method with ParameterCalling Method with Parameter
When using Method you must include When using Method you must include ParameterParameter
Use dance method after flipHatUse dance method after flipHat Example 1: Requires an object to Example 1: Requires an object to
dancedance
Multiple ParametersMultiple Parameters
Add Second Parameter – How many Add Second Parameter – How many times to Spintimes to Spin• Name: numberToSpinName: numberToSpin• Type: NumberType: Number• Use in the Turn InstructionUse in the Turn Instruction
Modify Method CallModify Method Call
Change 2Change 2ndnd Parameter Parameter Pick any 2 different numbersPick any 2 different numbers
Create Another MethodCreate Another Method
Create a Method for Snowman Create a Method for Snowman Blinking His EyesBlinking His Eyes• Is this a World / Global MethodIs this a World / Global Method• Is this a Class MethodIs this a Class Method
WhyWhy How is it Created?How is it Created?
HomeworkHomework
Chapter 4, Section 1 (4-1)Chapter 4, Section 1 (4-1)• #3 – Gallop and Jump#3 – Gallop and Jump• #4 – Helicopter Flight#4 – Helicopter Flight
Chapter 4, Section 2 (4-2)Chapter 4, Section 2 (4-2)• #6 – Frog Escape#6 – Frog Escape
Bring electronic copy of homework to classBring electronic copy of homework to classBring printed copy of homework to submitBring printed copy of homework to submit
Class MethodsClass Methods
Class Methods are methods Class Methods are methods that define a behavior for one that define a behavior for one single class object.single class object.
Select Horse ObjectSelect Horse Object MethodsMethods Create New MethodCreate New Method gallopgallop
Guidelines for Class MethodsGuidelines for Class Methods
Create lots of class methods as necessaryCreate lots of class methods as necessary Do not call world-level methods from Do not call world-level methods from
within a class-level methodwithin a class-level method Do not use instructions for other objects Do not use instructions for other objects
from within a class-level methodfrom within a class-level method You can use parameters to send necessary You can use parameters to send necessary
information instead of referring to other information instead of referring to other specific objects from within the method.specific objects from within the method.
InheritanceInheritance
Creating a class on a previously Creating a class on a previously defined classdefined class
Adding functionality to existing class Adding functionality to existing class by defining new methods or editing by defining new methods or editing existing onesexisting ones
Different members of design team Different members of design team can work on different methods for can work on different methods for the classthe class
Creating a New Class in ALICECreating a New Class in ALICE
Save object with new class methods Save object with new class methods as a New Classas a New Class• Code ReuseCode Reuse• Galloping HorseGalloping Horse
2 Step Process2 Step Process• Rename existing object in Object TreeRename existing object in Object Tree• Save As a New ClassSave As a New Class
Rename ObjectRename Object
Select the Object in the Select the Object in the Object TreeObject Tree
Right-ClickRight-Click RenameRename Select a New Meaningful Select a New Meaningful
Name for ObjectName for Object• GallopingHorseGallopingHorse
Save As New ClassSave As New Class
Select the Object in Select the Object in
the Object Treethe Object Tree Right-ClickRight-Click Save ObjectSave Object Navigate to folder Navigate to folder
where you want to save the objectwhere you want to save the object
Other objects: Alice\Alice\Required\GalleryOther objects: Alice\Alice\Required\Gallery
.a2c extension .a2c extension Alice 2.0 class Alice 2.0 class
New ClassNew Class
New Class (GallopingHorse) is now New Class (GallopingHorse) is now available in Object Galleryavailable in Object Gallery
All objects of GallopingHorse All objects of GallopingHorse • all methods of horse all methods of horse • class-method gallopclass-method gallop
HomeworkHomework
Exercise 4-3 Exercise 4-3 • #10#10• #11 #11 use a say instruction not a sound use a say instruction not a sound
file so all worlds don’t have to import file so all worlds don’t have to import sound filesound file
• Bring electronic copy of homework to classBring electronic copy of homework to class• Bring printed copy of homework to submitBring printed copy of homework to submit