Class Examples Simple, Airplane, Queue, Pile Copy vs' Clone - PowerPoint PPT Presentation

About This Presentation
Title:

Class Examples Simple, Airplane, Queue, Pile Copy vs' Clone

Description:

(Simple, Airplane, Queue, Pile) Copy vs. Clone. Lecture 19. Class Examples ... class Airplane. public. procedure TakeOff // comments here. procedure Land ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 43
Provided by: ccGa
Category:

less

Transcript and Presenter's Notes

Title: Class Examples Simple, Airplane, Queue, Pile Copy vs' Clone


1
Class Examples(Simple, Airplane, Queue,
Pile)Copy vs. Clone
Lecture 19
2
Class Examples
  • Simple, Airplane, Queue, Pile

3
Worlds Simplest Class!
LB
  • 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

4
(continued)
LB
  • protected
  • value isoftype Num
  • Procedure setValue(dataIn isoftype in
    Number)
  • value lt- dataIn
  • endprocedure // setValue
  • Function getValue returnsa Num()
  • getValue returns value
  • endfunction // getValue
  • Procedure Initialize()
  • value lt- 0
  • endprocedure // Initialize
  • endclass // Simple

5
Once Written, Its Easy!
  • Once weve 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

6
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 its flying or not

7
Airplane Symbolic Diagram
Airplane
Initialize
TakeOff
  • InTheAir
  • Altitude

ChangeAltitude
IsFlying
ServeSnack
Land
Fly
8
  • class Airplane
  • public
  • procedure TakeOff
  • // comments here
  • procedure Land
  • // comments here
  • procedure ChangeAltitude (NewHeight iot in Num)
  • // comments here
  • function IsFying returnsa boolean
  • // comments here
  • procedure Initialize
  • // comments here
  • procedure Fly (destination iot in String)
  • // comments here
  • protected
  • // create the persistent data
  • InTheAir isoftype Boolean
  • Altitude isoftype Num

9
  • // still in the protected section
  • procedure Initialize
  • InTheAir lt- FALSE
  • Altitude lt- 0
  • endprocedure // Initialize
  • procedure TakeOff
  • if InTheAir then
  • print("I'm in the air!")
  • else
  • InTheAir lt- TRUE
  • ChangeAltitude(3000)
  • endif
  • endprocedure // TakeOff

10
  • // still in the protected section
  • procedure ChangeAltitude
    (NewHeight iot in Num)
  • Altitude lt- NewHeight
  • endprocedure // ChangeAltitude
  • procedure Fly (destination iot in String)
  • print(Im flying to, destination)
  • ServeSnack
  • endprocedure // Fly
  • procedure ServeSnack
  • // comments here
  • MAKE PASSENGERS HAPPY
  • endprocedure // ServeSnack

11
  • // still in the protected section
  • function IsFlying returnsa boolean
  • IsFlying returns InTheAir
  • endfunction // IsFlying
  • procedure Land
  • if InTheAir then
  • InTheAir lt- FALSE
  • ChangeAltitude(0)
  • else
  • print("I'm not in the air!")
  • endif
  • endprocedure // Land
  • endclass // Airplane

12
Using the Airplane Class
  • algorithm Airport
  • uses Airplane
  • Cessna1090 isoftype Airplane
  • Cessna1090.Initialize
  • Cessna1090.Takeoff
  • Cessna1090.ChangeAltitude(30000)
  • Cessna1090.Fly(Baltimore)
  • Cessna1090.Land
  • endalgorithm

13
The Queue
Dequeue
  • A collection with restricted set of operations to
    change its state only modified by adding to one
    end and deleting from the other.

Enqueue
14
NumberQueue Symbolic Diagram
NumberQueue
Initialize
  • head
  • tail

Enqueue

Dequeue
IsEmpty
IsFull
15
  • class NumberQueue
  • public
  • 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
  • protected
  • List_type definesa record
  • data isoftype Num
  • next isoftype Ptr toa List_type
  • endrecord
  • // create the persistent data

16
  • // still in the protected section
  • procedure Enqueue(value iot in Num)
  • temp isoftype Ptr toa List_type
  • temp lt- new(List_type)
  • temp.data lt- value
  • temp.next lt- NIL
  • if(IsEmpty) then
  • head lt- temp
  • else
  • tail.next lt- temp
  • endif
  • tail lt- temp
  • endprocedure // Enqueue

17
  • // still in the protected section
  • procedure Dequeue (value iot out Num)
  • if(IsEmpty) then
  • // violates contract! Error!
  • else
  • value lt- head.data
  • head lt- head.next
  • if(IsEmpty) then
  • tail lt- NIL
  • endif
  • endif
  • endprocedure // Dequeue

