Chapter 6 Review Exercise Solutions
R6.1
Java supports while, for, and do loops. while loops are used when the loop must continue until a certain condition is met; for loops are used when a variable runs from a starting to an ending value with a constant increment or decrement; do loops are appropriate when the loop body must be executed at least once.
R6.2
The program prints a "multiplication table"--the remainders of the products of the numbers 0 . . . 9 divided by 10:
0000000000
0123456789
0246802468
0369258147
0482604826
0505050505
0628406284
0741852963
0864208642
0987654321
R6.3
for (i = 1; i <= 10; i++) . . .   10 times
for (i = 0; i < 10; i++) . . .    10 times
for (i = 10; i > 0; i--) . . .    10 times
for (i = -10; i <= 10; i++) . . . 21 times
for (i = 10; i >= 0; i++) . . .   never ends
for (i = -10; i <= 10; i = i + 2) . . . 11 times
for (i = -10; i <= 10; i = i + 3) . . . 7 times
R6.4
int s = 0; 
int i = 1; 
while (i <= 10) 
{ 
   s = s + i; 
   i = i + 1; 
} 
R6.5
int n = 1;
double x = 0;
double s = 1; // Trick to make sure the loop passes the first time
while (s > 0.01)
{  
   s = 1.0 / (n * n);
   x = x + s;
   n++;
}  
R6.6
An infinite loop is a loop that does not terminate. 
On many computers, you can kill the program by closing the console window in which the program executes, or by hitting a special key combination such as CTRL+C. If all else fails, you can always restart the computer :-) 
R6.7
while (!done)
{  
   read bridge name
   if (not OK) 
      done = true;
   else 
   {  
      read bridge length in feet
      if (not OK)
         done = true;
      else
      {   
         convert length to meters
         print bridge data
      }
   }
}

while (true)
{  
   read bridge name
   if (not OK) break;
   read bridge length in feet
   if (not OK) break;
   convert length to meters
   print bridge data
}

void readBridges()
{  
   while (true)
   {  
      read bridge name
      if (not OK) return;
      read bridge length in feet
      if (not OK) return;
      convert length to meters
      print bridge data
   }
}
R6.8
Random generator = new Random();
int numberToGuess = generator.nextInt(10) + 1;
int numTries = 0;
int guess = -1;
Scanner in = new Scanner(System.in);
while (numTries < 3 && guess != numberToGuess)
{
   System.out.println("Try to guess the number (between 1 and 10): ");
   guess = in.nextInt();
   numTries++;
}
if (guess == numberToGuess)
   System.out.println("You guessed the number!");
else
   System.out.println("Sorry, you couldn't guess the number.");
R6.9
Using a special number, such as a 0, as a sentinel works if there is some restriction on the input. In many cases, though, there isn’t. What if the program requires the number 0 for some input? Then the programmer must select a different sentinel. This can be troublesome because the programmer needs to think of a lucky number that the program doesn't use. A better idea is to use an input that is not a number, such as the letter Q.
R6.10
First call generator.nextInt(4) to get the card suit (e.g., 0 = clubs, 1 = spades, 
2 = hearts, 3 = diamonds). 
Then call generator.nextInt(13) to get the card value (1 = ace, 2 – 10 = face value, 
11 = Jack, 12 = Queen, 13 = King).
R6.11
An "off by one" error is when the index of a loop is one less or more than the actual iteration. From experience, the most common occurrence of this error would have to be in the for loop indexing, especially when used with strings: 
   for (i = 0; i <= s.length(); i++) // Oops...should be < s.length() 
R6.12
An example of a for loop where symmetric bounds are more natural would be one for executing a block of statements a given number of times:
for (int i = 1; i <= 10; i++) // Do ten times
When looking inside a string, you usually use an asymmetric loop: 
for (int i = 0; i < s.length(); i++)
R6.13
Nested loops are loops that have one loop situated inside another. An example is the code that is needed to print a table: 
   for (row = first; row <= last; row++)
   {  
      for (column = first; column <= last; column++)
         Print the (row, column) value
      Print a newline
   }
R6.14
•	Step into: steps inside method calls. You should step into a method to check whether it carries out its job correctly.
•	Step over: skips over method calls. You should step over a method if you know it works correctly.
R6.15
The procedure depends on the debugger. Most debuggers know about the String class and display its contents immediately when you ask to inspect or watch a string. However, you can also display the instance variables and inspect the value instance variable.
R6.16
This varies according to the debugger used. Typically, you inspect a variable of type Rectangle and carry out some action (such as clicking on a tree node) to open up the display of the instance variables. The x , y, width, and height instance variables should then be visible.
R6.17
This varies according to the debugger used. Typically, you inspect a variable of type BankAccount and carry out some action (such as clicking on a tree node) to open up the display of the instance variables. The balance variable should then be visible.
R6.18
The “divide-and-conquer” strategy involves stepping over the methods in main to pinpoint the location of the failure. When a failure occurs just after a particular method (say f), then restart main, set a breakpoint before f, step into f, and now apply the same strategy to f. Keep going until the error is found.