Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY^563


outlined in the problem statement. By using SortedListobjects, we have already taken
care of sorting the output: We will create the addresses in sorted order.


Responsibility Algorithms:The first two responsibilities are concrete. The next two
responsibilities are identical except for the name of the SortedListobject and the file
name. Let’s set up this processing as a helper method called “Generate lists”.


The fifth responsibility poses a greater challenge. When faced with a complex prob-
lem, a good strategy is to consider how you might solve the problem by hand (that is, to
look for things that are familiar). One algorithm might be to take the first name on the
first list, write it to the new list, and search the second list to see if it is found there. If
the name is present, it would be a duplicate, so we would cross it off the second list.
When we finish going through the first list, we then add to the new list all of the names
that remain on the second list. For each item on the first list, we must search the entire
second list.
This by-hand algorithm serves as a good model for a computer solution. Because the
lists are ordered alphabetically, however, a more efficient algorithm is possible. Let’s


Generate lists(SortedList list, ObjectInputStream inFile)
Set more data to true
whilemore data
try
Read an entry from inFile
list.insert(entry)
catchEOFException
Set more data to false

Class Name: LabelDriver Superclass: Object Subclasses:


Responsibilities Collaborations
Prepare files for input ObjectInputStream

Prepare files for output PrintWriter ObjectOutputStream
Read first file into SortedList SortedList
Read second file into SortedList SortedList
Merge two lists into third SortedList SortedList
Print labels PrintWriter
Write combined file ObjectOutputStream
Free download pdf