Sprint View

2019 FRQ 2

5 min read

Question 2:

This question involves the implementation of a fitness tracking system that is represented by the StepTracker class. 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 StepTracker class provides a constructor and the following methods.

• addDailySteps, which accumulates information about steps, in readings taken once per day

• activeDays, which returns the number of active days

• averageSteps, which returns the average number of steps per day, calculated by dividing the total number of steps taken by the number of days tracked

The following table contains a sample code execution sequence and the corresponding results.

Statements and Expressions Value Returned (blank if no value) Comment
StepTracker tr = new StepTracker(10000);   Days with at least 10,000 steps are considered active. Assume that the parameter is positive.
tr.activeDays(); 0 No data have been recorded yet.
tr.averageSteps(); 0.0 When no step data have been recorded, the averageSteps method returns 0.0.
tr.addDailySteps(9000);   This is too few steps for the day to be considered active.
tr.addDailySteps(5000);   This is too few steps for the day to be considered active.
tr.activeDays(); 0 No day had at least 10,000 steps.
tr.averageSteps(); 7000.0 The average number of steps per day is (14000 / 2).
tr.addDailySteps(13000);   This represents an active day.
tr.activeDays(); 1 Of the three days for which step data were entered, one day had at least 10,000 steps.
tr.averageSteps(); 9000.0 The average number of steps per day is (27000 / 3).
tr.addDailySteps(23000);   This represents an active day.
tr.addDailySteps(1111);   This is too few steps for the day to be considered active.
tr.activeDays(); 2 Of the five days for which step data were entered, two days had at least 10,000 steps.
tr.averageSteps(); 10222.2 The average number of steps per day is (51111 / 5).

Code Runner Challenge

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.

View IPYNB Source
// CODE_RUNNER: 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.

public class StepTracker {
    // Private instance variables
    private int minSteps;
    private int totalSteps;
    private int numDays;
    private int activeDays;
    
    // Constructor
    public StepTracker(int minSteps) {
        this.minSteps = minSteps;
        this.totalSteps = 0;
        this.numDays = 0;
        this.activeDays = 0;
    }
    
    // addDailySteps method
    public void addDailySteps(int steps) {
        totalSteps += steps;
        numDays++;
        if (steps >= minSteps) {
            activeDays++;
        }
    }
    
    // activeDays method
    public int activeDays() {
        return activeDays;
    }
    
    // averageSteps method
    public double averageSteps() {
        if (numDays == 0) {
            return 0.0; 
        }
        return (double) totalSteps / numDays;
    }
    
    public static void main(String[] args) {
        System.out.println("=== StepTracker Solution ===");
        System.out.println();
        
        StepTracker tr = new StepTracker(10000);
        System.out.println("Created StepTracker with minSteps = 10000");
        
        System.out.println(); 
        System.out.println("tr.activeDays() = " + tr.activeDays());
        System.out.println("Expected: 0");
        
        System.out.println();
        System.out.println("tr.averageSteps() = " + tr.averageSteps());
        System.out.println("Expected: 0.0");
        
        tr.addDailySteps(9000);
        System.out.println();
        System.out.println("Added 9000 steps (not active)");
        
        tr.addDailySteps(5000);
        System.out.println("Added 5000 steps (not active)");
        
        System.out.println();
        System.out.println("tr.activeDays() = " + tr.activeDays());
        System.out.println("Expected: 0");
        
        System.out.println();
        System.out.println("tr.averageSteps() = " + tr.averageSteps());
        System.out.println("Expected: 7000.0");
        
        tr.addDailySteps(13000);
        System.out.println();
        System.out.println("Added 13000 steps (ACTIVE!)");
        
        System.out.println();
        System.out.println("tr.activeDays() = " + tr.activeDays());
        System.out.println("Expected: 1");
        
        System.out.println();
        System.out.println("tr.averageSteps() = " + tr.averageSteps());
        System.out.println("Expected: 9000.0");
        
        tr.addDailySteps(23000);
        System.out.println();
        System.out.println("Added 23000 steps (ACTIVE!)");
        
        tr.addDailySteps(1111);
        System.out.println("Added 1111 steps (not active)");
        
        System.out.println();
        System.out.println("tr.activeDays() = " + tr.activeDays());
        System.out.println("Expected: 2");
        
        System.out.println();
        System.out.println("tr.averageSteps() = " + tr.averageSteps());
        System.out.println("Expected: 10222.2");
    }
}

StepTracker.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Scoring Guidelines:

Intent: Define implementation of a class to record fitness data

Total: 9 Points

+1: Declares all appropriate private instance variables

+1: Declares header: public StepTracker(int ___)

+1: Uses parameter and appropriate values to initialize instance variables

+1: Declares header: public void addDailySteps(int ___)

+1: Identifies active days and increments count

+1: Updates other instance variables appropriately

+1: Declares and implements public int activeDays()

+1: Declares header: public double averageSteps()

+1: Returns calculated double average number of steps

Question 2: Scoring Notes

Points Rubric Criteria Responses earn the point even if they… Responses will not earn the point if they…
+1 Declares all appropriate private instance variables   * omit keyword private
* declare variables outside the class
+2 Constructor    
+1 Declares header: public StepTracker(int __) * omit keyword public * declare method private
+1 Uses parameter and appropriate values to initialize instance variables * initialize primitive instance variables to default values when declared * fail to use the parameter to initialize some instance variable
* fail to declare instance variables
* initialize local variables instead of instance variables
* assign variables to parameters
+3 addDailySteps method    
+1 Declares header: public void addDailySteps(int __) * omit keyword public * declare method private
+1 Identifies active days and increments count * put valid comparison erroneously in some other method * fail to use the parameter as part of the comparison
* fail to increment a count of active days
* fail to increment an instance variable
* compare parameter to some numeric constant
+1 Updates other instance variables appropriately   * update another instance variable only on active days
* update another instance variable inappropriately
* fail to update appropriate instance variable
* update a local variable
+1 activeDays method    
+1 Declares and implements public int activeDays() * return appropriate count of active days where the instance variables were updated improperly in addDailySteps or activeDays * declare method private
* return value that is not the number of active days
* fail to return a value
+2 averageSteps method    
+1 Declares header: public double averageSteps() * omit keyword public * declare method private
+1 Returns calculated double average number of steps * maintain instance variables improperly but calculate appropriate average
* fail to handle the special case where no days are tracked
* use integer division
* calculate something other than steps divided by days
* fail to return

Course Timeline