[Page 291 (continued)] Solution 6.1 | Identify the syntax error in the following for loop statements: Commas are used instead of semicolons in the header. for (int k = 5; k < 100; k++) System.out.println(k); There should not be three semicolons in the header for (int k = 0; k < 12 ; k--) System.out.println(k); | Solution 6.2 | Identify the statements that result in infinite loops: Infinite loop because k is never incremented. Infinite loop because k is always odd and thus never equal to 100. | Solution 6.3 | Your sister is learning to count by fours. Write a for loop that prints the following sequence of numbers: 1, 5, 9, 13, 17, 21, 25. for (int k = 1; k <= 25; k = k+4) System.out.print(k + " "); | Solution 6.4 | What value will j have when the following loop terminates? Answer: j will be undefined when the loop terminates. It is a local variable whose scope is limited to the loop body. for (int i = 0; i < 10; i++) { int j; j = j + 1; } | | [Page 292] | Solution 6.5 | Write a nested for loop to print the following geometric pattern: # # # # # # # # # # # # # # # for(int row = 1; row <= 5; row++) { // For each row for (int col = 1; col <= row; col++) // Columns per row System.out.print('#'); System.out.println(); // New line } // row | Solution 6.6 | Identify the syntax error in the following while structures: int k = 5; while (k < 100) { System.out.println(k); k++ << Missing semicolon } int k = 0; while (k < 12;) { << Extra semicolon System.out.println(k); k++; } | Solution 6.7 | Determine the output and/or identify the error in each of the following while structures. int k = 0; while (k < 100) System.out.println(k); << Missing updater in loop body Output: infinite loop prints 0 0 0 0 0. . . while (k < 100) { << Missing initializer System.out.println(k); k++; } Output: unpredictable since k's initial value is not known | | [Page 293] | Solution 6.8 | Your younger sister is now learning how to count by sixes. Write a while loop that prints the following sequence of numbers: 0, 6, 12, 18, 24, 30, 36. int k = 0; // Initializer while (k <= 36) { // Loop-entry condition System.out.println(k); k += 6; // Updater } | Solution 6.9 | If N is even, divide it by 2. If N is odd, subtract 1 and then divide it by 2. This will generate a sequence that is guaranteed to terminate at 0. For example, if N is initially 15, then you get the sequence 15, 7, 3, 1, 0. Write a method that implements this sequence using a while statement. public static void sub1Div2(int N) { while(N != 0) { System.out.print(N + " "); if (N % 2 == 0) N = N / 2; else N = (N - 1) / 2; } System.out.println( N ); } // sub1Div2() | Solution 6.10 | Identify the syntax error in the following do-while structures: int k = 0; do while (k < 100) << Misplaced condition { System.out.println(k); k++; } << Belongs here int k = 0; do { System.out.println(k); k++; } while (k < 12) << Missing semicolon
| | [Page 294] | Solution 6.11 | Your sister has moved on to counting by sevens. Write a do-while loop that prints the following sequence of numbers: 1, 8, 15, 22, 29, 36, 43. n = 1; // Initializer do { System.out.print(n + " "); n += 7; // Updater } while (n <= 43); // Loop entry condition | Solution 6.12 | Write a method to input and validate pizza sales. public int getAndValidatePizzaPrice() { // Uses KeyboardReader int pizza = 0; do { reader.prompt("Input a pizza price (8, 10, or 15) "); reader.prompt("or 99 to end the list >> "); pizza = reader.getKeyboardInteger(); if ((pizza != 99) && (pizza != 8) && (pizza != 10) && (pizza != 15)) System.out.println("Error: you've entered an " + "invalid pizza price\n"); // Error input else // OK input System.out.println("You input " + pizza + "\n"); } while ((pizza != 99) && (pizza != 8) && (pizza != 10) && (pizza != 15)); return pizza; } // getAndValidatePizzaPrice() | Solution 6.13 | Write a method to input and validate pizza sales using the numbers 1, 2, and 3 to represent pizzas at different price levels. public int getAndValidatePizzaPrice() { // Uses KeyboardReader int pizza = 0; do { reader.prompt("Input a 1,2 or 3 to indicate pizza" + "price ( 1($8), 2($10), or 3($15) ) "); reader.prompt("or 0 to end the list >> "); pizza = reader.getKeyboardInteger(); if ((pizza < 0) || (pizza > 3)) // Error check System.out.println("Error: you've entered an " + "invalid value\n"); else // OK input System.out.println("You input " + pizza + "\n"); } while ( (pizza < 0) || (pizza > 3) ); if (pizza == 1) return 8; else if (pizza == 2) return 10; else if (pizza == 3) return 15; else return 0; } // getAndValidatePizzaPrice() | | [Page 295] | Solution 6.14 | For each of the following problems, decide whether a counting loop structure, a while structure, or a do-while structure should be used, and write a pseudocode algorithm. Printing the names of all the visitors to a Web site could use a counting loop because the exact number of visitors is known. for each name in the visitor's log print the name Validating that a user has entered a positive number requires a do-while structure in which you repeatedly read a number and validate it. do read a number if number is invalid, print error message while number is invalid Changing all the backslashes (\) in a Windows Web page address to the slashes (/) used in a Unix Web page address. for each character in the Web page address if it is a backslash replace it with slash Finding the largest in a list of numbers requires a while loop to guard against an empty list. initialize maxMPG to smallest possible number while there are more cars in the database if current car's MPG is greater than maxMPG replace maxMPG with it | Solution 6.15 | Identify any errors in the following switch structures (if there is no error, specify the output): int k = 0; switch (k) // Syntax error: missing braces case 0: System.out.println("zero"); break; case 1: System.out.println("one"); break; default: System.out.println("default"); break; [Page 296]int k = 0; switch (k + 1) { case 0: System.out.println("zero"); break; case 1: System.out.println("one"); // Output "one" break; default: System.out.println("default"); break; } int k = 6; switch (k / 3.0) // Syntax error: not an integral value { case 2: System.out.println("zero"); break; case 3: System.out.println("one"); break; default: System.out.println("default"); break; } | Solution 6.16 | A switch statement to print ice cream flavors: switch (flavor) { case 1: System.out.println("Vanilla"); break; case 2: System.out.println("Chocolate"); break; case 3: System.out.println("Strawberry"); break; default: System.out.println("Error"); } | | [Page 297] | Solution 6.17 | public final int VANILLA = 0, CHOCOLATE = 1, STRAWBERRY = 2; switch (flavor) { case VANILLA: System.out.println("Vanilla"); break; case CHOCOLATE: System.out.println("Chocolate"); break; case STRAWBERRY: System.out.println("Strawberry"); break; default: System.out.println("Error"); } | Solution 6.18 | Identify the pre- and postconditions on j and k where indicated in the following code segment: int j = 0; k = 5; do { if (k % 5 == 0) { // Precondition: j <= k j += k; k--; } else k *= k; } while (j <= k); // Postcondition: j > k | Solution 6.19 | Identify the pre- and postconditions for the following method, which computes xn for n >= 0. // Precondition: N >= 0 // Postcondition: power(x,n) == x to the n public double power(double x, int n ) { double pow = 1; for (int k = 1; k <= n; k++) pow = pow * x; return pow; } // power() | |