The complete course works in the browser. Local work is optional, but it gives you files you can keep, a real JDK and a path toward tools outside the playground's supported subset. The browser implements part of Java 25; a full JDK includes more APIs. This lesson uses the same source, not a different local-only solution.
Get the maintained source
Download task-tracker.zip and extract it. Open a terminal in the extracted task-tracker folder. You should see Main.java, Task.java, Status.java, TaskRepository.java, MemoryTaskRepository.java, TaskParser.java, ImportResult.java, TaskService.java and TrackerTest.java, plus README.md.
Install a JDK 25 from a trusted vendor if you want to run locally. A JDK includes both java, which launches programs, and javac, which compiles source. See the official Java getting-started guide. Check your terminal can find both:
Shell
java --versionjavac --version
Both should report 25 for a direct match with the browser reference. A missing command usually means the JDK installation or PATH is not configured; it is not a defect in your Java classes. PATH is the operating system's list of places to look for executable commands.
Compile, then choose an entry
From the folder containing the source files:
Shell
javac *.javajava Mainjava TrackerTest
javac writes .class files containing compiled bytecode. *.java selects all Java source files in this directory, so the compiler sees their relationships. On macOS and Linux shells the shell expands this pattern before launching javac; command-line handling differs across platforms. java Main names the class with a main method; do not write java Main.class. Re-run javac after editing source, otherwise java may execute old bytecode.
Main prints the sample report. TrackerTest prints All tracker checks passed, or throws AssertionError with a named failing expectation. The checks do not require -ea, because they throw explicitly rather than using Java's assert keyword. Compiler diagnostics identify a source filename and line; address the first error before treating later ones as independent failures.
Compare the same tests in the browser
The same project checksJavaimport java.util.ArrayList;
import java.util.List;
public final class ImportResult {
private final int accepted;
private final List<String> errors;
public ImportResult(int accepted, List<String> errors) {
this.accepted = accepted;
this.errors = new ArrayList<>(errors);
}
public int accepted() {
return accepted;
}
public List<String> errors() {
return new ArrayList<>(errors);
}
}
public class Main {
public static void main(String[] args) {
TaskRepository repository = new MemoryTaskRepository();
TaskService service = new TaskService(repository, new TaskParser());
String[] lines = {
"B2;Practice loops;20;TODO",
"bad record",
"A1;Read chapter;25;TODO",
"A1;Duplicate;10;TODO",
"C3;Write tests;20;TODO"
};
ImportResult result = service.importLines(lines);
System.out.println("Accepted: " + result.accepted());
System.out.println("Rejected: " + result.errors().size());
service.complete("B2");
for (Task task : service.openOrdered()) {
System.out.println(task.id() + ": " + task.title() + " (" + task.minutes() + ")");
}
System.out.println("Open minutes: " + service.openMinutes());
Task longest = Reports.longest(service.openOrdered());
System.out.println("Longest open: " + longest.id());
}
}
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public final class MemoryTaskRepository implements TaskRepository {
private final Map<String, Task> tasks = new LinkedHashMap<>();
@Override
public void add(Task task) {
if (task == null || tasks.containsKey(task.id())) {
throw new IllegalArgumentException("duplicate or missing task");
}
tasks.put(task.id(), task);
}
@Override
public Task find(String id) {
return tasks.get(id);
}
@Override
public void replace(Task task) {
if (task == null || !tasks.containsKey(task.id())) {
throw new IllegalArgumentException("existing task required");
}
tasks.put(task.id(), task);
}
@Override
public List<Task> all() {
return new ArrayList<>(tasks.values());
}
}
import java.util.List;
/** Generic reports that work for any Timed values, not only Task. */
public final class Reports {
private Reports() {
}
/** Reads only, so it accepts a list of any Timed subtype: List<Task> included. */
public static int totalMinutes(List<? extends Timed> items) {
int total = 0;
for (Timed item : items) {
total += item.minutes();
}
return total;
}
/** Returns the caller's own element type, or null for an empty list. Ties keep the first. */
public static <T extends Timed> T longest(List<T> items) {
T best = null;
for (T item : items) {
if (best == null || item.minutes() > best.minutes()) {
best = item;
}
}
return best;
}
}
public enum Status {
TODO,
DONE
}
/** Immutable task: copies of collections can safely share these values. */
public final class Task implements Timed {
private final String id;
private final String title;
private final int minutes;
private final Status status;
public Task(String id, String title, int minutes, Status status) {
if (id == null || id.trim().isEmpty() || id.contains(";")) {
throw new IllegalArgumentException("valid id required");
}
if (title == null || title.trim().isEmpty() || title.contains(";")) {
throw new IllegalArgumentException("valid title required");
}
if (minutes < 1 || minutes > 240 || status == null) {
throw new IllegalArgumentException("valid minutes and status required");
}
this.id = id.trim();
this.title = title.trim();
this.minutes = minutes;
this.status = status;
}
public String id() {
return id;
}
public String title() {
return title;
}
@Override
public int minutes() {
return minutes;
}
public Status status() {
return status;
}
public Task withStatus(Status next) {
return new Task(id, title, minutes, next);
}
}
/** A small four-field format, deliberately not general CSV. */
public final class TaskParser {
public Task parse(String line) {
if (line == null) {
throw new IllegalArgumentException("line required");
}
String[] fields = line.split(";", -1);
if (fields.length != 4) {
throw new IllegalArgumentException("expected id;title;minutes;status");
}
int minutes = Integer.parseInt(fields[2].trim());
Status status = Status.valueOf(fields[3].trim());
return new Task(fields[0], fields[1], minutes, status);
}
}
import java.util.List;
/** Duplicate adds and missing replacements reject before changing state. */
public interface TaskRepository {
void add(Task task);
Task find(String id); // Null means absent; null tasks are never stored.
void replace(Task task);
List<Task> all(); // A separate container in insertion order.
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public final class TaskService {
private final TaskRepository repository;
private final TaskParser parser;
public TaskService(TaskRepository repository, TaskParser parser) {
if (repository == null || parser == null) {
throw new IllegalArgumentException("dependencies required");
}
this.repository = repository;
this.parser = parser;
}
/**
* Read records until EOF. Skip blanks; report rejected records with their
* physical line number; retain successful records. The caller owns the
* reader: this method neither closes it nor hides a read failure.
*/
public ImportResult importFrom(BufferedReader reader) throws IOException {
List<String> errors = new ArrayList<>();
int accepted = 0;
int lineNumber = 0;
String line = reader.readLine();
while (line != null) {
lineNumber++;
if (!line.trim().isEmpty()) {
try {
repository.add(parser.parse(line));
accepted++;
} catch (IllegalArgumentException error) {
errors.add("Line " + lineNumber + ": " + error.getMessage());
}
}
line = reader.readLine();
}
return new ImportResult(accepted, errors);
}
/** Convenience for in-memory records: one array element per line. */
public ImportResult importLines(String[] lines) {
ImportResult result;
try (BufferedReader reader = new BufferedReader(new StringReader(String.join("\n", lines)))) {
result = importFrom(reader);
} catch (IOException error) {
throw new IllegalStateException("reading records failed", error);
}
return result;
}
/** Repeating completion is harmless. A missing ID returns false. */
public boolean complete(String id) {
Task task = repository.find(id);
if (task == null) {
return false;
}
repository.replace(task.withStatus(Status.DONE));
return true;
}
public List<Task> openOrdered() {
Comparator<Task> byMinutes = Comparator.comparingInt(Task::minutes);
return repository.all().stream()
.filter(task -> task.status() == Status.TODO)
.sorted(byMinutes.thenComparing(Task::id))
.toList();
}
public int openMinutes() {
return Reports.totalMinutes(openOrdered());
}
}
/** Anything with a duration in minutes. Task implements it; so could a future Session. */
public interface Timed {
int minutes();
}
import java.util.List;
/** No test library required; every failed expectation throws AssertionError. */
public class TrackerTest {
public static void main(String[] args) {
testValidationAndParser();
testRepository();
testImportRecovery();
testOrderingAndCompletion();
testReports();
System.out.println("All tracker checks passed");
}
static void testValidationAndParser() {
TaskParser parser = new TaskParser();
Task task = parser.parse(" A1 ; Read ; 1 ; TODO ");
check("normalized id", task.id().equals("A1"));
check("normalized title", task.title().equals("Read"));
check("minimum minutes", task.minutes() == 1);
check("maximum minutes", parser.parse("A;Read;240;DONE").minutes() == 240);
for (String line : new String[] {
"A;Read;0;TODO", "A;Read;241;TODO", "A;Read;nope;TODO",
"A;Read;25;", "A;Read;25;todo", "A;;25;TODO", ";Read;25;TODO",
"A;Read;25;TODO;extra"
}) {
try {
parser.parse(line);
throw new AssertionError("accepted invalid line: " + line);
} catch (IllegalArgumentException expected) {
// Only a documented input rejection counts as passing.
}
}
}
static void testRepository() {
TaskRepository repository = new MemoryTaskRepository();
Task original = new Task("A1", "Read", 25, Status.TODO);
repository.add(original);
try {
repository.add(new Task("A1", "Lost", 10, Status.DONE));
throw new AssertionError("duplicate accepted");
} catch (IllegalArgumentException expected) {
check("duplicate kept original", repository.find("A1") == original);
}
List<Task> snapshot = repository.all();
snapshot.clear();
check("snapshot cannot clear store", repository.all().size() == 1);
check("missing lookup", repository.find("missing") == null);
try {
repository.replace(new Task("missing", "Other", 10, Status.TODO));
throw new AssertionError("missing replacement accepted");
} catch (IllegalArgumentException expected) {
check("missing replace leaves store", repository.all().size() == 1);
}
}
static void testImportRecovery() {
TaskRepository repository = new MemoryTaskRepository();
TaskService service = new TaskService(repository, new TaskParser());
ImportResult result = service.importLines(new String[] {
"A1;Read;25;TODO", "bad", " ", "A1;Lost;10;TODO", "B2;Test;10;TODO"
});
check("accepted count", result.accepted() == 2);
check("rejected count", result.errors().size() == 2);
check("physical line number", result.errors().get(1).startsWith("Line 4:"));
check("good line after rejection", repository.find("B2") != null);
check("duplicate not overwritten", repository.find("A1").title().equals("Read"));
result.errors().clear();
check("error snapshot isolated", result.errors().size() == 2);
check("empty batch", service.importLines(new String[0]).accepted() == 0);
}
static void testOrderingAndCompletion() {
TaskRepository repository = new MemoryTaskRepository();
TaskService service = new TaskService(repository, new TaskParser());
service.importLines(new String[] {
"B;Second;20;TODO", "A;First;20;TODO", "C;Long;30;TODO"
});
check("tie broken by id", service.openOrdered().get(0).id().equals("A"));
check("primary sort", service.openOrdered().get(2).id().equals("C"));
check("sum open", service.openMinutes() == 70);
Task before = repository.find("A");
check("complete existing", service.complete("A"));
check("original immutable", before.status() == Status.TODO);
check("new state", repository.find("A").status() == Status.DONE);
check("completed excluded", service.openMinutes() == 50);
check("complete repeated", service.complete("A"));
check("repeat has same effect", service.openMinutes() == 50);
check("missing completion", !service.complete("missing"));
check("missing no new task", repository.all().size() == 3);
}
static void testReports() {
List<Task> tasks = List.of(
new Task("A", "Short", 10, Status.TODO),
new Task("B", "Long", 30, Status.TODO),
new Task("C", "Also long", 30, Status.TODO));
check("total of a List<Task>", Reports.totalMinutes(tasks) == 70);
check("empty total", Reports.totalMinutes(List.of()) == 0);
Task longest = Reports.longest(tasks);
check("longest keeps Task type and first of a tie", longest.id().equals("B"));
check("longest of empty", Reports.longest(List.<Task>of()) == null);
}
static void check(String name, boolean condition) {
if (!condition) {
throw new AssertionError(name);
}
}
}
Check your prediction
Text
All tracker checks passed
Exercise
Local or browser verification
Run the unmodified checks. Change the duplicate-ID rule to allow replacement, then run the checks again. Record the failing assertion, restore the rule, and confirm success. You can do this in the browser tabs without installing anything.
Hint 1: the idea
The repository check verifies that the original task survives duplicate rejection. Change only that policy so the failure has a single cause.
Hint 2: a first step
Find add in MemoryTaskRepository. Compare its duplicate guard with the named duplicate check in TrackerTest.
Show one solution
Removing the duplicate guard lets put replace the original task. TrackerTest then fails rather than printing its success line. Restore the guard before put and rerun. Locally, recompile with javac before running TrackerTest again; in the browser, Run compiles the edited source set each time.
A sensible next local step
The files use the default package deliberately so the browser can compile the flat source set. Real larger projects usually organize classes into named packages and use a build tool such as Maven or Gradle. Study those separately once you understand this project's boundaries; they are not prerequisites for today's checks.
The project has no disk persistence, HTTP service, GUI or account data. Adding any of them should introduce a clear boundary with its own failure policy and tests. Keep the deterministic in-memory checks even if you later add a live adapter. Never mistake a browser subset limitation for a rule that the full Java language forbids a feature.
Before leaving, save your edited source somewhere you control. Browser completion is a record of a visit, not a backup of your code or proof of mastery.
Completion is saved in this browser only. It does not measure mastery.