# Mutation Conceptions These exercises work with the following two classes: ```=java public class Office { String building; int room; String occupant; Office(String building, int room, String occupant) { this.building = building; this.room = room; this.occupant = occupant; } } public class Building { public Office office; public Building(Office office) { this.office = office; } } ``` #### Exercise A What does calling `question1()` return? ```=java public void F1(Office forOffice, String toOccupant) { forOffice = new Office(forOffice.building, forOffice.room, toOccupant); } public String question1() { Office carmenOffice = new Office("CS", 176, "Carmen"); F1(carmenOffice, "Fred"); return carmenOffice.occupant; } ``` **Choices:** - [ ] Carmen - [ ] Fred - [ ] Other: - [ ] I'm not really sure (explained below) #### Exercise B What does calling `question2()` return? ```=java public void F2(Office o1, Office o2) { o2 = o1; o2.room = 63; } public String question2() { Office labOffice = new Office("CS", 130, "Tyler Smith"); Office commonOffice = new Office("CS", 275, "everyone"); F2(labOffice, commonOffice); return labOffice.room + ", " + commonOffice.room; } ``` **Choices:** - [ ] 63, 63 - [ ] 63, 275 - [ ] 130, 63 - [ ] 130, 275 - [ ] Other: - [ ] I'm not really sure #### Exercise C What does calling `question4()` return? ```=java public void F4 (Office o1) { Building bill = new Building(o1); bill.office.room = 999; } public int question4() { Office peterOffice = new Office("CS", 37, "Peter Gibbons"); Building cs = new Building(peterOffice); F4(peterOffice); return cs.building.room; } ``` **Choices** - [ ] 37 - [ ] 999 - [ ] Other: - [ ] I'm not really sure #### Exercise D What does calling `question6()` return? ```=java public int f() { int x = 17; return g(); } public int g() { int y = 4; return x / y; } public int question6() { return f(); } ``` **Choices** - [ ] 0 - [ ] 4 - [ ] Other - [ ] I'm not really sure :::warning Don't go past here until Kathi says so ::: ### Memory Diagram Instructions https://hackmd.io/@csci0200/rJCsizE6K :::warning Don't go past here until Kathi says so ::: # Mutable vs Immutable Lists Consider the following simple interface for lists: ```=java interface IList<T> { public IList addFirst(T newelt); public IList addLast(T newelt); public int length(); } ``` #### Question 1 In your own words, what is the difference between a mutable list and an immutable list? Write no more than 2 sentences. #### Question 2 Assume you’re using a class `MysteryList` that implements `IList`, but you don’t know whether `MysteryList` implements mutable or immutable lists. Finish the following test case so that it will pass only if `MysteryList` implements immutable lists. ```=java public boolean testIsImmutable(Tester t) { MysteryList L = new MysteryList().addFirst(9).addFirst(8).addFirst(7); // optional additional lines (as many as you want) assert(_____________, ______________); } ``` #### Question 3 Now assume you are implementing an immutable list class from a chain of `Node` objects, where the fields are as follows (constructors are omitted for conciseness. ```=java public class ImmutableList<T> implements IList<T> { class Node { T data; Node next; } Node start; // the first node in the chain/front of list Node end; // the last node in the chain/back of list int eltCount; } ``` **Draw the memory diagram** that should exist after running the following line of code (include both environment and heap): ``` IList L2 = new ImmutableList<String>.addFirst(“brown”).addFirst(“go”); ``` Circle the diagram you have drawn so far (this will distinguish your diagram on the previous expression from the additions in the rest of the question). Extend the diagram (outside the circle) to include the expected results of subsequently running the following line of code (we know you don’t have the implementation -- the point of the question is for you to show what `addLast` **should do** on an immutable list). ```=java IList L3 = L2.addLast(“bears”); ```