Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^26) | Introduction to Object-Oriented Programming
In this example, you have lots of options. You can take a plane, walk, hitchhike, ride a bike,
or whatever. The method you choose depends on your circumstances. If you’re in a hurry,
you’ll probably decide to fly.
Once you’ve identified the essential objects and their capabilities (airplane; fly between
cities), you have to work out the details. It may help to establish intermediate goals that are eas-
ier to meet than the overall goal. Suppose a really cheap, direct flight to Austin goes out of
Newark, New Jersey. You might decide to divide the trip into legs: Boston to Newark, and then
Newark to Austin. Your intermediate goal is to get from Boston to Newark. Now you merely have
to examine the means of meeting that intermediate goal (see Figure 1.15). Is there an object (air-
plane) that has the necessary capabilities (fly between Boston and Newark)?
The overall strategy of means-ends analysis is to define the ends and then to analyze your
means of achieving them. The process translates easily to computer programming. That is,
you begin by writing down what the input is and what the output should be. Then you con-
sider the available objects and the actions they can perform and choose a sequence of those
actions that can transform the input into the desired results. If no appropriate object is avail-
able, then you may have to create a new one.


Divide and Conquer


We often break up large problems into smaller units that are easier to handle. Cleaning the
whole house may seem overwhelming; cleaning each room, one at a time, seems much
more manageable. The same principle applies to programming. We break up a large prob-
lem into smaller pieces that we can solve individually (see Figure 1.16). A problem is di-
vided into its component objects, and for each class of objects we define a set of capabilities.

Means: Fly, walk, hitchhike, bike,
drive, sail, bus

Means to Intermediate Goal: Commuter flight,
walk, hitchhike, bike, drive, sail, bus

Revised Means: Fly to Chicago and then to Austin;
fly to Newark and then to Austin: fly to Atlanta
and then to Austin

Start: Boston
Goal: Austin

Start: Boston
Goal: Austin

Start: Boston
Intermediate Goal: Newark
Goal: Austin

Solution: Take commuter flight to Newark and then catch cheap flight to Austin

Figure 1.15 Means-Ends Analysis
Free download pdf