We are writing software to collect and manage data on how fast racers can complete obstacle courses.
An obstacle course is a series of difficult physical challenges (like walls, hurdles, and ponds) that a racer must go through.
Each course consists of multiple obstacles. The software stores how long it takes for racers to finish each obstacle, and provides useful statistics based on those times.
Definitions:
- A
runis a particular attempt to complete an entire obstacle course. - A
run collectionis a group of runs on a particular course by the user. - An
obstacleis a portion of a course. We track how long it takes to finish each portion of the course.
Given the following classes, implement the missing method behavior:
class Course {
public String title;
public int obstacleCount;
public Course(String courseTitle, int obstacles) {
title = courseTitle;
obstacleCount = obstacles;
}
@Override
public boolean equals(Object o) {if (!(o instanceof Course)) {return false;}
Course c = (Course) o;
return c.title == this.title && c.obstacleCount == this.obstacleCount;
}
@Override
public int hashCode() {return (title == null ? 0 : title.hashCode()) * obstacleCount;
}
}
class Run {
public Course course;
public boolean complete;
public List<Integer> obstacleTimes;
public Run(Course runCourse) {
course = runCourse;
complete = false;
obstacleTimes = new ArrayList<>();}
public void addObstacleTime(int obstacleTime) {if (complete) {throw new IllegalStateException("Cannot add obstacle to complete run");
}
obstacleTimes.add(obstacleTime);
if (obstacleTimes.size() == course.obstacleCount) {complete = true;}
}
public int getRunTime() {return obstacleTimes.stream().mapToInt(Integer::intValue).sum();}
}
class RunCollection {
public Course course;
public List<Run> runs;
public RunCollection(Course collectionCourse) {
course = collectionCourse;
runs = new ArrayList<>();}
public int getNumRuns() {return runs.size();
}
public void addRun(Run run) {if (!run.course.equals(course)) {throw new IllegalArgumentException("run's Course is not the same as the RunCollection's");
}
runs.add(run);
}
public int personalBest() {return runs.stream().mapToInt(v -> v.getRunTime()).min().orElse(Integer.MAX_VALUE);
}
}
Implement the code so that the provided tests pass.
This problem is a Java object-modeling and aggregation exercise. You need to track obstacle times for each run, mark a run complete once it reaches the course obstacle count, reject invalid additions after completion, and keep only runs that belong to the same course inside a RunCollection. The key statistic is personalBest(), which returns the minimum total run time across all stored runs, or Integer.MAX_VALUE when the collection is empty. It mainly tests list handling, basic validation, and simple stream-based or loop-based minimum calculation.