Programming and Problem Solving with Java

(やまだぃちぅ) #1
Summary | 255

10.Use a debuggerif your system provides this kind of application. A debugger
runs your application in “slow motion,” allowing you to execute one
instruction at a time and to examine the contents of variables as they change.
If you haven’t already done so, find out whether a debugger is available on
your system.
11.If all else fails, use debug output statements—output statements inserted into
an application to help debug it. They output a message to a separate file that
indicates the flow of execution in the application or reports the values of vari-
ables at certain points in the application.
For example, if you want to know the value of variable betaat a certain
point in an application, you could insert this statement:
logFile.println("beta = "+ beta);
If this output statement appears in a loop, you will get as many values of beta
on the file associated with logFileas there are iterations of the body of the
loop.
After you have debugged your application, you can remove the debug out-
put statements or just precede them with //so that they’ll be treated as com-
ments. (This practice is referred to as commenting outa piece of code.) You can
remove the double slashes if you need to use the statements again.
12.An ounce of prevention is worth a pound of debugging. Use the checklist
questions, and design your loop correctly at the outset. It may seem like extra
work, but it pays off in the long run.

Summary


Applications operate on data. If data and applications are kept separate, the data are
available to use with other applications, and the same application can be run with
different sets of data. Noninteractive input/output allows data to be prepared before
an application is run and allows the application to be run again with the same data
in the event that a problem crops up during processing.
Data files are often used for noninteractive processing and to permit the output
from one application to be used as input to another application. In Java, we use four
file classes to read and write text data:FileReader,FileWriter,BufferedReader, and
PrintWriter.FileReaderandFileWriterwork with individual characters, and we use

Free download pdf