StepTracker free response1

StepTracker free response problem from the 2019 AP Computer Science A Exam.

StepTracker is #2 from the from the 2019 AP Computer Science A Free Response problems.

https://apcentral.collegeboard.org/pdf/ap19-frq-computer-science-a.pdf?course=ap-computer-science-a

StepTracker class

```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;
}

{
if(steps >= minStepsActive)
activeDays++;

days++;
totalSteps += steps;
}

public int activeDays()
{
return activeDays;
}

public double averageSteps()
{
if(days == 0)
return 0;

}
}
```

StepTracker class – alternate solution

I’ve received many questions about whether this problem can/should be done using an ArrayList. There is no benefit to using an ArrayList vs storing only the data you actually need to implement the methods. That said, a correct solution using an ArrayList would receive full credit.

```public class StepTracker
{
private final int minStepsActive;
private ArrayList<Integer> stepsPerDay;

public StepTracker(int minStepsActive)
{
this.minStepsActive = minStepsActive;
stepsPerDay = new ArrayList<Integer>();
}

{
}

public int activeDays()
{
int activeDays = 0;

for(Integer steps : stepsPerDay)
if(steps >= minStepsActive)
activeDays++;

return activeDays;
}

public double averageSteps()
{
if(stepsPerDay.size() == 0)
return 0;

int totalSteps = 0;

for(Integer steps : stepsPerDay)
totalSteps += steps;

return (double) totalSteps / stepsPerDay.size();
}
}
```

Again, I don’t recommend this solution. I’ve added it here in response to the many questions I’ve received about it.

One comment on “StepTracker free response”

1. Brandon Horn

This particular solution has numerous problems (missing class header, the averageSteps method, etc).

Using an ArrayList to store each value would work if done correctly. It’s overly complex though and offers no benefits vs just storing what you actually need.