Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class...

82
Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples

Transcript of Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class...

Page 1: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Introduction to Classes and Objects Initializing Objects

Making Use of Classesin Algorithms

Class Examples

Page 2: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Introduction to Classes and Objects

Page 3: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

The Scenario

• We want to provide a “black box” approach to reusable components…

Don’t know howit works, but it

works!

Input Output

Page 4: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

The Scenario

• We want to create reusable components for other programmers to use.

• They are focused on their work and don’t want to know the details of how our component works.

• They are also busy trying to do their work, so maybe they’ll take shortcuts and “hack.”

• We want to provide services to them but protect the implementation details of our components.

Page 5: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Classes

• A class is the fundamental unit of object-oriented programming.

• It allows us to implement ADTs, combining both data and operations into one logical bundle.

Page 6: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Objects

• Once we have a class defined, we can then declare any number of objects of that class.

For example:

• Once we have a queue class defined, we can then declare any number of queue objects.

• Each queue object will then have all the data and procedural abstractions needed for a queue.

Page 7: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Classes and Objects

• A class is like a blueprint:

• An object is an instantiated class (like a building):

Page 8: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Classes vs. Objects

• Class refers to the template by which we implement an ADT’s functionality. Thus, it is analogous to data type: it is only a definition or template.

• Object refers to a specific instance of the class. Thus, it is analogous to variable: it is an instance of a template.

Class : Object :: Type : Variable

Page 9: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Classes Allow for Reuse!

• Algorithms which make use of the queue class (by declaring instances of the queue, or objects) are clients who obtain services:

algorithm Movie_Theatre algorithm Restaurant algorithm Check-out_Line

• The authors of these algorithms can instantiate and use queues without writing them!

Page 10: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Components of a Class

• The individual data elements of a class are referred to as attributes of the class.

• The entire collection of data maintained within an object is referred to as the state of the object.

• The operations (procedures and functions) that are provided by the class (and thus allowed on the data) are called the methods of the class.

Page 11: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Controlling Visibility

• Recall we want to protect the implementation of the class.– Users shouldn’t need to know how it works– Users shouldn’t be able to violate the

behaviors (may only do what we allow them).

• Encapsulation allows us to hide the implementation details.

Page 12: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Public vs. Protected

Public

Protected

Page 13: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Visibility Within a Class

Public Section (Interface)

Protected Section(Implementation)

The rest of the algorithm

Page 14: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Anatomy of a Class

Classes have two sections:

• Public - the specification of the "visible” part of the class

• Protected - the specification of the "hidden” part of the class

Page 15: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Public Section

• Contains everything that the client algorithm needs to know to use the class

• Is the visible part of the class

• Defines the interface and contract with the user (algorithm)

• Contains the header lines of those methods that are available for clients of the class to call.

Page 16: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Protected Section

• Contains the details of the implementation of the class

• Cannot be seen by the client algorithm

• Hides attributes and method implementation

• Contains all method declarations and implementations.

Page 17: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Interactions

Protected Data & Implementation of Modules

Public Module Definitionsand Contract

Client Algorithm

Supplier Object

Page 18: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Encapsulation and Hiding

• A client algorithm can see only the public section of the class. A client has no idea what is within the protected section.

• A client algorithm can interact with an object only by calling the methods listed in the public section of the class.

• A client algorithm cannot directly access any of the data within an object. It may “get at” data only via calls to public methods.

Page 19: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

class data

publicmethod 1

publicmethod 2

publicmethod 3

a protectedmethod

Public vs. Protected Methods

• Clients may call any/all of the three public methods.

• Clients don’t know the hidden method exists; they cannot call it.

Some Class

Page 20: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Protected Methods

• A protected method is one which is defined in the protected section that does not have its header line in the public section.

• Clients don’t even know it exists.

• Such a method may only be called by other methods within the protected section.

Page 21: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Structure of a Class Definition

class <identifier>

public

<method definitions and contracts>

protected

<constants>

<type definitions>

<attribute declarations>

<method implementations>

endclass // <identifier>

Page 22: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

A Bus Class Example

MAX_PASSENGERS Current

Initialize

Add Passengers

RemovePassengers

Number of Passengers

Bus

Implementation

Page 23: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

class Buspublic procedure Initialize // contract here procedure AddPassengers (to_add iot in num) // contract here function NumPassengers returnsa num // contract here procedure RemovePassengers (to_remove iot in num) // contract hereprotected MAX_PASSENGERS is 50 current isoftype num

procedure Initialize current <- 0 endprocedure // Initialize

Page 24: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

procedure AddPassengers (to_add iot in num)

if (current + to_add <= MAX_PASSENGERS) then

current <- current + to_add

endif

endprocedure // AddPassengers

function NumPassengers returnsa num

