Cs 1311 Lecture 22 WDL
Cs 1311 Lecture 22 WDL
Announcements
Animal
Animal
Dog
MyMutt isoftype Mutt
MyAnimal isoftype Animal
Mutt MyDog isoftype Dog
. . .
MyDog <- MyMutt
MyAnimal <- MyMutt
Polymorphism Explained
Bank
Account
Savings Checking
Account Account
CD Account
A Collection of Bank Accounts
account_queue.Enqueue(john_account)
account_queue.Enqueue(paul_account)
account_queue.Enqueue(paul_other_account)
account_queue.Enqueue(george_account)
account_queue.Enqueue(ringo_account)
Polymorphic Banking
account_queue is polymorphic:
• Structured programming
• Object-Oriented programming
What’s left?
• Everything an object…
• Let’s make a class coordinate activities
Structured Programming
is-a is-a
is-a
Rabbit
Dog Cat
The Vector Class
Vector
Initialize
InsertElementAt
RemoveElementAt
• head
ElementAt …
Size
Contains
IndexOf
IsEmpty
algorithm VetClinic
uses Vector, Pet, Cat, Dog, Rabbit
loop
PrintMenu
GetChoice(choice)
exitif (choice = “QUIT”)
ProcessChoice(choice, Pens)
endloop
endalgorithm // VetClinic
procedure PrintMenu
print(“Please enter a choice:”)
print(“ADD a pet”)
print(“REMOVE a pet”)
print(“FEED pets”)
print(“LIST pets”) LB
...
print(“QUIT”)
endprocedure // PrintMenu
endprocedure // RemovePet
procedure FeedPets(Pens iot in/out Vector(Pet))
count isoftype num
count <- 1
loop
exitif(count > Pens.SizeOf)
// get the next pet in the collection and
// polymorphically call the Eat method on
// that pet (whatever its class)
Pens.ElementAt(count).Eat
count <- count + 1
endloop
public
procedure Initialize
// contract here
protected
Pens isoftype Vector(Pet)
procedure Initialize
Pens.Initialize
DoWork
endprocedure // Initialize
// Still in protected section
procedure DoWork
// contract here – protected method
choice isoftype string
loop
PrintMenu
GetChoice(choice)
exitif (choice = “QUIT”)
ProcessChoice(choice)
endloop
procedure PrintMenu
// contract here – protected method
print(“Please enter a choice:”)
print(“ADD a pet”)
print(“REMOVE a pet”)
print(“FEED pets”)
. . .
print(“QUIT”)
endprocedure // PrintMenu
procedure RemovePet
endprocedure // RemovePet
// Still in protected section
procedure FeedPets
// contract here – protected method
count isoftype num
count <- 1
loop
exitif(count > Pens.Size)
// get the next pet in the collection and
// polymorphically call the Eat method on
// that pet (whatever its class)
Pens.ElementAt(count).Eat
count <- count + 1
endloop
endclass // VetClinic
// --------------------------------------
algorithm VetExample
store isoftype VetClinic
store.Initialize
endalgorithm // VetExample
What Did We Do?
• Everything is an object
SimpleCalculator
0
Add
0
Clear
0
LB
SimpleCalculator
5
Add
0
Clear
0
LB
SimpleCalculator
5
Add
12
Clear
0
LB
SimpleCalculator
5
Add
12
Clear
0
LB
SimpleCalculator
5
Add
12
Clear
17
LB
SimpleCalculator
5
Add
12
Clear
17
LB
SimpleCalculator
0
Add
0
Clear
0
LB
Classes
CalcWindow TextBox
Clear
•ClearButton
•AddButton GetContents •Contents
•TopBox
•BottomBox SetContents Interact
•ResultBox
Initialize Button
SetName
ShowAt •Name
GetName
Listen
LB
Typical Operations
endprocedure
Typical Operations
Typical Operations
Starting it all up
SimpleCalculator
0
Add
0
Clear
0
A Vehicle Dealership Example
Queue of Vehicles
Dealership
Car Truck
User/Customer
Other Pure OO Examples
• Interactive programs
• Event-driven programming
Pseudocode to Java
class Simple
public
Procedure Initialize()
// PPP
Pseudocode to Java
protected
value isoftype Num
Procedure Initialize()
value <- 0
endprocedure
procedure setValue(newVal isoftype in Num)
value <- newVal
endprocedure
function getValue returnsa Num()
getValue returns value
endfunction
endclass
LB
class Simple {
private int value;
Java version
// ppp
public void Simple() {
value = 0;
} // Constructor
// ppp
public setValue(int newVal) {
value = newVal;
} // setValue
// ppp
public int getValue() {
return value;
} // getValue
} // Simple
Questions?