Checkpoint: Session State

The Course
•
4 min read

This checkpoint combines objects, constructors and method contracts. Keep the model focused on one session rather than building a complete planner yet. The normal program and the final state matter as much as the shape of the class.

The brief

Define Session with a fixed duration, a completion flag and two operations: complete() marks it done, while remaining() returns its duration until completion and zero afterward. The fields must be private. The constructor receives the duration; use trusted positive durations for this task. Repeating complete must not toggle the session or count its minutes again.

Create two sessions with durations 25 and 10. Print the first remainder, complete the first twice, then print both remainders. Expected values are 25, 0 and 10. The second object is a deliberate independence check: one mutable static field shared by every object would violate the brief.

Evidence to collect

Check a newly constructed session, one completed session and repeated completion. Use a separate instance to catch shared state. Read access modifiers rather than just observing output: a public duration field would permit unwanted writes even if these three outputs happened to match. Later validation will add another boundary; do not pretend positive inputs have been validated automatically.

A different model with independent state Java
public class Main {
    public static void main(String[] args) {
        Flag first = new Flag();
        Flag second = new Flag();
        first.enable();

        System.out.println(first.isEnabled());
        System.out.println(second.isEnabled());
    }
}

class Flag {
    private boolean enabled;

    public void enable() {
        enabled = true;
    }

    public boolean isEnabled() {
        return enabled;
    }
}
Check your prediction
true
false

The flag provides the repeated-assignment pattern, but does not have a duration. You must add that fixed data and the remaining-time rule for the checkpoint.

An alias still observes the same object Java
public class Main {
    public static void main(String[] args) {
        Flag first = new Flag();
        Flag alias = first;
        alias.enable();

        System.out.println(first.isEnabled());
    }
}

class Flag {
    private boolean enabled;

    public void enable() {
        enabled = true;
    }

    public boolean isEnabled() {
        return enabled;
    }
}
Check your prediction
true

A second variable is not an independent-instance test unless it refers to another new object. Use two new expressions when the requirement is independent state.

Your turn

Exercise

Implement Session

Write the private fields, constructor and public operations described above. Keep Main and Session in the workspace as two top-level classes for this answer. In the final project, the same collaboration will use separate named files.

Your workspace Java
public class Main {
    public static void main(String[] args) {
        // Exercise two separate sessions.
    }
}

class Session {
    // Define private state and public methods.
}
Hint 1: the idea

Separate fixed duration from changing completion. remaining reads the state without changing it.

Hint 2: a first step

Use an instance boolean initially false. complete assigns true rather than negating the flag.

Show one solution
public class Main {
    public static void main(String[] args) {
        Session first = new Session(25);
        Session second = new Session(10);

        System.out.println(first.remaining());
        first.complete();
        first.complete();

        System.out.println(first.remaining());
        System.out.println(second.remaining());
    }
}

class Session {
    private final int minutes;
    private boolean done;

    Session(int minutes) {
        this.minutes = minutes;
    }

    public void complete() {
        done = true;
    }

    public int remaining() {
        if (done) {
            return 0;
        }

        return minutes;
    }
}
25
0
10

Change it and explain

Deliberately change complete to done = !done and identify the failing repeated completion case. Then make the flag static as an experiment: which independence check fails? Restore both rules. Defects that preserve the first output may still break later operations.

Write down the public operations without their bodies. Could another person use this class from that description alone? A clear model names what callers can do and what is guaranteed afterward; its private implementation can then change without asking every caller to rewrite itself.

Was this article helpful?

Your response is saved on this device.