CSE 331: Software Design and Implementation, Autumn 2026
Your job is to deliver code that you know works.
delete decides (block A)308public void delete(String asker, int index)
309 throws NotOwnerException {
310 if (!asker.equals(owner)) {
311 throw new NotOwnerException(
312 asker + " does not own this book");
313 }
314 messages.remove(index);delete need from its caller? Who is askingjavac hold the caller to? A String: any will dojavac accept this call? (block A)124try {
125 book.delete(owner, index);
126} catch (NotOwnerException e) {308public void delete(String asker, int index)owner: the book the request namedjavac accepts it, and bob deletes125book.delete(owner, index);310if (!asker.equals(owner)) {owner is adadelete's asker is ada, not bobUser, a type of its own209class User {
210 private final String login;
211 // RI: login is not null, and not empty.
212 // AF(this) = the user whose login is login.
213
214 private User(String login) {
215 this.login = login;
216 }User can make a Userjavac accept line 125 now? (block C)125book.delete(owner, index);308public void delete(User asker, int index)
309 throws NotOwnerException {$ javac Guestbook.java Guestbook.java:125: error: incompatible types: String cannot be converted to User book.delete(owner, index); ^ ⋮ 1 error
125book.delete(new User(owner), index);User. Will javac accept it?$ javac Guestbook.java Guestbook.java:125: error: User(String) has private access in User book.delete(new User(owner), index); ^ 1 error
User, no code can turn a String into a UserUser?223public static Optional<User> from(HttpExchange exchange) {
224 String login = exchange.getRequestHeaders()
225 .getFirst("Tailscale-User-Login");
226 if (login == null || login.isEmpty()) {
227 return Optional.empty();
228 }
229 return Optional.of(new User(login));
230}from is static, so it is called on the class: User.from(exchange)User61Optional<User> signedIn = User.from(exchange);
67User asker = signedIn.get();125book.delete(asker, index);User.from for the user who sent this requestUser is what it hands to delete, and javac accepts itUser in this program came from a request,which lines do you have to read? Why are those enough?User came from a requestUser, lines 209 to 250, and nothing outside itUser can call itnew User: line 229, inside fromfrom makes that User from the request's loginjavac checks that every delete is handed a Userdelete does with itYour job is to deliver code that you know works.
javac checks every call, at every compileDefinition: design pattern
A design pattern is a solution to a design problem that comes up again and again, written down so that others can reuse it. It has four parts: a name, the problem it solves, the solution, and the consequences of using it, both what it gains and what it costs.
User.from is a static factory methodThe four parts: Gamma, Helm, Johnson, and Vlissides, Design Patterns (1995), pages 2 and 3.
8public interface HelpQueue {
10 int size();
18 void join(String netid);
26 String next();
29 List<String> waiting();
30}ListQueue, LinkedQueue, FastQueue, FasterQueueOn the screen only.
21public FastQueue() {
22 queue = new ArrayList<String>();
23 members = new HashSet<String>();
24 checkRep();
25}
26/** @requires no NetID is in netids twice
27 * @effects makes a queue of the students given, front first */
28public FastQueue(List<String> netids) {new FastQueue(netids) does not say "no NetID twice". A name couldFastQueue: only their arguments tell them apartOn the screen only.
new do here? (screen only)6HelpQueue queue = new FastQueue(List.of("ada", "ada"));$ java BadList Exception in thread "main" java.lang.IllegalStateException: someone is in queue twice at FastQueue.checkRep(FastQueue.java:41) at FastQueue.<init>(FastQueue.java:33) at BadList.main(BadList.java:6)
from returned an empty Optional for no login: no constructor canUser.from is oneThe name, problem, gains, and costs: Bloch, Effective Java, 3rd edition (2018), Item 1.
4ListQueue queue = new ListQueue();
5// Students join at the back.
6queue.join("ada");
7queue.join("bob");OfficeHours calls only HelpQueue's methodsListQueue, as each client that makes a queue doesFastQueue, we edit each of those linesLecture 10's OfficeHours.java, before today. Not on the sheet.
2public class HelpQueues {
3 /** @return a new help queue, with no students waiting */
4 public static HelpQueue empty() {
5 return new ListQueue();5 return new LinkedQueue();
6 }
7}4HelpQueue queue = HelpQueues.empty();HelpQueues.empty()javac checks nothing new hereHelpQueues.java changes to return new LinkedQueue().Without rereading OfficeHours.java,what can you be sure of about OfficeHours.java? What can you not be sure of?javac can tell us, and what it cannotOfficeHours still compilesjavac checks that LinkedQueue is a HelpQueueOfficeHours calls only HelpQueue's methodsOfficeHours behaves the sameLinkedQueue to satisfy HelpQueue's specificationjavac does not check thatemptyQueue is a Factory Method9public abstract class HelpQueueTest {
10 // A subclass says which implementation to test.
11 abstract HelpQueue emptyQueue();1public class ListQueueSpecTest extends HelpQueueTest {
2 HelpQueue emptyQueue() {
3 return new ListQueue();
4 }
5}Gamma, Helm, Johnson, and Vlissides, Design Patterns (1995), page 107. This code is on the screen only.
List.of("ada", "bob")Optional.of(user) and Optional.empty()Path.of("messages.txt")new at the call6door.arrive("ada");
7door.arrive("bob");
8ta.help();13class Door {
14 private HelpQueue queue = new ListQueue();
22class Ta {
23 private HelpQueue queue = new ListQueue();nobody to help: the door and the TA each made a queueOfficeQueue, a singleton (block G)10private static final OfficeQueue THE_QUEUE = new OfficeQueue();
14private OfficeQueue() {
15}
18public static OfficeQueue theQueue() {
19 return THE_QUEUE;
20}OfficeQueue: line 10 makes ittheQueue hands out the one14private HelpQueue queue = new ListQueue();14private HelpQueue queue = OfficeQueue.theQueue();
23private HelpQueue queue = new ListQueue();23private HelpQueue queue = OfficeQueue.theQueue();helping adajavac accept a second queue?new OfficeQueue() anyway$ javac Session.java Session.java:14: error: OfficeQueue() has private access in OfficeQueue private HelpQueue queue = new OfficeQueue(); ^ 1 error
OfficeQueuejavac checks that no client makes a second, at every compileGamma, Helm, Johnson, and Vlissides, Design Patterns (1995), page 129: the client "will get an error at compile-time".
The testing cost: Bloch, Effective Java, 3rd edition (2018), Item 3.
4System.out.println(Boolean.valueOf(true) == Boolean.valueOf(true));
5System.out.println(Integer.valueOf(100) == Integer.valueOf(100));
6System.out.println("hello" == new String("hello"));
7System.out.println("hello" == new String("hello").intern());true or false. Which?$ java Sharing true true false true
equalsjavac checks who can make the object, if the constructor is privatejavac checks nothing newjavac checks that no client makes a secondnew makes an object even when an equal one existsjavac checks nothingReusing an object already made: Bloch, Effective Java, 3rd edition (2018), Item 1.