Java Reference
In-Depth Information
that different ways of performing that activity can be substituted. The visitor de-
sign pattern is focused on encapsulating an activity that will be performed on all
members of a collection so that completely different activities can be substituted
within a generic method that accesses all of the collection members.
1.4
Problems, Algorithms, and Programs
Programmers commonly deal with problems, algorithms, and computer programs.
These are three distinct concepts.
Problems: As your intuition would suggest, a problem is a task to be performed.
It is best thought of in terms of inputs and matching outputs. A problem definition
should not include any constraints on how the problem is to be solved. The solution
method should be developed only after the problem is precisely defined and thor-
oughly understood. However, a problem definition should include constraints on
the resources that may be consumed by any acceptable solution. For any problem
to be solved by a computer, there are always such constraints, whether stated or
implied. For example, any computer program may use only the main memory and
disk space available, and it must run in a “reasonable” amount of time.
Problems can be viewed as functions in the mathematical sense. A function
is a matching between inputs (the domain) and outputs (the range). An input
to a function might be a single value or a collection of information. The values
making up an input are called the parameters of the function. A specific selection
of values for the parameters is called an instance of the problem. For example,
the input parameter to a sorting function might be an array of integers. A particular
array of integers, with a given size and specific values for each position in the array,
would be an instance of the sorting problem. Different instances might generate the
same output. However, any problem instance must always result in the same output
every time the function is computed using that particular input.
This concept of all problems behaving like mathematical functions might not
match your intuition for the behavior of computer programs. You might know of
programs to which you can give the same input value on two separate occasions,
and two different outputs will result. For example, if you type “ date ” to a typical
UNIX command line prompt, you will get the current date. Naturally the date will
be different on different days, even though the same command is given. However,
there is obviously more to the input for the date program than the command that you
type to run the program. The date program computes a function. In other words,
on any particular day there can only be a single answer returned by a properly
running date program on a completely specified input. For all computer programs,
the output is completely determined by the program's full set of inputs. Even a
“random number generator” is completely determined by its inputs (although some
random number generating systems appear to get around this by accepting a random
Search WWH ::




Custom Search