Java Reference
In-Depth Information
Exercise 7.22 Using the object you created in the previous exercise, call the testPlus
method. What result does it give? Is that the same result as was printed by the call to tes-
tAll ? Call testPlus one more time. What result does it give now? Should it always give the
same answer? If so, what should that answer be? Take a look at the source of the testPlus
method to check.
Exercise 7.23 Repeat the previous exercise with the testMinus method. Does it always
give the same result?
The experiments above should have alerted you to the fact that not all seems to be right with
the CalcEngine class. It looks like it contains some errors. But what are they, and how can we
find them? In the sections that follow, we shall consider a number of different ways in which we
can try to locate where errors are occurring in a class.
Manual walkthroughs
Manual walkthroughs are a relatively underused technique, perhaps because they are a par-
ticularly “low-tech” debugging and testing technique. However, do not let this fool you into
thinking that they are not useful. A manual walkthrough involves printing copies of the classes
you are trying to understand or debug and then getting right away from your computer! It is all
too easy to spend a lot of time sitting in front of a computer screen not making much progress
in trying to deal with a programming problem. Relocating and refocusing your efforts can often
free your mind to attack a problem from a completely different direction. We have often found
that going off to lunch or cycling home from the office brings enlightenment that has otherwise
eluded us through hours of slogging away at the keyboard!
A walkthrough
is an activity of
working through a
segment of code
line by line while
observing changes
of state and other
behavior of the
A walkthrough involves both reading classes and tracing the flow of control between classes and
objects. This aids understanding both the ways in which objects interact with one another and how
they behave internally. In effect, a walkthrough is a pencil-and-paper simulation of what happens
inside the computer when you run a program. In practice, it is best to focus on a narrow portion of
an application, such as a single logical grouping of actions or even a single method call.
A high-level walkthrough
We shall illustrate the walkthrough technique with the calculator-engine project. You might
find it useful to print out copies of the CalcEngine and CalcEngineTester classes in order
to follow through the steps of this technique.
We shall start by examining the testPlus method of the CalcEngineTester class, as it contains
a single logical grouping of actions that should help us gain an understanding of how several meth-
ods of the CalcEngine class work together to fulfill the computation role of a calculator. As we
work our way through it, we shall often make penciled notes of questions that arise in our minds.
For this first stage, we do not want to delve into too much detail. We simply want to look at
how the testPlus method uses an engine object, without exploring the internal details of
the engine. From earlier experimentation, it would appear that there are some errors to be
Search WWH ::

Custom Search