2019 AP CSA FRQ 2 - Steptracker

2019 AP CSA FRQ 2: StepTracker

Class Writing

Class Writing 9 Points Total Medium
Practice Timer (22 min recommended) 22:00

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

Topics Covered: Class DesignInstance VariablesConstructorAccessor MethodsMutator MethodsAccumulator Pattern

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 points
Write the complete StepTracker class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.
Method Signature: public class StepTracker
View Solution and Rubric

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

Forgetting to check for division by zero
In averageSteps, if numDays is 0, dividing will cause an error or return NaN. The specification states it should return 0.0 when no data has been recorded.
Integer division instead of double
When calculating average, totalSteps / numDays performs integer division. You must cast one operand to double: (double) totalSteps / numDays or 1.0 * totalSteps / numDays.
Not storing the minimum steps threshold
The minimum steps value must be stored as an instance variable so addDailySteps can compare against it later. Forgetting this instance variable is a common error.
Using >= instead of > or vice versa
The specification says 'at least 10,000 steps' which means >= not >. A day with exactly 10,000 steps IS considered active.
Making instance variables public
Instance variables should be declared private for proper encapsulation. Using public instance variables will lose points.
Not initializing instance variables
While Java initializes numeric instance variables to 0 by default, explicitly initializing them in the constructor is good practice and makes your intent clear.

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.

Official College Board Resources

Original Question PDF Scoring Guidelines

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com