Java Reference
In-Depth Information
***16.28
( Simulation: self-avoiding random walk ) A self-avoiding walk in a lattice is a
path from one point to another that does not visit the same point twice. Self-
avoiding walks have applications in physics, chemistry, and mathematics.
They can be used to model chain-like entities such as solvents and polymers.
Write a program that displays a random path that starts from the center and
ends at a point on the boundary, as shown in Figure 16.29a, or ends at a dead-
end point (i.e., surrounded by four points that have already been visited), as
shown in Figure 16.29b. Assume the size of the lattice is 16 by 16 .
(a) (b) (c) (d)
F IGURE 16.29 (a) A path ends at a boundary point. (b) A path ends at dead-end point. (c-d) Animation shows the progress
of a path step by step.
***16.29
( Animation: self-avoiding random walk ) Revise the preceding exercise to dis-
play the walk step by step in an animation, as shown in Figure 16.29c-d.
**16.30
( Simulation: self-avoiding random walk ) Write a simulation program to show
that the chance of getting dead-end paths increases as the grid size increases.
Your program simulates lattices with size from 10 to 80. For each lattice size,
simulate a self-avoiding random walk 10,000 times and display the probabil-
ity of the dead-end paths, as shown in the following sample output:
For a lattice of size 10, the probability of dead-end paths is 10.6%
For a lattice of size 11, the probability of dead-end paths is 14.0%
. . .
For a lattice of size 80, the probability of dead-end paths is 99.5%
*16.31
( Geometry: display an n-sided regular polygon ) Programming Exercise 13.25
created the RegularPolygonPanel for displaying an n -sided regular poly-
gon. Write a program that displays a regular polygon and uses two buttons
named and to increase or decrease the size of the polygon, as shown
in Figure 16.30a-b. Also enable the user to increase or decrease the size by
clicking the right or left mouse button and by pressing the UP and DOWN
arrow keys.
+
1
-
1
**16.32
( Geometry: add and remove points ) Write a program that lets the user click on
a panel to dynamically create and remove points (see Figure 16.30c). When
the user right-clicks the mouse, a point is created and displayed at the mouse
point. The user can remove a point by pointing to it and left-clicking the
mouse.
**16.33
( Geometry: pendulum ) Write a program that animates a pendulum swinging,
as shown in Figure 16.31. Press the UP arrow key to increase the speed and
the DOWN key to decrease it. Press the S key to stop animation and the R key
to resume it.
Search WWH ::




Custom Search