Methods and Control Structures
2019 AP® COMPUTER SCIENCE A FREE-RESPONSE QUESTIONS
- This question involves the implementation of a fitness tracking system that is represented by the
StepTrackerclass. AStepTrackerobject 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 dayactiveDays, which returns the number of active daysaverageSteps, 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;
}
}
}