Classes / Objects / Methods - PowerPoint PPT Presentation

About This Presentation
Title:

Classes / Objects / Methods

Description:

Classes / Objects / Methods Chapter 4 Object Oriented Programming Divides a program into Modules/Classes Uses Classes and Objects to simulate real world environments ... – PowerPoint PPT presentation

Number of Views:135
Avg rating:3.0/5.0
Slides: 33
Provided by: Debr82
Category:

less

Transcript and Presenter's Notes

Title: Classes / Objects / Methods


1
Classes / Objects / Methods
  • Chapter 4

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

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

4
Class
  • Properties
  • Characteristics
  • Methods
  • Behaviors

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

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

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

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

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

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

11
(No Transcript)
12
Add Method Instructions
13
Calling a Method
  • New Method must be Called
  • Program Instructions must indicate to Execute the
    new Method
  • In Alice
  • Drag Method into World.MyFirstMethod
  • In Other Languages
  • Call the Method using Method Name

14
(No Transcript)
15
Parameters
  • Allows communication with Methods
  • Communicate Values (Numbers, Colors)
  • Communicate Object Names

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

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

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

19
Use Parameter in Method
20
Calling Method with Parameter
  • When using Method you must include Parameter
  • Use dance method after flipHat
  • Example 1 Requires an object to dance

21
Multiple Parameters
  • Add Second Parameter How many times to Spin
  • Name numberToSpin
  • Type Number
  • Use in the Turn Instruction

22
Modify Method Call
  • Change 2nd Parameter
  • Pick any 2 different numbers

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

24
Homework
  • Chapter 4, Section 1 (4-1)
  • 3 Gallop and Jump
  • 4 Helicopter Flight
  • Chapter 4, Section 2 (4-2)
  • 6 Frog Escape

Bring electronic copy of homework to class Bring
printed copy of homework to submit
25
Class Methods
  • Class Methods are methods that define a behavior
    for one single class object.
  • Select Horse Object
  • Methods
  • Create New Method
  • gallop

26
Guidelines for Class Methods
  • 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.

27
Inheritance
  • 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

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

29
Rename Object
  • Select the Object in the Object Tree
  • Right-Click
  • Rename
  • Select a New Meaningful Name for Object
  • GallopingHorse

30
Save As New Class
  • Select the Object in
  • the Object Tree
  • Right-Click
  • Save Object
  • Navigate to folder
  • where you want to save the object
  • Other objects Alice\Alice\Required\Gallery
  • .a2c extension ? Alice 2.0 class

31
New Class
  • New Class (GallopingHorse) is now available in
    Object Gallery
  • All objects of GallopingHorse
  • all methods of horse
  • class-method gallop

32
Homework
  • Exercise 4-3
  • 10
  • 11 ? use a say instruction not a sound file so
    all worlds dont have to import sound file
  • Bring electronic copy of homework to class
  • Bring printed copy of homework to submit
Write a Comment
User Comments (0)
About PowerShow.com