This checkpoint combines objects, constructors and method contracts in a model you have not seen yet. Plan the fields and methods before writing code. The normal program and the final state matter as much as the shape of the class.
The brief
Define a Goal class for a weekly study goal:
Goal(String topic, int target)stores a topic and a target in minutes (trusted positive values for this task);public boolean log(int minutes)adds study time and returns true. It refuses a value of 0 or less: it returns false and changes nothing;public int left()returns the minutes still needed, never below 0;public String summary()returns text such asJava: 30/40 min.
All fields are private. Main must use only the four operations above.
Evidence to collect
The two examples below are smaller models with the same building blocks. Use them to predict, not to copy: neither has a target, a refused update or a "never below zero" rule.
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
falseThe flag shows private state that belongs to each object. Goal adds fixed data from a constructor and a rule that can refuse a change.
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
trueA 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.
Plan your acceptance cases
Predict the value for each row before you write the class.
| Case | Expected | Skill it checks |
|---|---|---|
new Goal("Java", 40), then left() | 40 | Constructor stores the target |
log(25), then log(-5) | true, then false | A refused update returns false |
after those two calls, summary() | Java: 25/40 min | The refused update changed nothing |
log(25) again, then left() | 0, not −10 | The "never below zero" rule |
a second new Goal("Reading", 20), summary() | Reading: 0/20 min | Each object has its own fields |
When your version works, break it on purpose: delete the check in log that
refuses 0 or less, so every call adds its minutes and returns true. Which row
fails first, and what does summary() print after log(-5)? Put the check back.
Your turn
Implement Goal
Write Goal from the brief, then make main print exactly:
true
false
Java: 25/40 min
true
0
Java: 50/40 min
Reading: 0/20 minpublic class Main {
public static void main(String[] args) {
// Create a Java goal of 40 and a Reading goal of 20.
// Log 25, then -5, on the Java goal and print each result.
// Print its summary, log 25 again, then print left() and both summaries.
}
}
class Goal {
// Define private fields, the constructor and four public methods.
}Hint 1: the idea
Decide which values are fixed (topic, target) and which one changes (minutes logged so far). log() is the only method that changes state, and it must check its rule before changing anything. left() only reads.
Hint 2: a first step
Start with three private fields and a constructor that stores topic and target; progress starts at 0. Write summary() next and print it once, so you can see the state as you add log() and left().
Show one solution
public class Main {
public static void main(String[] args) {
Goal javaGoal = new Goal("Java", 40);
Goal readingGoal = new Goal("Reading", 20);
System.out.println(javaGoal.log(25));
System.out.println(javaGoal.log(-5));
System.out.println(javaGoal.summary());
System.out.println(javaGoal.log(25));
System.out.println(javaGoal.left());
System.out.println(javaGoal.summary());
System.out.println(readingGoal.summary());
}
}
class Goal {
private final String topic;
private final int target;
private int progress;
Goal(String topic, int target) {
this.topic = topic;
this.target = target;
}
public boolean log(int minutes) {
if (minutes <= 0) {
return false;
}
progress += minutes;
return true;
}
public int left() {
if (progress >= target) {
return 0;
}
return target - progress;
}
public String summary() {
return topic + ": " + progress + "/" + target + " min";
}
}true
false
Java: 25/40 min
true
0
Java: 50/40 min
Reading: 0/20 minTwo lines carry the rules. In log(), if (minutes <= 0) returns before
progress += minutes, so the refused −5 leaves the summary at 25/40. In left(),
the progress >= target branch gives 0 instead of −10. Progress itself may pass
the target (50/40): the brief limits what left() reports, not how much you may
study. readingGoal still shows 0/20 because progress is an instance field.
Change it and explain
Deliberately move progress += minutes; above the if in log(), and find the
first printed line that changes. Then remove the branch from left():
which line changes? Restore both. 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.
Completion is saved in this browser only. It does not measure mastery.