1.7 Problem-Solving Techniques | 25
We also apply this strategy by using classes from the Java library. When we recognize an
object that we’ve seen before, we reuse it in the new problem. Even if an existing class isn’t
quite what we need, it can serve as the starting point for a new class. In Chapter 7, we see
how we can implement this problem-solving strategy in Java by using a mechanism called
inheritance, which allows us to define a new object that adds to the capabilities of an exist-
ing object.
Solve by Analogy
Often a problem may remind you of one you have seen before. You may find solving the
problem at hand easier if you remember how you solved the other problem. In other words,
you can draw an analogy between the two problems. For example, a solution to a perspec-
tive projection problem from an art class might help you figure out how to compute the dis-
tance to a landmark when you are on a cross-country hike. As you work your way through
the new problem, you may come across things that are different than they were in the old
problem, but usually you can deal with these minor details one at a time.
Analogy is really just a broader application of the strategy of looking for things that are
familiar. When you are trying to find an algorithm for solving a problem, don’t limit yourself
to computer-oriented solutions. Step back and try to get a larger view of the problem. Don’t
worry if your analogy doesn’t match perfectly—the only reason for starting with an analogy
is that it gives you a place to start (see Figure 1.14). The best programmers are people who
have broad experience solving all kinds of problems.
Means-Ends Analysis
Often the beginning state and the ending state are given; the problem requires you to define
a set of interactions between objects that takes you from one state to the other. Suppose you
want to go from Boston, Massachusetts, to Austin, Texas. You know the beginning state (you
are in Boston) and the ending state (you want to be in Austin). The problem is how to get from
one place to the other.
A library catalog system can give insight into how to organize a parts inventory.
Figure 1.14 Analogy