Prompt:

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.

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 int minSteps;
    private int totalSteps;
    private int activeDays;
    private int totalDays;

    public StepTracker(int minSteps) {
        this.minSteps = minSteps;
        this.totalSteps = 0;
        this.activeDays = 0;
        this.totalDays = 0;
    }

    public void addDailySteps(int steps) {
        totalSteps += steps;
        totalDays++;
        if (steps >= minSteps) {
            activeDays++;
        }
    }

    public int activeDays() {
        return activeDays;
    }

    public double averageSteps() {
        if (totalDays == 0) {
            return 0.0;
        } else {
            return (double) totalSteps / totalDays;
        }
    }

    public static void main(String[] args) {
        // Create a StepTracker object with a minimum step requirement of 10,000
        StepTracker tr = new StepTracker(10000);

        // Add daily steps and test the methods
        tr.addDailySteps(9000);
        tr.addDailySteps(5000);
        System.out.println("Active Days: " + tr.activeDays());
        System.out.println("Average Steps: " + tr.averageSteps());

        tr.addDailySteps(13000);
        System.out.println("Active Days: " + tr.activeDays());
        System.out.println("Average Steps: " + tr.averageSteps());

        tr.addDailySteps(23000);
        tr.addDailySteps(1111);
        System.out.println("Active Days: " + tr.activeDays());
        System.out.println("Average Steps: " + tr.averageSteps());

        tr.addDailySteps(33000);
        tr.addDailySteps(2222);
        System.out.println("Active Days: " + tr.activeDays());
        System.out.println("Average Steps: " + tr.averageSteps());

        tr.addDailySteps(44000);
        tr.addDailySteps(3333);
        System.out.println("Active Days: " + tr.activeDays());
        System.out.println("Average Steps: " + tr.averageSteps());
    }
}

StepTracker.main(null);
Active Days: 0
Average Steps: 7000.0
Active Days: 1
Average Steps: 9000.0
Active Days: 2
Average Steps: 10222.2
Active Days: 3
Average Steps: 12333.285714285714
Active Days: 4
Average Steps: 14851.777777777777

StepTracker Class Definition

public class StepTracker {
}
  • It has instance variables and methods to track and calculate step-related information from the table

Instance Variables

private int minSteps;
private int totalSteps;
private int activeDays;
private int totalDays;
  • minSteps: This variable holds the minimum number of steps required for a day to be considered active
  • totalSteps: This variable accumulates the total number of steps taken across all days
  • activeDays: It counts the number of days where the step count exceeded or equaled minSteps
  • totalDays: This variable keeps track of the total number of days for which step data has been recorded

Constructor

public StepTracker(int minSteps) {
    this.minSteps = minSteps;
    this.totalSteps = 0;
    this.activeDays = 0;
    this.totalDays = 0;
}
  • The constructor initializes the minSteps instance variable with the provided minSteps parameter
  • It sets totalSteps, activeDays, and totalDays to zero, as there is no data recorded initially

addDailySteps Method

public int activeDays() {
    return activeDays;
}
  • addDailySteps is used to accumulate daily step counts
  • It increments totalSteps by the number of steps taken on the current day
  • totalDays is incremented to keep track of the total number of days
  • If the number of steps on the current day is greater than or equal to minSteps, it increments activeDays

averageSteps Method

public double averageSteps() {
    if (totalDays == 0) {
        return 0.0;
    } else {
        return (double) totalSteps / totalDays;
    }
}
  • averageSteps method calculates and returns the average number of steps per day
  • It handles the case where no data has been recorded yet by returning 0.0 to prevent division by zero

Main Method for Testing

public static void main(String[] args) {
    // Create a StepTracker object with a minimum step requirement of 10,000
    StepTracker tr = new StepTracker(10000);

}
  • The main method is used for testing the StepTracker class
  • It creates a StepTracker object tr with a minimum step requirement of 10,000, adds daily steps, and tests the activeDays and averageSteps methods