2.

This question involves the implementation of a fitness tracking system that is represented by the StepTracker class. A StepTracker object is created with a parameter that defines the minimum number of steps that must be taken for a day to be considered active. The StepTracker class provides a constructor and the following methods.

  • addDailySteps, which accumulates information about steps, in readings taken once per day
  • activeDays, which returns the number of active days
  • averageSteps, which returns the average number of steps per day, calculated by dividing the total number of steps taken by the number of days tracked

The following table contains a sample code execution sequence and the corresponding results.

image

Write the complete StepTracker class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.

public class StepTracker
{
  private final int minStepsActive;
  private int activeDays;
  private int days;
  private int totalSteps;
  
  public StepTracker(int minStepsActive)
  {
    this.minStepsActive = minStepsActive;
    activeDays = 0;
    days = 0;
    totalSteps = 0;
  }
    
  public void addDailySteps(int steps)
  {
      if(steps >= minStepsActive)
        activeDays++;
  
      days++;
      totalSteps += steps;
  }
    
  public int activeDays()
  {
      return activeDays;
  }
    
  public double averageSteps()
  {
      if(days == 0)
        return 0;
    
      return totalSteps / (double) days;
  }
}