Programming and Problem Solving with Java

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

The Building-Block Approach


Another way of attacking a large problem
is to see if any solutions for smaller pieces
of the problem exist. It may be possible to
combine these solutions to solve most of
the big problem.This strategy is just a com-
bination of the look-for-familiar-things and
divide-and-conquer approaches. You look
at the big problem and see that it can be di-
vided into smaller problems for which so-
lutions already exist. Solving the big
problem is just a matter of putting the ex-
isting solutions together, like mortaring to-
gether blocks to form a wall (see Figure 1.17).
With an object-oriented programming
language, the building blocks are classes.
We often solve a problem by looking in the
class library to see which solutions have been developed previously; we then write a small
amount of additional code to put the pieces together. As we will see later, this problem-solv-
ing technique forms the basis for the methodology calledobject-based design.


Merging Solutions


Another way to combine existing solutions is to merge them on a step-by-step basis. For ex-
ample, to compute the average of a list of values, we must both sum and count the values.
If we already have separate solutions for summing values and for counting the number of


Shipping
System

Existing
Software

Existing
Software

New Program

Receiving
System

Parts
shipped

New code to mortar blocks together

Inventory System

Parts
received

Parts on
order

Parts sold,
not shipped

Accounts
Payable
System

Accounts
Receivable
System

Figure 1.17 Building-Block Approach


Easy
subproblem

Easy
subproblem

Hard problem

Hard
subproblem

Easy
subproblem

Easy
subproblem

Figure 1.16 Divide and Conquer
Free download pdf