All puzzles
Per page
S
Constructor ยท toString()

A book as text

Write the Book class: two instance variables, the constructor, then toString(), which returns the title and the page count.

new Book("Holes", 233).toString() returns "Holes (233 pages)".

12 strips: S1 to S12

return title + " (" + pages + " pages)";
S1
public String toString() {
S2
pages = p;
S3
title = t;
S4
private int pages;
S5
}
S6
private String title;
S7
public class Book {
S8
}
S9
String title = t;
S10
public Book(String t, int p) {
S11
}
S12

Answer key: S. A book as text

Order: S8, S7, S5, S11, S4, S3, S9, S2, S1, S12, S6. Extra: S10

Lines with the same mark can go in either order.

public class Book {
	private String title;
	private int pages;
	public Book(String t, int p) {
		title = t;
		pages = p;
	}
	public String toString() {
		return title + " (" + pages + " pages)";
	}
}

Extra strip: String title = t;. String title = t; makes a new local variable that is gone when the constructor ends. The instance variable stays null, so it returns "null (233 pages)".