Java Reference
In-Depth Information
6.6
I
mproving the quality of the ACME project
If you've been following along with the
ACME
exercises, you may have noticed
that we're suffering from a small bug. Running the command-line client tells you
that the Canadian dollar is stronger than the
US
dollar! Obviously, there's some
sort of logic bug in the code: The project compiles just fine, but the results don't
make common sense. This is a case where using
IDEA
's debugger may help you
discover where the erroneous code is hiding.
Open the
CommandLineClient
file, and click once on the gutter at the line
inside the
main()
method that reads
if (args.length != 2 && args.length != 3) {
A red circle indicating a line breakpoint should appear in the gutter; this break-
point will pause the execution of the program whenever the specified line is
reached. Having placed the breakpoint successfully, select the Run/Debug con-
figuration that runs a simple conversion from the drop-down control in the menu
bar, and select the
Run | Debug
menu option (
Shift+F9
).
The breakpoint is quickly marked with a check (indicating its success as a valid
breakpoint), and the running system comes to a stop as that line of code is
reached. The Editor window shows the breakpointed line highlighted in blue,
because this is where the current execution focus is found. In addition, the
Debug
tool window, usually docked to the bottom of the screen, shows the active frame
and all its scoped variables. Feel free to explore this interface while the debugged
program waits patiently.
To step through execution of the code, use the buttons on the left of the
Debug
tool window or the keyboard shortcuts. As noted in the text,
Step Into
lets
you follow execution into a submethod, whereas
Step Over
lets the method
return its result and keeps you at your current level on the stack.
Continue pressing
F8
(
Step Over
), watching the values of your variables as
each step occurs. Note that the rate is being properly returned from the service,
and the
oldAmount
is being parsed from the command line properly. It's the cal-
culation of the
newAmount
that seems to be wrong. 5.00 dollars
USD
should not
equate to 3.33 dollars
CDN
. The bug is in the formula: That line in
CommandLi-
neClient
should set
newAmount
to
oldAmount * rate
, not
oldAmount * (1/rate)
.
Repair the line, and rerun the configuration to see if the bug has been dis-
patched. Don't forget to rebuild/recompile before you test!