NunPassengers returns current

endfunction // NumPassengers

procedure RemovePassengers (to_remove iot in num)

if (current >= to_remove) then

current <- current – to_remove

endif

endprocedure // RemovePassengers

endclass // Bus

Page 25: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Recipe for Writing a Class

• Write shell/template• Decide on what the user needs to know

about - PUBLIC• Specify the protected data• Diagram & think of sample algorithm• Write public section with contract

information• Write protected section with

implementation details

Page 26: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Attribute Visibility in the Protected Section

Variables declared at the global level of the protected section can be directly accessed throughout the protected section without passing them via parameter.

protectedcurrent isoftype num . . .

procedure AddPassengers (to_add iot in num) if (current + to_add <= MAX_PASSENGERS) then current <- current + to_add endif endprocedure // AddPassengers

function NumPassengers returnsa num NunPassengers returns current endfunction // NumPassengers . . .

Page 27: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Attribute Visibility in the Protected Section

• Allow direct access, bypassing parameters!

• May want clients of the class to be able to call methods

• Don't want clients to know the details of the protected section's data representation.

• If method parameter lists included data that's protected, then would be telling the client about that representation.

• So, allow direct access within protected

• Still have encapsulation (the class itself)

Page 28: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Summary

• Classes implement ADTs• An object is an instance of a class• Encapsulation allows for the protection of details

within the class– Public section provides interface– Protected section provides implementation

• Attributes declared at the global level of the protected section may be directly accessed within any method in the protected section.

Page 29: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Questions?

Page 30: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Initializing Objects

Page 31: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

The Scenario

• You’ve defined a Queue class and now want to create an instance of that class.

• There are some attributes which should be initialized when an object is created.

• We’d like to initialize our Head and Tail pointers to NIL.

• But the algorithm shouldn’t know about the pointers!

Page 32: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Need to Initialize

• Our Queue class will thus need a method that “sets things up” when an object is created.

• Different languages handle this differently.

• We’ll use a normal method that must be invoked like any other…

Page 33: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

“A Method By Any Other Name…”

• An initialization method is just like any other method.

• For clarity and convenience, we always use the identifier Initialize for such methods.

Page 34: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Initialize Common but Not Required

• Most classes have attributes that require some initial values.

• We expect most classes to have initialization methods.

• But it’s not required.

Page 35: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Initialize In the Class Definition

class Initialize_Example

public

. . .

procedure Initialize (<parameters>)

// contract information

. . .

protected

. . .

procedure Initialize (<parameters>)

<implementation>

endprocedure // Initialize

endclass

Page 36: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Invoking the Initialize Method

• In the algorithm, we invoke (or call) the Initialize method just as any other method.

• It is assumed that Initialize is only called once, but there is no rule to enforce this.

• The algorithm must explicitly call the initialize method for each object that has attributes that need to be initialize.

Page 37: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

An Example

In the algorithm:

MyQueue isoftype Queue

MyQueue.Initialize

In the class definition:

procedure Initialize

head <- NIL

tail <- NIL

endprocedure // Initialize

Page 38: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Another Example

In the algorithm:MyAirplane isoftype Plane

MyAirplane.Initialize(“Delta 955”)

In the class definition:

procedure Initialize(flight_name iot in string) altitude <- 0

current_flight <- flight_name

endprocedure // Initialize

Page 39: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Summary

• Very often, objects have attributes that need some initial values.

• Thus these classes need some method to set these attributes.

• Different languages handle this differently:– Some have “special” methods that are

automatically executed.– We’ll treat the Initialize method like any

other method and call it from the algorithm for each object created.

Page 40: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Questions

Page 41: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Making Use of Classesin Algorithms

Page 42: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

The Scenario

• You need to create a simulation of an airport.

• A coworker has developed an airplane class you’d like to use in your algorithm.

• How do we “get at” the airplane class?

• Notice we don’t have access to modify the class, just make use of it.

Page 43: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Importing the Class

• Algorithms and class definitions often reside in separate computer files.– Recall that a benefit of Object-Oriented

Programming is re-use via class libraries

• We need the ability to import a class into our algorithm or another class

• The “uses” clause allows this import.

Page 44: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

The “Uses” Clause

algorithm <Algorithm Name> uses <Class Name>, <Class Name>, . . . . . .endalgorithm // <Algorithm Name>

class <Class Name> uses <Other Class Name> . . .endclass // <Class Name>

Page 45: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

An Example

Having previously defined an Airplane class, the algorithm can make use of the class and create objects of the class as follows:

algorithm Airport

uses Airplane

Cessna, Prop isoftype Airplane

. . .

Page 46: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

A More Complex Example

algorithm BigAirport

uses Airplane, Helicopter, FlightController

Plane1, Plane2 isoftype Airplane

