Most commercial computer programs are larger than the programs we have seen thus far.
A good way to manage the complexity of a large program is to decompose it into smaller pieces, decomposing
these pieces until each piece is small and manageable (e.g., less than 100 lines of Java).
This technique sometimes is called divide and conquer.
There are 2 important ways that a problem is decomposed into smaller units.
Here, we introduce one of them: Decomposing a large problem into methods, decomposing these methods into
smaller methods until each method is small and manageable.
The Java API (Application Program Interface) contains a rich collection of pre-defined methods (i.e., methods
that already have been written for you).
All of the methods that we have used thus far were written for us, from paint, init, repaint, and action to
things like getText, toString, and println.
Now, we start writing our own methods.
We write a method only once, but can use the method as many times as we like.
(The intelligently lazy programmer uses this technique artfully.)
A method is invoked by a method call.
g.drawString("There's madness to my method!", 25, 100);
The method call specifies:
the name of the method (drawString), and
the information it needs to do its work, its arguments ("There's madness to my method!", 25, 100 ).
Our textbook's analogy is to a hierarchical management style:
A manager requests (invokes) a worker to perform a certain task (method), supplying the worker with the
information necessary to complete the task (arguments), but not knowing or insisting how that task is to be
done.
The worker receives the information, performs the task, and returns the results to the manager.
In the process of performing the task, the worker may request the services of other workers to perform
subtasks, in a manner similar to the way that the manager requested his services.
In modern management, and in Java, this decomposition of complex tasks need not be strictly hierarchical.
See a figure that illustrates these working relationships.