2019 AP CSA FRQ 2 - Steptracker
2019 AP CSA FRQ 2: StepTracker
Class Writing
Overview: The StepTracker class models a fitness tracking system. 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 class tracks cumulative step data and can report the number of active days and average steps per day.
Skills Tested: Writing a complete class, Declaring instance variables, Writing constructors, Writing accessor methods, Writing mutator methods, Tracking running totals
Provided Code
// You must write the complete class from scratch
// The class should include:
// - A constructor that takes the minimum steps for an active day
// - addDailySteps method to record steps for a day
// - activeDays method to return count of active days
// - averageSteps method to return average steps per day
Part COMPLETE: StepTracker (Complete Class) (9 points)
9 pointspublic class StepTracker
Scoring Rubric
| +1 | Declares appropriate private instance variable(s) for minimum steps threshold |
|---|---|
| +1 | Declares appropriate private instance variable(s) to track total steps |
| +1 | Declares appropriate private instance variable(s) to track number of days |
| +1 | Declares appropriate private instance variable(s) to track active days count |
| +1 | Constructor initializes instance variable for minimum steps using parameter |
| +1 | addDailySteps updates total steps and day count |
| +1 | addDailySteps increments active days when steps >= minimum |
| +1 | activeDays returns number of active days |
| +1 | averageSteps returns correct average (0.0 if no days, otherwise total/days) |
Solution
public class StepTracker
{
private int minSteps;
private int totalSteps;
private int numDays;
private int activeDayCount;
public StepTracker(int minStepsForActive)
{
minSteps = minStepsForActive;
totalSteps = 0;
numDays = 0;
activeDayCount = 0;
}
public void addDailySteps(int steps)
{
totalSteps += steps;
numDays++;
if (steps >= minSteps)
{
activeDayCount++;
}
}
public int activeDays()
{
return activeDayCount;
}
public double averageSteps()
{
if (numDays == 0)
{
return 0.0;
}
return (double) totalSteps / numDays;
}
}Step-by-Step Explanation
See solution code above.
Alternative Solution 1
public class StepTracker
{
private int threshold;
private int total;
private int days;
private int active;
public StepTracker(int min)
{
threshold = min;
// Other variables default to 0
}
public void addDailySteps(int s)
{
total = total + s;
days = days + 1;
if (s >= threshold)
active = active + 1;
}
public int activeDays()
{
return active;
}
public double averageSteps()
{
if (days == 0)
return 0.0;
return 1.0 * total / days;
}
}Common Mistakes to Avoid
Exam Tips
- Study the example table carefully: The specification table shows exactly what each method should return at each step. Trace through it to understand the expected behavior before writing code.
- Identify what data needs to persist: Ask yourself: what information do I need to remember between method calls? This tells you what instance variables you need.
- Write all required methods: Make sure you implement every method mentioned in the specification: constructor, addDailySteps, activeDays, and averageSteps. Missing any will cost points.
- Match return types exactly: activeDays returns int, averageSteps returns double. Make sure your method signatures match these return types.
- Think about edge cases: What happens when no data has been recorded? The spec tells us: activeDays returns 0, averageSteps returns 0.0. Handle these cases explicitly.
Related FRQs
Study Resources
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
tanner@apcsexamprep.com
Courses
AP CSA, CSP, & Cybersecurity
Response Time
Within 24 hours
Prefer email? Reach me directly at tanner@apcsexamprep.com