1 2 3 4 5 6 7 8 9 10 11 12 13 14 String theBardAlas poor Yor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
String theBard=\"Alas poor Yorick\";
String theSubBard=theBard.substring(5,9);
String theCliche=\"A poor workman blames his tools\";
String theSubCliche=theCliche.substring(2,7);
String output=\"The result is: \";
if (theSubBard.equals(theSubCliche)) {
output = output + \" a cliched phrase!\";
}
else {
output = output + \" a work of genius!\";
}
System.out.println(output);
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
String str = \"Hello World!\";
int test = 6;
String output = \"\";
if (test >= str.length()) {
output = \"Bigger!\";
}
else {
output = \"Smaller!\";
}
For the following program segment, give the output produced by the segment:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | String theBard=\"Alas poor Yorick\"; String theSubBard=theBard.substring(5,9); String theCliche=\"A poor workman blames his tools\"; String theSubCliche=theCliche.substring(2,7); String output=\"The result is: \"; if (theSubBard.equals(theSubCliche)) { output = output + \" a cliched phrase!\"; } else { output = output + \" a work of genius!\"; } System.out.println(output); |
Solution
For first four parts:
str = \"Hello World\"
test = 6
output = \"\"
as test is less than str.length. Therefore, value fo these variables don\'t change.
For last two parts, output will be:
str = \"Hello World\"
test = 6
output = \"Smaller!\"
as else part will be executed that cause a change to the value of output.


