There is usually one free response question on the AP Computer Science exam each year that has you build a complete class. In 2019 it was the StepTracker
problem.
This problem gives you no code to start with, but a flushed out explanation of how the code you write should run. If you look at the first page of this problem there is a table of how the class should respond with calls to the various methods.
First, we need to create the class signature header. It’ll look like this.
public class StepTracker {
}
For AP CSA classes are always public, so the signature should look like this.
For this class to work we need to keep track of how many total days we’ve recorded, how many total steps have been taken, and how many days are considered “active.” We also need to know how many steps it takes to be considered active.
This all tells us what we need to instance variables and what our constructor needs to do.
Here’s what I did for the instance variables to take care of what we need to track.
private int days;
private int activeDays;
private int totalSteps;
private int minActive;
The problem gives an example of calling the constructor as StepTracker tr = new StepTracker(10000);
. The 10,000 is how many steps are considered active, so we’ll need to store that. All other instance variables are going to start at 0.
public StepTracker(int m) {
minActive = m;
days = 0;
activeDays = 0;
totalSteps = 0;
}
Sure, if you didn’t set days
, activeDays
and totalSteps
to 0 they would still be 0 since that’s the default for primitive int
values. But, I like to explicitly set values.
The activeDays
method is an accessor method.
public int activeDays() {
return activeDays;
}
activeSteps
returns the average of daily steps, or 0 if there are not any days recorded.
public double averageSteps() {
if (days == 0) {
return 0.0;
}
return (double) totalSteps / days;
}
A couple of things to unpack here.
First, we have to check whether we’ve recorded any days. If days
is 0 then we have to return 0.0
early. Otherwise we’d be dividing by zero on the last line.
We also need to cast to make sure that it’s returning a double and not truncating integer division. I did this by casting totalSteps
as a double before dividing by days
. Remember that dividing a double by an integer returns a double.
And now we need to implement the method to record the steps recorded for a day.
public void addDailySteps(int steps) {
days++;
totalSteps += steps;
if (steps >= minActive) {
activeDays++;
}
}
First, the code increments the instance variable days
since we’re adding a day to the records.
Next totalSteps
is incremented by steps
so that we keep tracking the total number of steps taken.
And last, if steps
is at least minActive
steps then we increment activeDays
since that day was “active.”
And here is the complete class from all the pieces above.
public class StepTracker {
private int days;
private int activeDays;
private int totalSteps;
private int minActive;
public StepTracker(int m) {
minActive = m;
days = 0;
activeDays = 0;
totalSteps = 0;
}
public int activeDays() {
return activeDays;
}
public double averageSteps() {
if (days == 0) {
return 0.0;
}
return (double) totalSteps / days;
}
public void addDailySteps(int steps) {
days++;
totalSteps += steps;
if (steps >= minActive) {
activeDays++;
}
}
}