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. =============================================================================== Steps [0] through [4] =============================================================================== class Problem7 +-------------------------------------------------------------------+ | +---+ +---+ +---+ +---+ +---+ +---+ | | args | *-+-+ F1 | * | F2 | * | B1 | * | B2 | * | B3 | | | | +---+ | +-+-+ +-+-+ +-+-+ +-+-+ +---+ | +------------+------+----------+----------+----------+--------------+ | | | | | <------+ | [1] | [2] [3] +--+ [4] +------+ class Frippy | | | | +-------------------+----------+-------------+--------------+--------------+ | V | | | | | +----------+ +----------+ >+----------+ | +----------+ | +----------+ | | | +----+ | | +----+ | | +----+ | | | +----+ | | | +----+ | | | | x | | | | x |0 | | | x |0 | | | | x | | | | | x | | | | | | +----+ | | +----+ | | +----+ | | | +----+ | | | +----+ | | | | | | | | | | | | | | | | | | +----+ | | +----+ | | +----+ | | | +----+ | | | +----+ | | | | F | | | | F |null| | | F |null| | | | F | | | | | F | | | | | | +----+ | | +----+ | | +----+ | | | +----+ | | | +----+ | | | +----------+ +----------+ +----------+ | +----------+ | +----------+ | | | | | +--------------------------------------------+--------------+--------------+ | | class Boingo | | +--------------------------------------------+--------------+--------------+ | | | | | +----------+ +----------+ +----------+ +>+----------+ +>+----------+ | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | | a | | | | a | | | | a | | | | a |null| | | a |null| | | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | | | | | | | | | | | | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | | b | | | | b | | | | b | | | | b |null| | | b |null| | | | | +----+ | | +----+ | | +----+ | | +----+ | | +----+ | | | +----------+ +----------+ +----------+ +----------+ +----------+ | | | +--------------------------------------------------------------------------+ class String +----------------------+ | | | "grok" | | | +----------------------+ =============================================================================== Steps [5] through [7] (atop [0] through [4] =============================================================================== class Problem7 +-------------------------------------------------------------------+ | +---+ +---+ +---+ +---+ +---+ +---+ | | args | *-+-+ F1 | * | F2 | * | B1 | * | B2 | * | B3 | | | | +---+ | +-+-+ +-+-+ +-+-+ +-+-+ +---+ | +------------+------+----------+----------+----------+--------------+ | | | | | <------+ | [1] | [2] [3] +--+ [4] +------+ class Frippy | | | | +-------------------+----------+-------------+--------------+--------------+ | V | | | | | +----------+ +----------+ >+----------+ | +----------+ | +----------+ | | | +----+ | | +----+ | | +----+ | | | +----+ | | | +----+ | | | | x | | | | x |0 | | | x |0 | | | | x |0 | | | | x | | | | | | +----+ | | +----+ | | +----+ | | | +----+ | | | +----+ | | | | | | | | [7] | | | | | | | | | | +----+ | | +----+ | | +-\-/+ | | | +----+ | | | +----+ | | | | F | | | | F |null| | | F |nuXl*-+-+>| F |null| | | | F | | | | | | +----+ | | +----+ | | +-/-\+ | | | +----+ | | | +----+ | | | +----------+ +----------+ +----------+ | +----------+ | +----------+ | | | ^ | | +--------------------------------------------+--------+-----+--------------+ | | | | [6] | | class Boingo | | | +--------------------------------------------+--------|-----+--------------+ | | | | | | +----------+ +----------+ +----------+ +>+------|---+ +>+----------+ | | | +----+ | | +----+ | | +----+ | | +--|-+ | | +-\-/+ | | | | a | | | | a | | | | a | | | | a |nu|l| | | a |nuXl*-+-+-+ | | +----+ | | +----+ | | +----+ | | +--|-+ | | +-/-\+ | | | | | | | | | | | | | | | | | | | +----+ | | +----+ | | +----+ | | +-\*/+ | | +----+ | | | | | b | | | | b | | | | b | | | | b |nuXl| | | b |null| | | | | | +----+ | | +----+ | | +----+ | | +-/-\+ | | +----+ | | | | +----------+ +----------+ +----------+ +----------+ +----------+ | | | | | +--------------------------------------------------------------------------+ | | class String | +----------------------+ | | | [5] | | "grok"<--------------+-----------------------------------------------------+ | | +----------------------+ =============================================================================== Steps [8] through [12] (atop [0] through [7]) =============================================================================== class Problem7 +--------------------------------------------------------------[10]-+ | +---+ +---+ +----+ +---+ +---+ +---+ | | args | *-+-+ F1 | * | F2 |null| B1 | * | B2 | * | B3 | ? | | | +---+ | +-+-+ +-+--+ +-+-+ +-+-+ +---+ | +------------+------+----------+----------+----------+--------------+ | | | | | <----+ | [1] [11] X [2] [3] +--+ [4] +------+ class Frippy | | | | +-------------------+----------+-------------+--------------+--------------+ | [12] V | | | [12] | | +--\-------/ +----------+ >+----------+ | +----------+ | +--\-------/ | | | \----+/| | \-/--+ | | +----+ | | | +----+ | | | \----+/| | | | x |\ / | | x |0[9]|1| | x |0 | | | | x |0 | | | | x |\ / | | | | +-\-/+ | | /-\--+ | | +----+ | | | +----+ | | | +-\-/+ | | | | X | | | | [7] | | | | | | X | | | | +-/-\+ | | +----+ | | +-\-/+ | | | +-\-/+ | | | +-/-\+ | | | | F |/ \ | | F |null| | | F |nuXl*-+-+>| F |nuXl| | | | F |/ \ | | | | /----+\| | +----+ | | +-/-\+ | | | +*/-\+ | | | /----+\| | | +--/-------\ +----------+ +----------+ | +----+-----+ | +--/-------\ | | [8] ^----------------+------+ ^ | | +--------------------------------------------+--------+-----+--------------+ | | | | [6] | | class Boingo | | | +--------------------------------------------+--------|-----+--------------+ | [12] [12] [12] | | | | | +--\-------/ +--\-------/ +--\-------/ +>+------|---+ +>+----------+ | | | \----+/| | \----+/| | \----+/| | +--|-+ | | +-\-/+ | | | | a |\ / | | a |\ / | | a |\ / | | a |nu|l| | | a |nuXl*-+-+-+ | | +-\-/+ | | +-\-/+ | | +-\-/+ | | +--|-+ | | +-/-\+ | | | | | X | | X | | X | | | | | | | | | | +-/-\+ | | +-/-\+ | | +-/-\+ | | +-\*/+ | | +----+ | | | | | b |/ \ | | b |/ \ | | b |/ \ | | b |nuXl| | | b |null| | | | | | /----+\| | /----+\| | /----+\| | +-/-\+ | | +----+ | | | | +--/-------\ +--/-------\ +--/-------\ +----------+ +----------+ | | | | | +--------------------------------------------------------------------------+ | | class String | +----------------------+ | | | [5] | | "grok"<--------------+-----------------------------------------------------+ | | +----------------------+