// Example for drawing box diagrams class Person { public String name; // name of current Person public Person friend; // friend of current Person is also a Person // constructor public Person(String s) { name=s; } // method for setting value of $friend$: public void set_friend(Person p) { friend = p; } // using $toString$ to print description of current object: public String toString() { return "Name of person: "+name+"; Name of friend: "+friend.name; } } public class drawboxes1 { public static void main(String[] args) { // Create 4 new people: Person a = new Person("Dave"); Person b = new Person("Jeff"); Person c = new Person("Nate"); Person d = new Person("Tony"); // Ira's friend is Jeff: a.set_friend(b); // Description of Ira: System.out.println("Part 1:"); System.out.println(a); // Part 1: DRAW BOX DIAGRAMS FOR THE CODE UP TO THIS LINE. // Creating objects as inputs and return values: System.out.println("Part 2:"); b.set_friend(new Person("Alan")); System.out.println(b); // Part 2: DRAW BOX DIAGRAMS FOR THE CODE UP TO THIS LINE. // What I did was create a new Person object // As input to a method, the new object supplies an address // You don't actually get or need a reference variable // In this case, the dummy argument of set_friend $p$ acts as // the reference variable. // More important: remember that a new object did get created! // Aliases // if you assign a reference to an object to another reference // an object gets obliterated...which one? c = d; // + $c$ gets the address of $d$ // + so, the object that $c$ referred to is gone // + $c$ now refers to the same object that $d$ does // + changes to $c$ and $d$ affect eachother because they mean the // the same object System.out.println("Part 3:"); c.set_friend(b); System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); // Part 3: DRAW BOX DIAGRAMS FOR THE CODE UP TO THIS LINE. } } /* output: Part 1: Name of person: Dave; Name of friend: Jeff Part 2: Name of person: Jeff; Name of friend: Alan Part 3: Name of person: Dave; Name of friend: Jeff Name of person: Jeff; Name of friend: Alan Name of person: Tony; Name of friend: Jeff Name of person: Tony; Name of friend: Jeff */