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)"; S1public String toString() { S2pages = p; S3title = t; S4private int pages; S5} S6private String title; S7public class Book { S8} S9String title = t; S10public Book(String t, int p) { S11} S12Order: 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)".