Homework: Tracing With References
Answer the following questions. For the tracing exercises, you should be able to complete the tracing tables on this page and then print the page from the browser.
-
Consult the contract for
Standardinterface's methodtransferFromspecified here and then complete the tracing table from Slides 78-79 from References reproduced here wheremandkare variables of typeNaturalNumber.Statement Variable Values m → 143
k → 70m.transferFrom(k);
-
Explain why an immutable type cannot have a
transferFrommethod as specified in theStandardinterface. -
Complete the following tracing tables. Each of them has a short method declaration followed by short client code that invokes the method. Carefully complete each tracing table starting from the client code and tracing over the method call and through the method body.
-
Swapping for primitive types:
Statement Variable Values private static void swap1(int i1, int i2) {
int tmp = i1;
i1 = i2;
i2 = tmp;
} Start tracing here int x = 7, y = 12;
swap1(x, y);
-
Swapping for (immutable) reference types:
Statement Variable Values private static void swap2(String s1, String s2) {
String tmp = s1;
s1 = s2;
s2 = tmp;
} Start tracing here String x = "legends", y = "leaders";
swap2(x, y);
-
Swapping for (mutable) reference types:
Statement Variable Values private static void swap3(NaturalNumber n1, NaturalNumber n2) {
NaturalNumber tmp = n1;
n1 = n2;
n2 = tmp;
} Start tracing here NaturalNumber x = new NaturalNumber2(41), y = new NaturalNumber2(78);
swap3(x, y);
-