18
  • // still in the protected section function
    IsEmpty returnsa Boolean
  • IsEmpty returns (head NIL)
  • endfunction // IsEmpty
  • function IsFull returnsa Boolean
  • IsFull returns FALSE // dynamic
  • endfunction // IsFull
  • procedure Initialize
  • // initialize the persistent data
  • head lt- NIL
  • tail lt- NIL
  • endprocedure // Initialize
  • endclass // NumberQueue

19
  • algorithm Store
  • uses NumberQueue
  • temp isoftype num
  • checkout isoftype NumberQueue
  • checkout.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(persons number)
  • endif
  • if (NOT checkout.IsEmpty) then
  • checkout.Dequeue(temp)
  • print(Now servicing person, temp)
  • endif
  • endloop
  • endalgorithm // Store

20
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.

21
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

22
Pile Symbolic Diagram
NumPile
Initialize
  • Head
  • num_of_things

StickOn
DigOut
Random
IsEmpty
23
  • class NumPile
  • public
  • procedure 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 - contract
  • procedure Initialize
  • // comments here - contract

24
  • protected
  • PileNode definesa Record
  • thing isoftype Num
  • next isoftype ptr to PileNode
  • endrecord // PileNode
  • head isoftype ptr toa PileNode
  • num_of_things isoftype Num
  • procedure Initialize
  • num_of_things lt- 0
  • head lt- NIL
  • endprocedure // Initialize
  • function IsEmpty returnsa boolean
  • IsEmpty returns (head NIL)
  • endfunction // IsEmpty

25
  • // still in the protected section
  • function Random returnsa Num
  • // returns a random number lt
  • // num_of_things
  • endfunction // Random
  • procedure StickOn (thing isoftype in Num)
  • place_to_insert isoftype Num
  • place_to_insert lt- Random
  • new_node isoftype ptr toa PileNode
  • new_node lt- new(PileNode)
  • // loop through pile until place-to-
  • // insert is reached, then insert node
  • num_of_things lt- num_of_things 1
  • endprocedure // StickOn

26
  • // still in the protected section
  • procedure DigOut (thing isoftype out Num)
  • thing_to_snag isoftype Num
  • place_to_get isoftype Num
  • place_to_get lt- Random
  • // code for looping through pile to
  • // find right thing-to-snag, then
  • // remove it
  • num_of_things lt- num_of_things - 1
  • thing lt- thing_to_snag
  • endprocedure // Dig-Out
  • endclass // NumPile

27
Using the Pile Class
  • algorithm Lotto
  • uses NumPile
  • lotto_pile isoftype NumPile
  • lotto_pile.Initialize
  • ticket isoftype Num
  • loop
  • exitif (All Entries Purchased)
  • Get_Entry(ticket) // get input from user
  • lotto_pile.StickOn(ticket)
  • endloop
  • // Now, find one winner
  • lotto_pile.DigOut(ticket)
  • print ("The winning number is", ticket)
  • endalgorithm // Lotto

28
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

29
Questions?
30
Copy vs. Clone
31
The Scenario
  • Imagine we have an object of type Queue
  • Wed like to duplicate the contents of the object
  • The assignment operator (lt-) duplicates variables
  • How do we duplicate objects?

32
Representing Objects
  • Objects may have static and dynamic components.

MyNumQueue
\\
11
13
9
q_head
q_tail
33
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)

34
Create Two Objects
  • MyNumQueue isoftype Queue(Num)
  • YourNumQueue isoftype Queue(Num)
  • // do some work to fill in MyNumQueue

MyNumQueue
YourNumQueue
\\
\\
q_head
q_tail
q_head
q_tail
35
Copying an Object
  • YourNumQueue lt- copy(MyNumQueue)

YourNumQueue
q_head
q_tail
q_head
q_tail
MyNumQueue
36
Risk of Copying an Object
  • YourNumQueue.Enqueue(42)

YourNumQueue
q_head
q_tail
\\
42
q_head
q_tail
MyNumQueue
37
Risk of Copying an Object
  • MyNumQueue.Enqueue(31)

YourNumQueue
q_head
q_tail
\\
42
11
13
9
\\
31
q_head
q_tail
MyNumQueue
38
Create Two Objects
  • MyNumQueue isoftype Queue(Num)
  • YourNumQueue isoftype Queue(Num)
  • // do some work to fill in MyNumQueue

MyNumQueue
YourNumQueue
\\
\\
q_head
q_tail
q_head
q_tail
39
Cloning an Object
  • YourNumQueue lt- clone(MyNumQueue)
  • - or
  • YourNumQueue lt- MyNumQueue

MyNumQueue
YourNumQueue
q_head
q_tail
q_head
q_tail
40
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

41
Questions
42
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com