Free Response Question (FRQ) - StepTracker Class

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period4 exercises13 graded tests

Files

README.mdDownload

Free Response Question (FRQ) - StepTracker Class

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

The 2019 FRQ 2 for the class StepTracker is question 2 on page 7 of https://apstudents.collegeboard.org/sites/default/files/2019-05/ap19-frq-computer-science-a.pdf, reproduced below.

StepTracker Class FRQ

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 ExpressionsValue 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();0No data have been recorded yet.
tr.averageSteps();0.0When 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();0No day had at least 10,000 steps.
tr.averageSteps();7000.0The average number of steps per day is (14000 / 2).
tr.addDailySteps(13000);This represents an active day.
tr.activeDays();1Of the three days for which step data were entered, one day had at least 10,000 steps.
tr.averageSteps();9000.0The 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();2Of the five days for which step data were entered, two days had at least 10,000 steps.
tr.averageSteps();10222.2The 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.

Determining 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.

It may help to first identify the variables that are needed for the constructor and the accessor and mutator methods.

Check your understanding: 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.

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.

Answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQstepTracker.html#steptracker_instance_variables

These multiple choice questions will help you to determine the instance variables through the constructor parameters and accessor and mutator methods.

Check your understanding: Given the StepTracker class description above, which of these statements describes an instance variable that the StepTracker constructor should set using a parameter? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQstepTracker.html#steptracker-constructor

Check your understanding: Which of the following methods is an accessor method that returns the value of an instance variable? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQstepTracker.html#steptracker-accessor

Check your understanding: Which of the following methods is a mutator method that changes the value of an instance variable? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQstepTracker.html#steptracker-mutator

What are the instance variables (at least 4!) that you need for the StepTracker class? What are the data types for each instance variable?

Discuss this with your class, or answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQstepTracker.html#steptracker-variables

Writing 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.

Exercise 1

Write the first draft of the class StepTracker 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.

Open the 01-stepTrackerCode1 folder and edit Main.java. Press Run to try it; Run Tests checks it (3 tests). Name the class, then rename the file to match.

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.

Rubric for instance variables and constructor

Rubric for instance variables and constructor

Writing 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.

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;
    }
}

This multiple choice problem can help you determine the accessor method header.

Check your understanding: Which of the following is a good method header for the accessor method activeDays()? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQstepTracker.html#steptracker-accessor-header

Exercise 2

Copy the code from your first draft of the class StepTracker with the instance variables and constructor. Write the accessor methods activeDays which returns the number of active days.

Open the 02-stepTrackerCode2 folder and edit StepTracker.java. Press Run to try it; Run Tests checks it (3 tests).

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.

Rubric for accessor method activeDays()

Rubric for accessor method activeDays()

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;
    }
}

This multiple choice problem can help you determine the mutator method header.

Check your understanding: Which of the following is a good method header for the mutator method addDailySteps? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQstepTracker.html#steptracker-mutator-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:

Statements and ExpressionsValue Returned (blank if no value)Comment
tr.addDailySteps(5000);This is too few steps for the day to be considered active.
tr.activeDays();0No day had at least 10,000 steps.
tr.addDailySteps(13000);This represents an active day.
tr.activeDays();1Of 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 below may help you.

Check your understanding: Which of the following values does the mutator method addDailySteps need to change? (check all that apply) — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQstepTracker.html#steptracker-mutator-changes

Exercise 3

Copy the code from your draft of the class StepTracker 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.

Open the 03-stepTrackerCode3 folder and edit StepTracker.java. Press Run to try it; Run Tests checks it (3 tests).

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.

Rubric for mutator method

Rubric for mutator method

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.

This multiple choice problem can help you determine the accessor method header.

Check your understanding: Which of the following is a good method header for the accessor method averageSteps() which returns the average number of steps per day? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/FRQstepTracker.html#steptracker-accessor-header2

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.

Statements and ExpressionsValue Returned (blank if no value)Comment
tr.averageSteps();0.0When no step data have been recorded, the averageSteps method returns 0.0.
Exercise 4

Copy the code from your draft of the class StepTracker 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.

Open the 04-stepTrackerCode4 folder and edit StepTracker.java. Press Run to try it; Run Tests checks it (4 tests).

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.

Rubric for averageSteps method

Rubric for averageSteps method

For teachers

Free Response Question (FRQ) - StepTracker Class

Students read CSAwesome2's “Free Response Question (FRQ) - StepTracker Class” in CS Room and work through its 4 Java coding exercises.

  • Time: 1 class period
  • Tests: 13 in 4 graded exercises, set up for you when you import the lesson

Goals

  • Practice for Unit 3: Class Creation.

Before class

In class

  1. Students read README.md. Questions that need CS Awesome's interactive widgets link back to it.
  2. Students do exercises 01–04, each in its own folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-stepTrackerCode1Main.javaAutograded3
02-stepTrackerCode2StepTracker.javaAutograded3
03-stepTrackerCode3StepTracker.javaAutograded3
04-stepTrackerCode4StepTracker.javaAutograded4

Teachers can download a reference solution after signing in.