CS100, Spring 2000, Solutions to Review Question 7 for Prelim 2 7. We have done a little something extra in these sample solutions. First, we have numbered the statements and the corresponding values, e.g. arrows. You are *allowed* but not *required* to do so. TIP: Do it if you find it useful, e.g. it helps you keep track of what is happening; otherwise, do not do it. Second, we show intermediate stages of the box diagram to give you a feel for the steps as they happen over time. class Frippy { | public class Problem7 { int x; | public static void main(String[] args) { [0] Frippy F; | Frippy F1 = new Frippy(); [1] } | Frippy F2 = new Frippy(); [2] | Boingo B1 = new Boingo(); [3] class Boingo { | Boingo B2 = new Boingo(); [4] String a; | Frippy b; | B2.a = "grok"; [5] } | B1.b = new Frippy(); [6] | F2.F = B1.b; [7] | | B1.b.F = F1; [8] | ++B1.b.F.x; [9] | | Boingo B3; [10] | F2 = null; [11] | // *** diagram up to this point *** [12] | } | } [0] parameter $args$ is like a local variable; it is a reference, but in CS100 we don't care what it refers to. creates a new Frippy object, including giving the instance / variables their default values [1] $new Frippy()$ \ returns a reference to the new object. $F1 = new Frippy()$ the value (reference) is stored in F1 [2], [3], [4] are like [1] ==== [5] the old value $null$ is crossed out. the new value (reference to "grok") is placed into $a$. [6] $new$ creates a new object. the reference to this object is stored in $b$. the reference is a "new" value, so the old value $null$ is crossed out. [7] the old value $null$ is crossed out. the new value (reference to a Frippy object) is drawn. ===== [8] old value $null$ is ossed out; new value drawn in [9] old value 0 is crossed out; new value 1 is drawn nearby [10] local variables are *not* given default values; thus, the value is undetermined and indicated by $?$. [11] the old value (reference) is crossed out by drawing a tidy X over the arrow; the new value $null$ is drawn in. [12] cross out all unused object boxes.