Skip to content

On Your Own: Reading Tracker

The Course
•
20 min study

Close the planner solution. This independent brief uses a different domain so you can check whether the ideas transfer. Spend time writing a contract and acceptance table before opening hints. It is fine to revisit a particular earlier lesson, but try not to copy and rename the final project wholesale.

The brief

Model a Book with a trimmed nonblank title, a fixed total page count and a changing number of pages read. The constructor accepts title and pages; pages must be nonnegative. Start with zero read. Throw IllegalArgumentException for a null or blank title or a negative page count. A zero-page book is allowed and has zero remaining pages immediately.

Provide readPages(int amount) returning boolean. Accept a nonnegative amount only when it fits in the unread remainder; otherwise return false without changing state. Provide summary() returning title followed by the remaining-page count. Repeated reads are genuine additional work, unlike the planner's complete action. Decide which state changes and explain this difference.

Acceptance plan

CaseExpected
new Book(" Java ", 100), then readPages(25)true; summary Java: 75 pages left
then readPages(-1) and readPages(80)both false; still 75 left
then readPages(75)true; 0 left
then readPages(0)true; still 0 left
a second new Book("Reading", 10)Reading: 10 pages left throughout
new Book(null, 10), new Book(" ", 10), new Book("Bad", -1)each throws IllegalArgumentException
new Book("Empty", 0)allowed; readPages(0) true, readPages(1) false

Write each row as a labelled check, using the check helper and the expectInvalid pattern from the Testing lesson for the constructor rows.

For a multi-file version, use Main.java for your checks and Book.java for the model, with the class matching its filename. The two-tab workspace below supplies those filenames and deliberately incomplete methods. Implement the model in Book.java and add checks in Main.java. You can also download this starter for local work; all core practice still works in the browser.

A minimal transfer example: fixed capacity and accepted changes Java
public class Main {
    public static void main(String[] args) {
        int capacity = 100;
        int used = 25;
        int request = 80;
        boolean fits = request >= 0 && request <= capacity - used;

        System.out.println(fits);
        System.out.println(capacity - used);
    }
}
Check your prediction
Text
false
75

The fit test uses the remaining capacity before mutation. It does not add request to used first. Testing against capacity-used also avoids overflow from adding two potentially large positive ints.

Your turn

Exercise

Build and check Book

Implement the brief. Start with the three prints already in Main.java (true, false, Java: 75 pages left). When those work, turn every row of the acceptance table into a labelled check in Main.java, and finish with a line such as Book checks passed that prints only if every check passed.

Your workspace: Main.java and Book.java Java
public class Main { public static void main(String[] args) { Book book = new Book(" Java ", 100); System.out.println(book.readPages(25)); System.out.println(book.readPages(80)); System.out.println(book.summary()); // Add independent checks for the other acceptance cases here. } } public class Book { // Add the private fixed values and changing progress here. public Book(String title, int pages) { // Validate and store the supplied values. } public boolean readPages(int amount) { // Reject an invalid update before changing progress. return false; } public String summary() { // Return a description based on the current state. return ""; } }
Hint 1: the idea

Keep fixed values final and the progress value mutable. Validate a request before adding it.

Hint 2: a first step

Use pages-read as the available remainder. If a request is outside its allowed range, return false before assigning to read. For the constructor rows, copy the shape of expectInvalid from Testing: a boolean that only the catch sets to true.

Show one solution

This reference is shown as one Main.java file so you can read it together. In the two-tab workspace, put the Book fields and method bodies in Book.java, keeping its public class Book declaration. Put only the Main code in Main.java. Do not paste both class declarations into both tabs.

Java
public class Main {
    public static void main(String[] args) {
        Book book = new Book(" Java ", 100);
        Book other = new Book("Reading", 10);

        System.out.println(book.readPages(25));
        System.out.println(book.readPages(80));
        System.out.println(book.summary());

        check("negative rejected", !book.readPages(-1));
        check("rejections change nothing", book.summary().equals("Java: 75 pages left"));
        check("exact remainder", book.readPages(75));
        check("finished", book.summary().equals("Java: 0 pages left"));
        check("zero read accepted", book.readPages(0));
        check("zero read changes nothing", book.summary().equals("Java: 0 pages left"));
        check("independent", other.summary().equals("Reading: 10 pages left"));

        expectInvalid(null, 10);
        expectInvalid(" ", 10);
        expectInvalid("Bad", -1);

        Book empty = new Book("Empty", 0);
        check("empty book reads 0", empty.readPages(0));
        check("empty book rejects 1", !empty.readPages(1));

        System.out.println("Book checks passed");
    }

    static void expectInvalid(String title, int pages) {
        boolean rejected = false;

        try {
            new Book(title, pages);
        } catch (IllegalArgumentException error) {
            rejected = true;
        }

        check("rejects " + title + "/" + pages, rejected);
    }

    static void check(String label, boolean condition) {
        if (!condition) {
            throw new AssertionError(label);
        }
    }
}

class Book {
    private final String title;
    private final int pages;
    private int read;

    Book(String title, int pages) {
        if (title == null || title.trim().isEmpty()) {
            throw new IllegalArgumentException("Title is required");
        }

        if (pages < 0) {
            throw new IllegalArgumentException("Pages must be nonnegative");
        }

        this.title = title.trim();
        this.pages = pages;
    }

    public boolean readPages(int amount) {
        if (amount < 0 || amount > pages - read) {
            return false;
        }

        read += amount;
        return true;
    }

    public String summary() {
        return title + ": " + (pages - read) + " pages left";
    }
}
Text
true
false
Java: 75 pages left
Book checks passed

The deciding line is amount > pages - read in readPages: it compares the request with what is left before read changes, so a refused read leaves the summary at 75. The constructor validates before assigning any field, so a bad book is never created at all; that is why those rows need expectInvalid rather than a check on summary(). Repeated reads add up, unlike the planner's complete(), because reading more pages is new progress.

Change it and explain

Turn the acceptance plan into a test program with labeled checks. For a multi-file version, verify the same Book from a separate Main entry. Add one deliberate mutation—accept requests above the remainder—and make sure a check detects it.

Explain why a setter such as setRead(anyNumber) is not part of this model's API. The public operation expresses progress and preserves a bound. If you needed corrections or undo later, design that behavior explicitly rather than quietly allowing every caller to bypass the current rule.

Was this article helpful?

Your response is saved on this device.