Propedéutico de
Programación
Coordinación de Ciencias
Computacionales
9/13
Material preparado por:
Dra. Pilar Gómez Gil
Chapter 4
ADT Sorted List
Remember the difference between an unsorted list and a
sorted list?
Remember the definition of a key?
If the list is a sorted list
of names, what would be the key?
of bank balances, what would be the key?
of grades, what would be the key?
ADT Unsorted List Operations
•
Transformers
–
MakeEmpty
–
InsertItem
–
DeleteItem
•
Observers
–
IsFull
–
GetLength
–
RetrieveItem
••
Iterators
–
ResetList
–
GetNextItem
change state
observe state
process all
ADT Sorted List
•
Which member function specifications and
implementations must change to ensure
that any instance of the Sorted List ADT
remains sorted at all times?
–
InsertItem
–
DeleteItem
•
La inserción tendrá que ser de tal manera que
Linked Implementation
What about passing spot where item
would be if there?
Linked Implementation
•
Is Inserting as easy? Let's see
Set location to listData
Set moreToSearch to (location != NULL)
while moreToSearch
switch (item.ComparedTo(location->info))
case GREATER
:
Set location to location->next
Set moreToSearch to (location != NULL)
case LESS : Set moreToSearch to false
See
the
problem
Linked Implementation
Inserting ‘S’ into a Sorted List
‘C’ ‘L’ ‘X’
Private data:
length
3
listData
currentPos ?
predLoc location
moreToSearch
Finding proper position for ‘S’
‘C’ ‘L’ ‘X’
Private data:
length
3
listData
currentPos ?
predLoc location
NULL
moreToSearch true
Finding proper position for
‘
S
’
‘C’ ‘L’ ‘X’
Private data:
length
3
listData
currentPos ?
predLoc location
moreToSearch true
Finding Proper Position for
‘
S
’
‘C’ ‘L’ ‘X’
Private data:
length
3
listData
currentPos ?
predLoc location
moreToSearch
false
Inserting
‘
S
’
into Proper Position
‘C’ ‘L’ ‘X’
Private data:
length
4
listData
currentPos
predLoc location
moreToSearch false
‘S’
•
Ver
sorted.h
Linked Implementation
•
Does DeleteItem have to be changed?
Object-Oriented Design Methodology
•
Object-oriented design decomposes a problem into classes
•
Four stages to the decomposition process
–
Brainstorming
–
Filtering
–
Scenarios
Object-Oriented Design Methodology
•
Brainstorming
•
A group problem-solving technique that involves the
spontaneous contribution of ideas from all members of the
group
–
All ideas are potential good ideas
–
Think fast and furiously first, and ponder later
–
A little humor can be a powerful force
Object-Oriented Design Methodology
•
Filtering
•
Determine which are the core classes in the problem
solution
–
There may be two classes in the list that have many
common attributes and behaviors
–
There may be classes that really don’t belong in the
Object-Oriented Design Methodology
•
Scenarios
•
Sequences of steps that describe an interaction between a
client and an application or program
–
Simulate class interactions
–
Ask “What if?” questions
–
Assign responsibilities to each class
–
There are two types of responsibilities
•
What a class must know about itself (knowledge)
•
What a class must be able to do (behavior)
•
Use case
Object-Oriented Design Methodology
Role
playing
Object-Oriented Design Methodology
•
Responsibility Algorithms
•
The algorithms must be written for the
responsibilities
–
Knowledge responsibilities usually just return
the contents of one of an object’s variables
–
Action responsibilities are a little more
complicated, often involving calculations
Responsibilities algorithms are often
decomposed using top-down design
Relationships Between Classes
•
Containment
–
“part-of”
–
An address class may be part of the definition of a student
class
•
Inheritance
–
Classes can inherit data and behavior from other classes
–
“is-a”
Computer Example
•
Let’s work through this problem-solving process by
creating an address list
•
Brainstorming and filtering
–
Circling the nouns and underlining the verbs is a good way
Computer Example
•
First pass at a list of classes
list
name
telephone number
email address
list
order
names
list
scraps
paper
cards
Filtered List
CRC Cards
CRC Cards
CRC Cards
Responsibility Algorithms
Person Class
Initialize
name.initialize()
Write "Enter phone number; press return."
Get telephone number
Write "Enter email address; press return."
Get email address
name.print()
Write "Telephone number: " + telephoneNumber
Write "Email address: " + emailAddress
Tells name to initialize itself
Responsibility Algorithms
Name Class
Initialize
"Enter the first name; press return."
Read firstName
"Enter the last name; press return."
Read lastName
Print "First name: " + firstName
Print "Last name: " + lastName