ControlDeck isoftype FlightController

. . .

endalgorithm

Page 47: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Classes and Objects

• A class is like a blueprint:It is analogous to data type

• An object is an instantiated class (like a building):It is analogous to variable

Class : Object :: Type : Variable

Page 48: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Creating Objects

• Once the algorithm has imported the class definition, then it can create objects of the class.

• Objects are created just as any variable:

<Object Identifier> isoftype <Class Name>

For example:

MyPlane, YourPlane isoftype Airplane

ControlDeck isoftype FlightController

Page 49: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Accessing Methods of Objects

• After instantiating a class and creating an object in the algorithm, we want to make use of its methods.

• There are many methods in the class, so how do we access the one we want?

• Just as fields of a record, we access the methods in an object using the dot (.) operator.

Page 50: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Invoking Methods of an Object

algorithm Example

uses MyClass

MyObject isoftype MyClass

MyObject.Initialize

MyObject.<Any Method in Public Section>

. . .

endalgorithm

Page 51: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

An Airplane Class

Airplane

Initialize

TakeOff

ChangeAltitude• InTheAir

• AltitudeIsFlying

ServeSnackLand

Fly

Page 52: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

An Example of Accessing Methods

algorithm Flying uses Airplane

MyAirplane isoftype Airplane MyAirplane.Initialize

if (NOT MyAirplane.IsFlying) then MyAirplane.TakeOff MyAirplane.ChangeAltitude(30000) MyAirplane.Fly(“Cincinnati”) MyAirplane.Land else print(“Sorry, that plane is already flying!”)endalgorithm

Function

Procedures

Page 53: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Only Access Public Methods

• The algorithm cannot access protected attributes or protected methods.

• The algorithm only has access to methods as declared in the public section.

Examples of illegal use:

MyAirplane.Altitude <- 30000

print(MyAirplane.InTheAir)

MyAirplane.ServeSnack // method call

Page 54: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Summary

• Import class definitions using the uses clause.

• Declare objects as any other variable.

• Access public methods of objects using the dot (.) operator.

Page 55: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Questions?

Page 56: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Class Examples

Airplane, Queue, Pile

Page 57: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Once Written, It’s Easy!

Once we’ve written the class…• We test it and validate that it works• We can then make use of it in any

algorithm• Notice in the following algorithm

examples how little work is done– All manipulation is hidden from

the algorithm– All the “details” are abstracted

into the object

Page 58: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Airplane Example

An Airplane knows how to:• Take off• Land• Fly to a destination (and serve a snack)• Change its altitude

It also has the following attributes• Current altitude• Whether it’s flying or not

Page 59: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Airplane Symbolic Diagram

Airplane

Initialize

TakeOff

ChangeAltitude• InTheAir

• AltitudeIsFlying

ServeSnackLand

Fly

Page 60: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

class Airplanepublic

procedure TakeOff// comments here

procedure Land // comments here

procedure ChangeAltitude (NewHeight iot in Num)// comments herefunction IsFying returnsa boolean// comments hereprocedure Initialize// comments hereprocedure Fly (destination iot in String)// comments here

protected // create the persistent data

InTheAir isoftype BooleanAltitude isoftype Num

Page 61: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

// still in the protected section procedure Initialize

InTheAir <- FALSEAltitude <- 0

endprocedure // Initialize

procedure TakeOff if InTheAir then

print("I'm in the air!") else

InTheAir <- TRUE ChangeAltitude(3000)

endifendprocedure // TakeOff

Page 62: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

// still in the protected sectionprocedure ChangeAltitude (NewHeight iot in Num) Altitude <- NewHeightendprocedure // ChangeAltitude

procedure Fly (destination iot in String) print(“I’m flying to”, destination) ServeSnack endprocedure // Fly

procedure ServeSnack// comments here

MAKE PASSENGERS HAPPYendprocedure // ServeSnack

Page 63: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

// still in the protected sectionfunction IsFlying returnsa boolean

IsFlying returns InTheAirendfunction // IsFlying

procedure Land if InTheAir then

InTheAir <- FALSE ChangeAltitude(0)

else print("I'm not in the air!")

endif endprocedure // Land

endclass // Airplane

Page 64: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Using the Airplane Class

algorithm Airportuses Airplane

Cessna1090 isoftype Airplane

Cessna1090.InitializeCessna1090.TakeoffCessna1090.ChangeAltitude(30000)

Cessna1090.Fly(“Baltimore”) Cessna1090.Landendalgorithm

Page 65: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

The Queue

A collection with restricted set of operations to change its state: only modified by adding to one end and deleting from the other.

Enqueue

Dequeue

Page 66: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

NumberQueue Symbolic Diagram

NumberQueue

Initialize

Enqueue

Dequeue

• head

• tail

