An extension should start with a user-visible rule, not with a new library. Our planner can report all accepted and completed minutes, so an unfinished-time report is a natural next operation. Add remainingMinutes() to Planner, returning plannedMinutes() minus completedMinutes(). The existing completion invariants ensure the result is nonnegative without storing another changing counter.
Write the evidence first
Decide what remainingMinutes() should return at each stage of one planner's life before writing the method:
| Stage | remainingMinutes() |
|---|---|
| empty planner | 0 |
| after adding Java 25 and Reading 10 | 35 |
| after complete(0) | 10 |
| after complete(0) again | 10 |
| after complete(1) | 0 |
The fourth row is the interesting one: completing a session twice must not subtract its minutes twice.
A compiler failure proves only that the API is missing. Once the method exists, the meaningful evidence is that its returned values match the contract. Keep that distinction clear when you introduce intentional defects.
Keep one source of truth
It is tempting to add a stored remaining field and update it in add and complete. That creates a new obligation: every mutation must keep three totals synchronized, including repeated completion and failed additions. A derived result avoids that extra mutable state. Reuse already-tested methods instead of copying their loops and hoping future changes keep every version aligned.
Do not add persistence, a GUI or a framework for this task. Finish one precise change, rerun the existing tests, then decide what the next useful capability is. Small steps make regressions easier to locate.
public class Main {
public static void main(String[] args) {
System.out.println(remaining(35, 0));
System.out.println(remaining(35, 25));
System.out.println(remaining(35, 35));
}
static int remaining(int planned, int completed) {
return planned - completed;
}
}Check your prediction
35
10
0This works because completed can never exceed planned: in the project, both totals come from the same list of sessions, and a session's minutes count as completed at most once. Don't reuse this bare subtraction for unrelated inputs without stating that precondition.
Your turn
Add remainingMinutes to the planner
This workspace is the real project, with PlannerTest.java as the entry. Run it
once first: it prints All planner checks passed. Then:
- In PlannerTest.java, add the five checks from "Write the evidence first" above, just before the final println, and run. It fails to compile, because remainingMinutes does not exist yet.
- In Planner.java, add
public int remainingMinutes()and run again until all checks pass, old and new.
Hint 1: the idea
The planner can already answer "how much is planned" and "how much is done". The new method needs no new field and no loop: it combines those two answers. The tests need a planner whose state you change between checks, so the same call gives a different answer after each complete().
Hint 2: a first step
In Planner.java, put the new method inside the class, next to
completedMinutes(). In PlannerTest.java, make a fresh Planner extended = new Planner(); and check it returns 0 before you add anything. Then add the 25 and
10 sessions and check 35.
Show one solution
Planner.java, with the new method above report():
import java.util.ArrayList;
public class Planner {
private final ArrayList<Session> sessions = new ArrayList<>();
public void add(String topic, int minutes) {
if (sessions.size() >= 100) {
throw new IllegalStateException("Plan is full");
}
// Construct and validate before changing the list.
Session session = new Session(topic, minutes);
sessions.add(session);
}
public int size() {
return sessions.size();
}
public boolean complete(int index) {
if (index < 0 || index >= sessions.size()) {
return false;
}
sessions.get(index).complete();
return true;
}
public int plannedMinutes() {
int total = 0;
for (Session session : sessions) {
total += session.minutes();
}
return total;
}
public int completedMinutes() {
int total = 0;
for (Session session : sessions) {
if (session.isDone()) {
total += session.minutes();
}
}
return total;
}
public int remainingMinutes() {
return plannedMinutes() - completedMinutes();
}
public String report() {
int planned = plannedMinutes();
int completed = completedMinutes();
return size() + " sessions, " + completed + "/" + planned + " min complete";
}
}Lines added to PlannerTest.java, before the final println:
Planner extended = new Planner();
check("remaining empty", extended.remainingMinutes() == 0);
extended.add("Java", 25);
extended.add("Reading", 10);
check("remaining new", extended.remainingMinutes() == 35);
extended.complete(0);
check("remaining after first", extended.remainingMinutes() == 10);
extended.complete(0);
check("remaining after repeat", extended.remainingMinutes() == 10);
extended.complete(1);
check("remaining all done", extended.remainingMinutes() == 0);All planner checks passedremainingMinutes() calls the two methods the old tests already check, so it inherits their rules: a repeated complete(0) is counted once, which is why the "after repeat" check still expects 10. Each check follows a real complete() call on the same object, so the tests prove the value tracks the planner's state, not just that a subtraction works.
Change it and explain
After implementing the instance method in the project, deliberately return plannedMinutes() without subtracting completion and show which new check fails. Restore it and rerun the old tests too. A new feature should preserve the old contract unless you explicitly decide to change that contract.
For a second extension, consider a method returning the count of unfinished sessions. Define its empty and repeated-completion behavior before coding. Do not expose the internal ArrayList just to make a caller count it; keep the model responsible for its own collection.
Completion is saved in this browser only. It does not measure mastery.