Java Reference
In-Depth Information
z*x y = z*x*x y-1
Therefore, the assignments to z and y keep the value of z*x y the same, so the
value of the first conjunct remains unchanged.
In both cases — y even and y odd— the repetend keeps the invariant true.
With the aid of the invariant, we have analyzed the algorithm and seen that
it is correct.
7.3.3
The spiral
Activity 7-2.4 of the CD shows you a loop that draws spirals of 2000 line seg-
ments of increasing size, with successive lines being drawn in alternating colors
at the same angle. Change the angle, and you get radically different designs.
Watch the activity so that you can see the colorful and beautiful designs that arise
from this one loop. Amazingly different designs are drawn just by changing the
angle between successive lines.
On this black-and-white paper, we cannot do justice to the spirals that are
drawn, to the description of what each spiral consists of, or to the loop itself.
Therefore, we do not discuss the algorithm here. Look at it on the CD. Also,
obtain the program from the CD and run it yourself, experimenting with differ-
ent angles to see the designs that arise.
Activity
7-2.4
See lesson
page 7-2 to ob-
tain this loop.
7.4
Loop patterns
A schema is just a “generalized presentation or a framework of reference”. A
loop schema is a loop (with initialization) that performs an abstract task, like:
Process the natural numbers 0..n-1
whose repetend includes an abstract statement, like:
Process k
We can use such a schema in many ways. For example, suppose we need to count
the w 's in a String s . We need not program this from scratch. Instead, because
the characters are numbered 0 through s.length() - 1 , we can start with the
loop schema and refine it to fit the new task. We investigate this idea.
7.4.1
Schema to process natural numbers
We develop an abstract loop to sequence through the integers in 0 .. n-1 , for
some natural number n , processing each one in turn. This loop is abstract because
we do not say what it means to process a number.
The abstract loop should do this:
Activity
7-3.1
Search WWH ::

Custom Search