Question 321
A fitness app records daily exercise sessions for one user. You will implement a class FitnessTracker that stores a rolling log of the user's most recent workouts and reports summary statistics. The class will be used like this.
FitnessTracker ft = new FitnessTracker(7); // track the last 7 sessions
ft.logSession(30, 4.2); // 30 minutes, 4.2 kilometers
ft.logSession(45, 6.0);
ft.logSession(20, 2.5);
double avgPace = ft.averagePace(); // minutes per kilometer
int active = ft.activeDays(25); // sessions of at least 25 minutes
A FitnessTracker must meet the following specifications.
| Method / Constructor Header | Description |
|---|---|
public FitnessTracker(int capacity) |
Creates a tracker that retains at most capacity of the most recently logged sessions. The tracker begins with no logged sessions. You may assume capacity > 0. |
public void logSession(int minutes, double kilometers) |
Records a single workout session. If the tracker already holds capacity sessions, the oldest logged session is discarded before the new session is added. minutes is always > 0 and kilometers is always > 0.0. |
public int sessionCount() |
Returns the number of sessions currently stored (always between 0 and capacity). |
public double averagePace() |
Returns the average pace (minutes per kilometer) across all currently stored sessions, computed as (total minutes) / (total kilometers) using double arithmetic. If no sessions are stored, returns 0.0. |
public int activeDays(int minMinutes) |
Returns the number of currently stored sessions whose duration is greater than or equal to minMinutes. |
Write the complete FitnessTracker class. Your implementation must meet all specifications and conform to the example. You may define additional private fields, constructors, and helper methods as needed. You may use any ArrayList- or array-based approach.
Main Solution
Sample Solution:
public class FitnessTracker
{
private int capacity;
private ArrayList<Integer> minutesLog;
private ArrayList<Double> kmLog;
public FitnessTracker(int cap)
{
capacity = cap;
minutesLog = new ArrayList<Integer>();
kmLog = new ArrayList<Double>();
}
public void logSession(int minutes, double kilometers)
{
if (minutesLog.size() == capacity) // evict oldest first
{
minutesLog.remove(0);
kmLog.remove(0);
}
minutesLog.add(minutes);
kmLog.add(kilometers);
}
public int sessionCount()
{
return minutesLog.size();
}
public double averagePace()
{
if (minutesLog.size() == 0) return 0.0;
int totalMin = 0;
double totalKm = 0.0;
for (int i = 0; i < minutesLog.size(); i++)
{
totalMin += minutesLog.get(i);
totalKm += kmLog.get(i);
}
return totalMin / totalKm; // double division
}
public int activeDays(int minMinutes)
{
int count = 0;
for (int i = 0; i < minutesLog.size(); i++)
{
if (minutesLog.get(i) >= minMinutes) count++;
}
return count;
}
}
Rubric (9 pts): class declaration + private state (1); constructor initializes all fields with empty log (1); logSession adds (1); logSession correctly discards oldest when at capacity (1); sessionCount (1); averagePace handles the empty case (1); averagePace sums and divides using double arithmetic (1); activeDays iterates all stored sessions using >= (1); overall Java class structure / visibility consistent (1).
Answer images

