Programming and Problem Solving with Java

(やまだぃちぅ) #1
1.7 Problem-Solving Techniques | 29

Remember that you always have a place to begin when trying to solve any problem: Write
it down on paper in your own words so that you understand it. Once you paraphrase the
problem, you can focus on each of the subparts individually instead of trying to tackle the en-
tire problem at once. This process gives you a clearer picture of the overall problem. It helps
you see pieces of the problem that look familiar or that are analogous to other problems you
have solved in the past. It also pinpoints areas where something is unclear, where you need
more information.
As you write down a problem, you tend to group things together into small, under-
standable chunks of data and operations, which may be natural candidates for objects. Your
description of the problem may collect all of the information about data and results into one
place for easy reference. Then you can see the beginning and ending states necessary for
means-ends analysis.
Most mental blocks are caused by not really understanding the problem. Writing down
the problem in your own words is a good way to focus on the subparts of the problem, one
at a time, and to understand what is required for a solution.


Object-Oriented Problem Solving


The initial step in solving many problems is to identify the obvious objects in the problem
description. If you are given a recipe, for example, the first thing you do is to identify the in-
gredients. Some may already be in your cupboard or refrigerator. For others, you may have to
go shopping. Object-oriented problem solving involves much the same process. You look at
a problem statement and make a list of the objects in it. Some of those objects are already avail-
able in the Java library, just waiting to be used. Other objects you may have to write yourself.
Remember that the computer can do only certain things. Your primary concern, then, is
making the computer coordinate the actions of objects to produce the desired effects. If you
keep in mind the objects, operations, and data types available in Java, you won’t design an
algorithm that is difficult or impossible to code.
In this book, we introduce you to these object classes and operations in a gradual man-
ner. In each chapter, we add to our knowledge of Java and programming techniques so that
we can tackle a growing range of problems. At first the problems may seem trivial because
we have so much to learn before we can write even a simple program. Soon, however, we
encounter problems that require thoughtful use of these problem-solving techniques. In
Chapter 6, we introduce an object-oriented design technique that helps us to organize our
problem solving for even larger programs.
Coming up with an algorithm for solving a particular problem is not always a cut-and-
dried process. Just the opposite, in fact—it is usually a trial-and-error process requiring
several attempts and refinements. We test each attempt to see if it really solves the problem.
If it does, that’s great. If it doesn’t, we try again. You typically use a combination of the
techniques we’ve described to solve any nontrivial problem.

Free download pdf