Java Reference
In-Depth Information
7.26 (Knight's Tour: Closed-Tour Test) In the Knight's Tour (Exercise 7.22), a full tour occurs
when the knight makes 64 moves, touching each square of the chessboard once and only once. A
closed tour occurs when the 64th move is one move away from the square in which the knight start-
ed the tour. Modify the application you wrote in Exercise 7.22 to test for a closed tour if a full tour
has occurred.
7.27 (Sieve of Eratosthenes) A prime number is any integer greater than 1 that's evenly divisible
only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as
follows:
a) Create a primitive-type boolean array with all elements initialized to true . Array ele-
ments with prime indices will remain true . All other array elements will eventually be
set to false .
b) Starting with array index 2, determine whether a given element is true . If so, loop
through the remainder of the array and set to false every element whose index is a mul-
tiple of the index for the element with value true . Then continue the process with the
next element with value true . For array index 2, all elements beyond element 2 in the
array that have indices which are multiples of 2 (indices 4, 6, 8, 10, etc.) will be set to
false ; for array index 3, all elements beyond element 3 in the array that have indices
which are multiples of 3 (indices 6, 9, 12, 15, etc.) will be set to false ; and so on.
When this process completes, the array elements that are still true indicate that the index is a
prime number. These indices can be displayed. Write an application that uses an array of 1,000 ele-
ments to determine and display the prime numbers between 2 and 999. Ignore array elements 0
and 1.
7.28 (Simulation: The Tortoise and the Hare) In this problem, you'll re-create the classic race of
the tortoise and the hare. You'll use random-number generation to develop a simulation of this
memorable event.
Our contenders begin the race at square 1 of 70 squares. Each square represents a possible
position along the race course. The finish line is at square 70. The first contender to reach or pass
square 70 is rewarded with a pail of fresh carrots and lettuce. The course weaves its way up the side
of a slippery mountain, so occasionally the contenders lose ground.
A clock ticks once per second. With each tick of the clock, your application should adjust the
position of the animals according to the rules in Fig. 7.32. Use variables to keep track of the posi-
tions of the animals (i.e., position numbers are 1-70). Start each animal at position 1 (the “starting
gate”). If an animal slips left before square 1, move it back to square 1.
Animal
Move type
Percentage of the time
Actual move
To r t o i s e
Fast plod
50%
3 squares to the right
Slip
20%
6 squares to the left
Slow plod
30%
1 square to the right
Hare
Sleep
20%
No move at all
Big hop
20%
9 squares to the right
Big slip
10%
12 squares to the left
Small hop
30%
1 square to the right
Small slip
20%
2 squares to the left
Fig. 7.32 | Rules for adjusting the positions of the tortoise and the hare.
 
Search WWH ::




Custom Search