Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY^31


Each of these responsibilities requires further expansion. For example, in making up
the food list, Sally needs to take into account any food allergies among the guests. It
takes James several steps to set up the stereo, and he has to develop a play-list of the
music so that people won’t have to wait between dances while he chooses the next
song. Calling the guests involves keeping track of who answered the phone and who
needs another call. This algorithm is just a top-level solution. Developing a complete
solution would require you to work out and write down all of the details. Computers re-
quire such complete solutions. Fortunately, for a problem like party planning, you can
count on your friends to work out the details themselves. (If you were planning a flight
of the space shuttle, where any misstep could have serious consequences, you’d work
out every step that everyone needs to do. Such life-critical tasks are often programmed
with the same precision that a computer requires.)
Because the number of potential guests to call is limited, you decide that it would be
easier to merge this part of the solution into a single responsibility that you handle.
Often, when we first write down a problem solution, we notice things that we want to
change. Our solutions are not written in stone—we can modify them as necessary.
However, it is helpful in programming to settle on a solution and avoid making unnec-
essary changes once we begin coding.
Our collection of objects needs to be coordinated in carrying out their responsibili-
ties. We don’t just perform these steps in random fashion. In programming, such a


James
Call guests and keep a count of the ones who can come.
Select music and gather it together.
Deliver and set up stereo.
Run stereo.
Help lead games.
Clean up apartment and move furniture back.
Return stereo and put away music.

Sally
Call guests and keep a count of the ones who can come.
Get counts from you and James and sum them.
Make a shopping list and buy food.
Help James deliver the stereo.
Take care of food at party.
Clean up apartment and move furniture back.
Help James return the stereo.
Free download pdf