1 For the following program segment fill in the tracing tabl
1. For the following program segment, fill in the tracing table below as described in the course notes. You should make a duplicate of this table in your homework submission.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int x = 15, y=30, z=10, a=0;
if ( x > y ) {
if (x > z) {
a = x;
}
else {
a = z;
}
}
else {
if (y > z) {
a = y;
}
else {
a = z;
}
}
System.out.println(\"The value is: \"+a);
2.For the following program segment, fill in the trace table below with values as described in the course notes. You should make a duplicate of this table in your homework submission. In addition, write a single sentence in English summarizing what this code segment does:
1
2
3
4
5
6
7
8
int n = 9431;
String output=\"\";
while (n > 0) {
int x = n % 10;
output = output + x;
n = n / 10;
}
System.out.println(\"Result is: \"+output);
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | int x = 15, y=30, z=10, a=0; if ( x > y ) { if (x > z) { a = x; } else { a = z; } } else { if (y > z) { a = y; } else { a = z; } } System.out.println(\"The value is: \"+a); |
Solution
According to this code:
if (y > z) {
a = y;
}
is the condition is which is correct
basically what this program does is reverse the string.
in while loop
x will be assigned with last value of the string.
output will concate the x value
n will exclude the last value of its value
| Statement | Program State |
| int x = 15, y = 30, z = 10, a = 0; | |
| x = 15 y = 30 z = 10 a = 0 | |
| if ( x > y ) { | |
| x = 15 y = 30 z = 10 a = 0 | |
| if (x > z) { | |
| x = 15 y = 30 z = 10 a = 0 | |
| a = x; | |
| x = 15 y = 30 z = 10 a = 0 | |
| } else { | |
| x = 15 y = 30 z = 10 a = 0 | |
| a = z; | |
x = 15 y = 30 z = 10 a = 0 | |
| } } else { | |
| x = 15 y = 30 z = 10 a = 0 | |
| if (y > z) { | |
| x = 15 y = 30 z = 10 a = 0 | |
| a = y; | |
| x = 15 y = 30 z = 10 a = 30 | |
| } else { | |
| x = 15 y = 30 z = 10 a = 30 | |
| a = z; | |
| x = 15 y = 30 z = 10 a = 30 | |
| } } | |
| x = 15 y = 30 z = 10 a = 30 | |
| System.out.println(\"The value is: \" + a); | |
| x = 15 y = 30 z = 10 a = 30 |



