| N | N^2 | N^3 |
| 1 | 1 | 1 |
| 2 | 4 | 8 |
| 3 | 9 | 27 |
| 4 | 16 | 64 |
| 5 | 25 | 125 |
output.setText("");
String header = " N " + "N^2" + "N^3";
output.append(header + "\n\n");
for ( int N = 1; N <= 5; N++){
output.append(" " + N + " " + Format.justify('c',N*N,3) +
Format.justify('c', N*N*N, 3);
}
| @@@@@
$$$$$ &&&&& |
Only indentation conventions need to be applied.
The code produces this output as it is given. Here is properly indented code: if ( y == 8 ) if ( x == 5 ) System.out.println( "@@@@@" ); else System.out.println( "#####" ); System.out.println( "$$$$$" ); System.out.println( "&&&&&" ); |
| @@@@@ | Here, we can just group a large
collection of code in the else clause: if ( y == 8 ) if ( x == 5 ) System.out.println( "@@@@@" ); else { System.out.println( "#####" ); System.out.println( "$$$$$" ); System.out.println( "&&&&&" ); } |
| @@@@@
&&&&& |
Here, we can just group a medium
collection of code in the else clause: if ( y == 8 ) if ( x == 5 ) System.out.println( "@@@@@" ); else { System.out.println( "#####" ); System.out.println( "$$$$$" ); } System.out.println( "&&&&&" ); |
| #####
$$$$$ &&&&& |
We want the else to match the first if, so
use braces to make this so:
if ( y == 8 ){ if ( x == 5 ) System.out.println( "@@@@@" ); } else { System.out.println( "#####" ); System.out.println( "$$$$$" ); System.out.println( "&&&&&" ); } |
For the decrypt method, we reverse the steps:
public int decrypt( int codedNumber) {
//test the input for four digits
if (codedNumber / 10000 > 0)
return -1;
//extract the digits and unswap
them
int digit1,digit2,digit3,digit4;
digit3 = codedNumber % 10;
codedNumber /= 10;
digit4 = codedNumber % 10;
codedNumber /= 10;
digit1 = codedNumber % 10;
digit2 = codedNumber / 10;
//subtract the 7 from each digit,
but make sure the result is positive
digit1 = (digit1 - 7 + 10) %
10;
digit2 = (digit2 - 7 + 10) %
10;
digit3 = (digit3 - 7 + 10) %
10;
digit4 = (digit4 - 7 + 10) %
10;
//rebuild the regular number.
return (digit4*1000 + digit3*100
+ digit2*10 + digit1);
}
public static void buttonClicked(Button
myButton) {
int n = 4;
int x = 6;
while (n > 0) {
switch (x) {
case 1:
n *= 3;
break;
case 3:
n -= 6;
break;
default:
n++;
break;
}//end switch
x = methodA(n);
results.append("x= " + x + " and n = " + n);
}// end while
} // end buttonClicked
} // end TraceMe
Originally, n = 4 and x = 6, when we enter the
while loop. Since n = 4 > 0, we do the body of the while loop. A switch
on 6, causes the default case to execute, so n is incremented to 5 and
we break from the switch statement. Next, x = methodA(5) = 7 (since the
if test is false, we return 5 + 2). We'll print "x = 7 and n = 5" in the
TextArea and go back to the while loop test. Since n = 5 > 0, we switch
on x = 7, which sets n = 6 by the default case. Then x = methodA(6) = 2,
and we append "x = 2 and n = 6" to the TextArea. Back to the while test,
which is true, so we do the while loop body and switch on 2, yielding the
default case again, so n = 7, and x = methodA(7) = 3, so "x = 3 and n =
7" is appended. A switch on 3, causes case 3 to execute, so n becomes 1.
Then, x = methodA(1) = 3, so "x = 3 and n = 1" is appended. Since 1 > 0,
we switch on 3 and set n = n - 6 = -5. Now, x = methodA(-5) = -3, and "x
= -3 and n = -5" is appended to the TextArea. Finally, the while test fails,
and the buttonClicked method is over.
what are the final values of the variables liquid and yolk,
after the following lines of code are executed?
int liquid, yolk = 5;
liquid = scramble(yolk);