2019 AP® COMPUTER SCIENCE A FREE-RESPONSE QUESTIONS

  1. 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

public class StepTracker {   // new class 'StepTracker'

    StepTracker st = new StepTracker(10000);

    //variables which will be used in methods
    private int days;
    private int activeDays;
    private int totalSteps;
    private int minActive;

    public int StepTracker(int m) {
        minActive = m;
    }

    public int activeDays() { // returns # of active days
        return activeDays;
    }

    public void addDailySteps(int steps) { // adds 1 to day counter and allows input of step count. will also incremenet active days if step count is met
        days++;
        totalSteps += steps;
        if (steps >= minActive) {
            activeDays++;
        }
    }

    public double averageSteps() { // takes the average of steps across days
        if (days > 0) {
            return (double) totalSteps / days;
        }
        else {
            return 0.0;
        }
    }
}