Java Reference
In-Depth Information
This allows the method poolCapacity to be called while the program is being coded.
Ultimately, the stub for the method poolCapacity is replaced with a method that
properly calculates the amount of water needed to fill the pool based on the values of
the parameters. In the meantime, the method stub allows work to continue on other parts
of the program that call the method poolCapacity .
Avoiding Bugs: One-Piece-at-a-Time Coding
It is evident from the programming examples given in this and the previous chapters, that
before a programming code is written, the problem must be thoroughly understood and
analyzed.Iftheproblemislargeandcomplex,itmustbebrokenintosubproblems,andif
a subproblem is still complex, it must further be divided into subproblems. The subdivi-
sion of a problem should continue to the point where the solution is clear and obvious.
After thoroughly understanding and analyzing a (sub) problem, an algorithm is designed,
which can be coded into a programming language such as Java. Once a subproblem is
solved, we can continue with the solution of another subproblem and if all the subpro-
blems of a problem are solved, we can continue with the next level. Eventually, the
overall solution of the problem must be assembled and tested to ensure that the
programming code accomplishes the required task.
In general, a Java program is a collection of classes and a class is a collection of data members
and methods. (Sometimes a class may also contain an inner class.) Each class and each method
must work properly. To accomplish this, as explained in the previous section, once a method
is written, it can be tested using stubs and drivers. (Note that for complex methods and
algorithms sometimes a mathematical proof may be required to verify the correctness of the
algorithm. The discussion of this is beyond the scope of this topic.) Since a method can
be tested in isolation, it is not necessary to code all the methods in order. However, once all
the methods are written, the overall program must be tested.
The technique to solve a problem by subdividing into smaller problems is known as divide-
and-conquer and top-down design approach. These techniques are suitable and work for
many kinds of problems, including most of the problems given in this topic and the problems
you will encounter as a beginning programmer.
To simplify the overall solution of a problem that consists of many subproblems, we
write and test the code one piece at a time. Typically, once a subproblem is solved and
the code is tested, it is saved as the first version or a version of the program. We continue
to add and save the program one piece at a time. Keep in mind that a working program
with fewer features is better than a nonworking one with many features.
DEBUGGING
QUICK REVIEW
1 . Methods enable you to divide a program into manageable tasks.
2 . The Java system provides standard (predefined) methods.
 
 
Search WWH ::




Custom Search