Skip to main content

Section 11.2 Classes Practice - StepTracker Class

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.
Table 11.2.1.
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).
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.
Part a. Determining the Instance Variables
Read through the problem statement and determine the instance variables required for this class. When starting to work on a problem like this it helps to circle the words that are important and may describe the instance variables.
It may help to first identify the variables that are needed for the constructor and the accessor and mutator methods.

Checkpoint 11.2.2.

Answering the multiple choice problem will help you determine the instance variables through the constructor parameters and accessor and mutator methods.

Checkpoint 11.2.3.

Given the StepTracker class description above, which of these statements describes an instance variable that the StepTracker constructor should set using a parameter?
  • the minimum number of steps that must be taken for a day to be considered active
  • Yes, the problem definition describes this as a parameter to create a StepTracker object.
  • the number of active days
  • This is not described as a parameter to create an StepTracker object.
  • the average number of steps per day
  • This is not described as a parameter to create an StepTracker object.
  • the total number of steps taken
  • This is not described as a parameter to create an StepTracker object.
  • number of days tracked
  • This is not described as a parameter to create an StepTracker object.

Checkpoint 11.2.4.

Which of the following methods is an accessor method that returns the value of an instance variable?
  • StepTracker tr = new StepTracker(1000)
  • This is a call to the constructor.
  • tr.addDailysteps(1000);
  • No, addDailySteps(1000) probably adds the given steps to an instance variable as a mutator method.
  • tr.activeDays();
  • Yes, activeDays() is an accessor method that returns the number of active days (a great instance variable!).

Checkpoint 11.2.5.

Which of the following methods is a mutator method that changes the value of an instance variable?
  • StepTracker tr = new StepTracker(1000)
  • No, this is a call to the constructor.
  • tr.addDailysteps(1000);
  • 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.
At this point you should be able to answer the following questions: What are the instance variables (at least 4!) that you need for the StepTracker class? What are the data types for each instance variable?
Part b. Writing the Class Header and Constructor

Checkpoint 11.2.6.

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.
Part c. Writing the Accessor Method activeDays
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.
Remember that accessor methods usually look like the following:
class ExampleClass
{
  //Instance variable declaration
  private typeOfVar varName;

  // Accessor method template
  public typeOfVar getVarName()
  {
     return varName;
  }
}
Answering the multiple choice problem will help you determine the accessor method header.

Checkpoint 11.2.7.

Which of the following is a good method header for the accessor method activeDays()?
  • public void activeDays()
  • 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.

Checkpoint 11.2.8.

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.
Part d. Writing the Mutator Method addDailySteps
This problem asks you to write a more complex mutator method called addDailySteps worth 3 points.
Remember that mutator methods often look like the following:
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;
    }
}
Answering the multiple choice problem will help you determine the mutator method header.

Checkpoint 11.2.9.

Which of the following is a good method header for the mutator method addDailySteps?
  • public void addDailySteps()
  • Mutator methods take a parameter to change the value of an instance variable.
  • private void addDailySteps()
  • Mutator methods should not be private.
  • public int addDailySteps(int numSteps)
  • Mutator methods do not usually return a value.
  • public void addDailySteps(int numSteps)
  • Correct, mutator methods are public with a void return type and take a parameter to change the value of an instance variable.
  • private int addDailySteps()
  • Mutator methods should not be private and should take a parameter to change the value of an instance variable.
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:
Table 11.2.10.
Statements and Expressions Value Returned (blank if no value) Comment
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.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.
Consider each of your instance variables and whether this method should change them. The problem which you can reveal below may help you.

Checkpoint 11.2.11.

Which of the following values does the mutator method addDailySteps need to change? (check all that apply)
  • the minimum number of steps that must be taken for a day to be considered active
  • The minimum is set by the constructor.
  • the number of active days
  • Yes, addDailySteps should determine whether the number of steps given in its parameter is an active day and if so, change this variable.
  • the average number of steps per day
  • This method does not have to calculate the average.
  • the total number of steps taken
  • Yes, addDailySteps should add the number of steps taken that day in its parameter to the total.
  • number of days tracked
  • Yes, addDailySteps is called each day and can change the variable for the number of days being tracked.

Checkpoint 11.2.12.

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.
Part d. Writing 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.
Answering the multiple choice problem will help you determine the accessor method header.

Checkpoint 11.2.13.

Which of the following is a good method header for the accessor method averageSteps() which returns the average number of steps per day?
  • public void averageSteps()
  • 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.
  • public int averageSteps(int numSteps)
  • Accessor methods do not usually take parameters.
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.
Table 11.2.14.
Statements and Expressions Value Returned (blank if no value) Comment
tr.averageSteps(); 0.0 When no step data have been recorded,
the averageSteps method returns 0.0.

Checkpoint 11.2.15.

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.
You have attempted of activities on this page.