Code FRQ

StepTracker, object w/ parameter that defines min steps to be active

methods:

  • addDailySteps, once per day
  • activeDays
  • averageSteps, returns avg num steps per day (total steps/number of days)

Write the StepTracker class

public class StepTracker {
    // initialize variables
    private int minActiveSteps
    private int totalSteps
    private int numTotalDays
    private int numActiveDays

    // set default construct
    public StepTracker(int minActiveSteps) {
        this.minActiveSteps = minActiveSteps;
        this.totalSteps = 0;
        this.numTotalDays = 0;
        this.numActiveDays = 0;
    }

    // once per day, increment steps, days, and possibly active
    public void addDailySteps(int numSteps) {
        this.totalSteps += numSteps;
        this.numTotalDays++;

        if (numSteps >= this.minActiveSteps) {
            this.numActiveDays++;
        }
    }

    // getter
    public int activeDays() {
        return this.numActiveDays;
    }

    // calculate
    public double averageSteps() {

        if (numTotalDays == 0) {
            return 0.0;
        } else {
            return (double) this.totalSteps/this.numTotalDays;
        }
    }
}

Overview

here is where the private initial variables are stored:

private int minActiveSteps
    private int totalSteps
    private int numTotalDays
    private int numActiveDays

now for constructor:

public StepTracker(int minActiveSteps) {
    this.minActiveSteps = minActiveSteps;
    this.totalSteps = 0;
    this.numTotalDays = 0;
    this.numActiveDays = 0;
}

It sets the default minactivesteps, and sets all the rest to 0.

public void addDailySteps(int numSteps) {
    this.totalSteps += numSteps;
    this.numTotalDays++;

    if (numSteps >= this.minActiveSteps) {
        this.numActiveDays++;
    }
}

It adds the steps to the total steps, and if the steps is over the active threshold, the active days will increase.

public int activeDays() {
    return this.numActiveDays;
}

just a getter

public double averageSteps() {

    if (numTotalDays == 0) {
        return 0.0;
    } else {
        return (double) this.totalSteps/this.numTotalDays;
    }
}

If there is 0 days, return 0 cuz thats what it says in the q

otherwise return steps/days to get average steps

Extra Notes

Class Creation

All classes should be capitalized to distinguish them from primitive datatypes (naming conventions). Constructors are necessary for each class in order for them to initialize their internal attributes, though they're special in that they do not require a return type because the object is being created in its stead.

Accessors and Mutators

Accessor methods (called getters) allow code outside of the class to access private variables. They're public methods that return private attributes, allowing the class to control access to its private attributes.

Mutator methods (called setters) allow code outside of the class to change private variables. They're public methods that take in a parameter (the new value) and return nothing, allowing the class to control outside code changing its private attributes.

Static variables and methods

Static variables (also known as class variables) are variables that are shared by the entire class, meaning that it will be the same for every instance of an object. It's useful for when certain attributes need to be standardized for an entire class (i.e. any object of a quadrilateral class must have the number of sides set equal to 4).

Static methods (also known as class methods) are methods that can be called using the class itself (along with objects of the class). Essentially, they make it easy for outside code to call certain methods specific to a class without having to instantiate a new object every single time, which can be seen in common imported Java classes such as Math.

Access modifiers

This

The keyword "this" inside classes means that the object will reference itself as the called object, meaning that the class can make self references even before having any objects created. The most common usage of this can be seen inside of constructors, where attributes are self-referenced to instantiate values.

Main method vs. Tester methods

The Main method is the method which starts the entire program along will all of its associated files. This is what actually starts the program and all of its components. Tester methods are usually public static void main(String[] args) methods inside of all the other classes in order to test that certain features inside of each class are working. As such, they are not called when the main method is run and only serves to make sure everything works.

public class Rhombus {
    private static int sides = 4;
    private String name;
    private double[] angles;

    public Rhombus(String name, double[] angles) {
        this.name = name;
        this.angles = angles;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static int getSides() {
        return sides;
    }

    public static void main(String[] args) {
        Rhombus timmy = new Rhombus("Tim", new double[]{90, 90, 90, 90});
    }
}

Rhombus.main(null);