IsEmpty

IsFull

Page 67: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

class NumberQueuepublic procedure Enqueue(value iot in Num) // contract information here procedure Dequeue(value iot out Num) // contract - queue not empty procedure Initialize // contract information here function IsEmpty returnsa Boolean // contract information here function IsFull returnsa Boolean // contract information here

protectedList_type definesa record data isoftype Num next isoftype Ptr toa List_typeendrecord

// create the persistent data head, tail isoftype Ptr toa List_type

Page 68: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

// still in the protected section

procedure Enqueue(value iot in Num)

temp isoftype Ptr toa List_type

temp <- new(List_type)

temp^.data <- value

temp^.next <- NIL

if(IsEmpty) then

head <- temp

else

tail^.next <- temp

endif

tail <- temp

endprocedure // Enqueue

Page 69: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

// still in the protected section procedure Dequeue (value iot out Num) if(IsEmpty) then

// violates contract! Error! else

value <- head^.data head <- head^.next if(IsEmpty) then

tail <- NIL endif

endif endprocedure // Dequeue

Page 70: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

// still in the protected section function IsEmpty returnsa Boolean IsEmpty returns (head = NIL)endfunction // IsEmpty

function IsFull returnsa Boolean IsFull returns FALSE // dynamicendfunction // IsFull

procedure Initialize // initialize the persistent data head <- NIL tail <- NILendprocedure // Initialize

endclass // NumberQueue

Page 71: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

algorithm Store uses NumberQueue temp isoftype num

checkout isoftype NumberQueuecheckout.Initialize

. . .loop

some people enter and leave store randomly exitif ((no people in store) AND (closing_time)) if (someone walks up for service) then checkout.Enqueue(person’s number) endif if (NOT checkout.IsEmpty) then checkout.Dequeue(temp) print(“Now servicing person”, temp) endif endloopendalgorithm // Store

Page 72: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Example: Simulating the Lotto

• We want to define a class that will allow us to simulate the lottery.

• We want to place elements into random locations in the collection.

• When we get an item from the collection, we want a random element.

Page 73: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

A “Pile” Class

• A data structure in which– Items are inserted somewhere

randomly in the middle of the structure

– Items are removed from a random location in the structure

Page 74: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Pile Symbolic Diagram

NumPile

Initialize

StickOn

DigOut

• Head

• num_of_things

IsEmpty Random

Page 75: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

class NumPilepublicprocedure StickOn (the_thing iot in Num)// purpose: put an item on the pile.

// pre: none // post: the pile has the item added to it

procedure DigOut (the_thing iot out Num)// purpose: get an item off of the pile.// pre: the pile is not empty.// post: the pile has a random element// removed.

function IsEmpty returnsa boolean // comments here - contractprocedure Initialize

// comments here - contract

Page 76: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

protectedPileNode definesa Record

thing isoftype Numnext isoftype ptr to PileNode

endrecord // PileNode

head isoftype ptr toa PileNodenum_of_things isoftype Num

procedure Initialize

num_of_things <- 0

head <- NIL

endprocedure // Initialize

function IsEmpty returnsa boolean IsEmpty returns (head = NIL) endfunction // IsEmpty

Page 77: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

// still in the protected sectionfunction Random returnsa Num // returns a random number <=

// num_of_thingsendfunction // Random

procedure StickOn (thing isoftype in Num) place_to_insert isoftype Num place_to_insert <- Random new_node isoftype ptr toa PileNode new_node <- new(PileNode)

// loop through pile until place-to- // insert is reached, then insert node

num_of_things <- num_of_things + 1endprocedure // StickOn

Page 78: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

// still in the protected section

procedure DigOut (thing isoftype out Num)

thing_to_snag isoftype Num

place_to_get isoftype Num

place_to_get <- Random

// code for looping through pile to

// find right thing-to-snag, then

// remove it

num_of_things <- num_of_things - 1

thing <- thing_to_snag

endprocedure // Dig-Out

endclass // NumPile

Page 79: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Using the Pile Classalgorithm Lotto uses NumPile

lotto_pile isoftype NumPilelotto_pile.Initializeticket isoftype Numloop

exitif (All Entries Purchased)Get_Entry(ticket) // get input from userlotto_pile.StickOn(ticket)

endloop

// Now, find one winner lotto_pile.DigOut(ticket)print ("The winning number is", ticket)

endalgorithm // Lotto

Page 80: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Summary

• Writing classes involves considerable work in – Design, Implementation, & Testing

• But once done, algorithms may make use of the classes– Instantiating objects and

manipulating them– Hiding the details and implementation–Much of the work is done inside the

object

Page 81: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.

Questions?

Page 82: Introduction to Classes and Objects Initializing Objects Making Use of Classes in Algorithms Class Examples.