2019 Free Response Question 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

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 {

   // accessing and showing our private instance variables
    private int totalSteps;
    private int minimumSteps;
    private int daysActive;
    private int days;
    
    // constructor with the parameter 
    public StepTracker(int least){
        minimumSteps = least;
        totalSteps = 0; // values to initialize variables
        daysActive = 0;
        days = 0;
    }

    //added the dailySteps method as the "AddDailySteps"
    public void AddDailySteps(int steps){
        totalSteps += steps; //shows active days and the incremental counting
        days++;
        if (steps >= minSteps){
            daysActive++; // updates the other instance variables
        }
    }

    //the activeDays method
    public int getdaysActive(){ // declared and implemented into program
        return days;
    }

    public double avgSteps(){
        if (days == 0){
            return 0.0;
        } 
        else{

            //returns the calculated double of the average number of steps walked
            return (double) totalSteps / days; 
        }
    }

}