This is also available as an AI-graded FRQ on the JuiceMind IDE for AP exam practice with rubrics and AI instant feedback. Teachers must create free accounts and assign the FRQ on JuiceMind for students to access it.
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.
This question asks you to 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.
Subsection3.17.2Determining the Instance Variables
Work in pairs or groups to read through the problem statement and determine the instance variables required for this class. During the exam, it helps to mark the words that are important and may describe the instance variables. Different groups may come up with different variables that will still work. Groups should report back and compare answers in class to determine the best variables before writing the class.
Select the phrases below which probably mention an instance variable. Note that some of these may be referring to the same variable or to local variables.
Think about phrases that indicate a value that is being stored or changed or returned.
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 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.
Given the StepTracker class description above, which of these statements describes an instance variable that the StepTracker constructor should set using a parameter?
Yes, addDailySteps(1000) is a mutator method that adds the steps given as a parameter to an instance variable that keeps track of the steps taken so far.
tr.activeDays();
No, activeDays() is an accessor method that returns the number of active days.
tr.averageSteps();
No, averageSteps() is a complex accessor method that calculates and returns the average number of steps from the instance variable.
Subsection3.17.3Writing the Class Header and Constructor
You will receive at least 1 point if you write the class header and a constructor that has the same name as the class and no return type. You will receive another point for creating private instance variables inside the class. Complete the class definition below with the class name, the instance variables you determined above, and the constructor. Remember that for this problem, a StepTracker object is created (with a constructor) with a parameter that defines the minimum number of steps that must be taken for a day to be considered active. The constructor will often have a parameter which it should assign to an instance variable. It should also assign default values to the other instance variables.
Write the first draft of the class StepTracker below with the class name, the instance variables, and the constructor with a parameter for the minimum number of steps threshold for active days. Make sure it compiles.
Here is the rubric for the instance variables and the constructor for this problem. Did you receive all 3 points? In class, your teacher may have you grade each othersβ code.
Subsection3.17.4Writing the Accessor Method activeDays
Each method in the FRQ is worth 1 - 3 points. The method header is usually worth 1 point and the code in the method body is usually worth another point or two depending on how complex it is.
This problem asks you to write a simple accessor method called activeDays which returns the number of active days (which should be an instance variable) for 1 point.
Accessor methods need a return type since they return the value of an instance variable or a value calculated from instance variables.
private void activeDays()
Accessor methods should not be private.
public int activeDays(int numSteps)
Accessor methods do not usually take parameters.
public void activeDays(int numSteps)
Accessor methods need a return type since they return the value of an instance variable or a value calculated from instance variables, and they do not usually have a parameter.
public int activeDays()
Correct, accessor methods are public, have a return type, and no parameter.
Copy the code from your first draft of the class StepTracker above with the instance variables and constructor. Write the accessor methods activeDays which returns the number of active days.
Here is the rubric for the accessor method activeDays for this problem. The second column is small mistakes that will still earn the point but the third column is larger mistakes that will not earn the point. Did you receive the point for this method? In class, your teacher may have you grade each othersβ code.
class Example
{
// Instance variable declaration
private typeOfVar varName;
// Mutator method template
public void changeVarName(typeOfVar newValue)
{
// an instance variable is changed through = or an operator like +=, -=,
// ++, etc.
varName = newValue;
}
}
This multiple choice problem can help you determine the mutator method header.
The code for this mutator method is a little more complex than the template above, because it needs to change more than 1 instance variable. Notice the comments in the sample code execution:
Copy the code from your draft of the class StepTracker above with the class name, the instance variables, constructor, and accessory method. Write the mutator method addDailySteps which takes a parameter and adds it to the appropriate instance variable and changes other instance variables appropriately.
Here is the rubric for the mutator method for this problem. The second column is small mistakes that will still earn the point but the third column is larger mistakes that will not earn the point. Did you receive all the points? In class, your teacher may have you grade each othersβ code.
Subsection3.17.6Writing the Accessor Method averageSteps
This problem asks you to write a more complex accessor method which uses the instance variables to calculate and return the averageSteps for 2 points. This method returns the average number of steps per day, calculated by dividing the total number of steps taken by the number of days tracked.
Accessor methods need a return type since they return the value of an instance variable or a value calculated from instance variables.
public int averageSteps()
When you compute an average using division, you usually end up with a double value, not int.
public double averageSteps()
Correct, accessor methods are public, have a return type, and no parameter. In this case, returning an average requires a double return type.
public void averageSteps(int numSteps)
Accessor methods need a return type since they return the value of an instance variable or a value calculated from instance variables, and they do not usually have a parameter.
The complex accessor method averageSteps() must calculate the average number of steps from your instance variables. Notice that the first time it is called in the sample code execution, it returns 0.0 since there are no steps recorded. This avoids a divide by 0 error.
Copy the code from your draft of the class StepTracker above with the instance variables, constructor, accessor and mutator methods. Write the accessor method 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.
Here is the rubric for the averageSteps method for this problem. Did you receive all the points? In class, your teacher may have you grade each othersβ code.