+ All Categories
Home > Documents > Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Date post: 04-Jan-2016
Category:
Upload: emily-turner
View: 219 times
Download: 3 times
Share this document with a friend
42
Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone
Transcript
Page 1: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Class Examples(Simple, Airplane, Queue, Pile)

Copy vs. Clone

Page 2: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Class Examples

Simple, Airplane, Queue, Pile

Page 3: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

World’s Simplest Class!class Simple

public

Procedure setValue(dataIn isoftype in Number)

// Precon: Initialization

// Purpose: Sets value to dataIn

// Postcon: Value is changed

Function getValue returnsa Num()

// Precon: Initialization

// Purpose: Returns value to user

// Postcon: No changes to object

Procedure Initialize()

// Precon: Object exists

// Purpose: Initialization

// Postcon: Value is defined to be zero

LB

Page 4: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

(continued) protected

value isoftype Num

Procedure setValue(dataIn isoftype in Number)

value <- dataIn

endprocedure // setValue

Function getValue returnsa Num()

getValue returns value

endfunction // getValue

Procedure Initialize()

value <- 0

endprocedure // Initialize

endclass // Simple

LB

Page 5: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 6: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 7: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Airplane Symbolic Diagram

Airplane

Initialize

TakeOff

ChangeAltitude• InTheAir

• AltitudeIsFlying

ServeSnackLand

Fly

Page 8: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 9: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

// 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 10: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

// 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 11: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

// 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 12: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Using the Airplane Class

algorithm Airportuses Airplane

Cessna1090 isoftype Airplane

Cessna1090.InitializeCessna1090.TakeoffCessna1090.ChangeAltitude(30000)

Cessna1090.Fly(“Baltimore”) Cessna1090.Landendalgorithm

Page 13: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 14: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

NumberQueue Symbolic Diagram

NumberQueue

Initialize

Enqueue

Dequeue

• head

• tail

IsEmpty

IsFull

Page 15: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 16: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

// 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 17: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

// 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 18: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

// 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 19: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 20: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 21: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 22: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Pile Symbolic Diagram

NumPile

Initialize

StickOn

DigOut

• Head

• num_of_things

IsEmpty Random

Page 23: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 24: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 25: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

// 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 26: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

// 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 27: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 28: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

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 29: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Questions?

Page 30: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Copy vs. Clone

Page 31: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

The Scenario

• Imagine we have an object of type Queue

• We’d like to duplicate the contents of the object

• The assignment operator (<-) duplicates variables

• How do we duplicate objects?

Page 32: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Representing Objects

• Objects may have static and dynamic components.

q_head q_tail

11 13 \\9

MyNumQueue

Page 33: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Shallow vs. Deep Duplication

Copy performs a shallow duplication – duplicating only the static data.

Cloning performs a deep duplication – duplicating the static and dynamic data (i.e. following pointers into the heap and duplicating the heap data)

Page 34: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Create Two Objects

MyNumQueue isoftype Queue(Num)

YourNumQueue isoftype Queue(Num)

// do some work to fill in MyNumQueue

q_head q_tail

MyNumQueue

q_head q_tail

\\

YourNumQueue

\\11 13 \\9

Page 35: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Copying an Object

YourNumQueue <- copy(MyNumQueue)

q_head q_tail

MyNumQueue

q_head q_tail

YourNumQueue

11 13 \\9

Page 36: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Risk of Copying an Object

YourNumQueue.Enqueue(42)

q_head q_tail

\\

MyNumQueue

q_head q_tail

YourNumQueue

42 11 13 9

Page 37: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Risk of Copying an Object

MyNumQueue.Enqueue(31)

q_head q_tail

\\

MyNumQueue

q_head q_tail

YourNumQueue

42

\\31

11 13 9

Page 38: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Create Two Objects

MyNumQueue isoftype Queue(Num)

YourNumQueue isoftype Queue(Num)

// do some work to fill in MyNumQueue

q_head q_tail

MyNumQueue

q_head q_tail

\\

YourNumQueue

\\11 13 \\9

Page 39: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Cloning an Object

YourNumQueue <- clone(MyNumQueue)

- or –

YourNumQueue <- MyNumQueue

q_head q_tail

11 13 \\9

MyNumQueue

q_head q_tail

YourNumQueue

11 13 \\9

Page 40: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Summary

• Duplication of objects:– Shallow: only duplicate static memory– Deep: duplicate static and dynamic memory

• Copy is shallow duplication• Clone is deep duplication

• Assignment operation on objects is clone

Page 41: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Questions

Page 42: Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.

Recommended