Java

OOP GUIDE / WORK

 

STEPTRACKER CLASS SOURCE

 

Source

  • This question was part of the 2019 AP exam free response questions.  You can find all four of the original questions here on the AP site.

 

Online Solution & Guide

  • AP publishes a solution guide contains marking tips and a few solutions for each question after the test.  Click here to see the guide.

 

 

TASK – PART 1 – STEPTRACKER CLASS

 

Here is my solution:

 

 

public class StepTracker

{

     private int totalSteps;

     private int totalDays;

     private int activeDays;

     private int activeLimit;

    

     public StepTracker(int activeLimit)

     {

           this.activeLimit = activeLimit;

          totalSteps = 0;

           totalDays = 0;

           activeDays = 0;

     }

    

     public int activeDays()

     {

           return activeDays;

     }

    

     public double averageSteps()

     {

           if (totalDays == 0)  //careful to avoid divide by 0 issue

                return 0;      

           else                 //careful to avoid int division

                return (double)totalSteps / totalDays;     

     }

    

     public void addDailySteps(int steps)

     {

           totalSteps += steps;

           totalDays++;

           if (steps >= activeLimit)

                activeDays++;

     }

}

 

 

 

 

TASK – PART 2 – TESTING THE STEPTRACKER CLASS

 

No solution required.