1 P1. Simple Loops    Often it is necessary to repeat a portion of coding several times in a program. A simple loop can automate the repetition. Here is a program that computes the number of digits needed to represent a number in base 10./*   Count number of digits needed to express an integer in base 10   using multiple if statements*/public static void main(String[] args){   String input = JOptionPane.showInputDialog(      "Input an integer between 1 and 9999");   int n = Integer.parseInt(n):   if (n < 1 || n > 9999) return;   int temp = n;   int d = 1;   if (temp > 9)   {      temp = temp / 10;      d++;   }   if (temp > 9)   {      temp = temp / 10;      d++;   }   if (temp > 9)   {      temp = temp / 10;      d++;   }   if (temp > 9)   {      temp = temp / 10;      d++;   }   System.out.println(input + " can be expressed in " + d + " digits");   System.exit(0);}Repeating the section below four times, even using copy/paste, is tedious, and the coding works only for input <= 9999.   if (temp > 9)   {        temp = temp / 10;      d++;   }It is helpful to have a way of testing that the input is greater than 1, and to execute the succeeding control block if it is. Replacing if with while does this./*   Count number of digits needed to express an integer in base 10   using while loop*/public static void main(String[] args){   String input = JOptionPane.showInputDialog(      "Input an integer between 1 and 9999:");   int n = Integer.parseInt(n):   if (n < 1 || n > 9999) return;   int d = 1;   int temp = n;   while (temp > 9)   {      temp = temp / 10;      d++;   }   System.out.println(input + " can be expressed in " + d + " digits");   System.exit(0);}    The fractions 1/2, 1/4, 1/8 get closer and closer to 0 as they are divided by two. Change the previous program to count the number of divisions by two needed to be within 0.0001 of zero. Answer:

 2 P2. Loop TerminationWhich values of nyear cause the following loops to terminate?/* Count the number of years from a user-input year until the year 3000. */ public static void main(String[] args) {    int millennium = 3000;    String input = JOptionPane.showInputDialog(      "Please enter the current year:");    int nyear = Integer.parseInt(input);    while (nyear != millennium)    {        nyear++;    }System.out.println(" Another " + (millennium - nyear) + "years to the millennium.");   System.exit(0);} Answer:

 3 Re-write the preceding while loop so that it will terminate for any integer input. Answer:

 4 P3. for LoopsA variable that counts the iterations of a loop is called a loop index or loop control variable. In the preceding examples nyear serves as an index, counting the number of years to the next millennium. This type of loop is frequently written using the for idiom. for (loop_index = start_value; condition; index_increment) Write a program controlled by two (non-nested) for loops that produces the following listing of inclusive dates, from the fifth century B.C. through the fifth century A.D.Century 5 BC 400-499 Century 4 BC 300-399 Century 3 BC 200-299 Century 2 BC 100-199 Century 1 BC 1-99 Century 1 AD 1-99 Century 2 AD 100-199 Century 3 AD 200-299 Century 4 AD 300-399 Century 5 AD 400-499 Answer:

 5 Write the same program with a single loop for (i = -5 ; i <= 5 ; i++) and an if in the body of the loop. Answer:

6.

R1. Other Loops

One loop type might be better suited than another to a particular purpose. The following usages are idiomatic.

 for Known number of iterations
 while Unknown number of iterations
 do while At least one iteration

Convert the following while loop to a do while loop.

public static void main(String[] args)
{
int sum = 0;
int n = 1;
while (n != 0)
{
String input = JOptionPane(
"Please enter a number, 0 to quit:");
n = Integer.parseInt(input);
if (n != 0)
{
sum += n;
System.out.println("Sum = " + sum);
}
}
}

 7 Is this an improvement? Why or why not? Answer:

 8 Convert the while loop to a for loop./* Program to compute the first integral power to which 2 can be   raised that is greater than that multiple of a given integer.*/ public static void main(String[] args) {   String input = JOptionPane.showInputDialog(      "Please enter a number, 0 to quit:");   int n = Integer.parseInt(input);   int i = 1;   while (n * n > Math.pow(2,i))   {       i++;   }    System.out.println("2 raised to " + i      + " is the first power of two greater than " + n + " squared");   } } Answer:

 9 Is this an improvement? Why or why not? Answer:

 10 Convert to a while loop:public static void main(String[] args) {   int i;   for (i = 1; i <= 10; i++)   {        System.out.println(i + " squared equals " + i * i);   } } Answer:

 11 Is this an improvement? Why or why not? Answer:

 12 P4. Iterating through a StringYou can access the individual characters in a string by the charAt method of the String class. The charAt method returns a character of type char. Note that char constants are enclosed in single quotes such as 'e'.Many word-processors can check spelling. One of the corrections applied is to swap 'e' for 'i' if 'i' occurs immediately before 'e' and immediately after 'c'. For example, concieve is corrected to conceive. Write a program that carries out this correction. Your program should read a string and print the corrected string (or the original string if no correction is necessary). Answer:

 13 You can access through the individual words of a string by using a StringTokenizer.StringTokenizer tokenizer = new StringTokenizer(aString); while (tokenizer.hasMoreTokens()) { String word = tokenizer.nextToken();   do something with word }Write a program that reads a line of input from the user and then prints out all words in the input line that start with an uppercase letter. For example, if the input line is My mother lives in Maine and drives a Mercedes, then your program outputsMy Maine Mercedes Answer:

 14 R2. Tracing LoopsWhat is the output of the following loop?for ( i = 0 ; i < 5 ; i++ ) {      System.out.print(i + " "); } Answer:

 15 Leave the loop as it is and change only the expression inside System.out.print so that the program will display "1 2 3 4 5 ".What change do you make to the argument of System.out.print? Answer:

 16 What is the output of the following loop?int decimals = 1; while (decimals < 100000) { System.out.print(decimals + " ");   decimals *= 10; } Answer:

 17 Leave the loop as it is and change only the expression inside System.out.print so that the program will display "1 2 3 4 5 ".What change do you make to the argument of System.out.print? Answer:

 18 What is the output of the following loop?int i = 5; do {    System.out.print(i + " ");    i--; }   while( i > 0 ); Answer:

 19 Leave the loop as it is and change only the expression inside System.out.print so that the program will display "1 2 3 4 5 ".What change do you make to the argument of System.out.print? Answer:

 20 P5. Nested LoopsWrite a program to draw a top view of 24 soda cans, that is 24 circles, arranged in a 4 x 6 grid like this: Answer: