Java Reference
In-Depth Information
// { k < 1000 }
Reduce k to 0 , keeping the definition of s and k true ;
return s;
In making this refinement, we have made progress. The statement to reduce
k can now assume that k < 1000 . And, because of the way the statement to reduce
k is written, the program is still correct.
To test what we have done so far, we can replace the expression “English
equivalent of k / 1000 ” by (k / 1000) . We should call the function several times,
perhaps with several boundary-case arguments — 999999 , 2000 , 1001 , 1000 ,
999 , and 1 — and make sure we get the expected answers back. We can then pro-
ceed to refine “English equivalent of k / 1000 ”.
We stop the development of this algorithm here because the main ideas have
been illustrated. Activity 2-5.4 on the CD explains the development far better
than can be done on paper.
Summary of the functional and procedural approaches
The functional approach emphasizes the use of function calls and de-empha-
sizes the use of local variables and assignment statements. The procedural
approach makes heavy use of variables and assignments and creates far fewer
functions. Which method you prefer is a matter of taste —and perhaps your pre-
vious programming experiences. If you prefer one over the other, make a con-
scious effort to practice the other so that you become adept at both approaches.
Then, you can use whichever is more preferable in any given situation.
Assertions in programs
A program usually contains comments to help the reader understand it. Some
comments explain what a program segment does. Other comments describe rela-
tionships between variables of the program. Here are two examples of the latter
type of comment:
Style Note
13.2, 13.2.2
// { x < y }
// { n is the number of values read in so far }
In this section, we study such comments. We begin by studying the notion of a
relation .
Relations about variables and values
A relation is simply a true-false statement about some variables. For example,
the relation 2<3 is a true statement, which happens not to mention any variables
at all, while the relation 2=3 is a false statement. The relation 2<x concerns the
single variable x ; we cannot tell whether it is true or false until we know what
Search WWH ::

Custom Search