5.1 Anatomy of a Class

  • Class: blueprint used to create objects
  • Instances, attributes, constructors, methods
  • Objects: instances of a class
  • public: no restricted access -- classes and constructors
  • private: restrict access -- instance variables

5.2 Constructors

  • initializes instance variables when an object is created
  • Each object has attributes which are the instance of a class
  • Constructors set the initial state of an object by assigning initial values to instance variables

5.3 Comments

  • ignored by compiler
  • make code more readable
  • not required for the AP exam

5.4 Accessor Method

  • Allows objects outside of the class to obtain values
  • non void returns a single value
  • toString() Method

5.5 Mutator Method

  • Don't return values
  • Must be used when different classes need to modify instance variables

2019 FRQ Question 2

public class StepTracker
{
    private int minSteps;
    private int totalSteps;
    private int numDays;
    private int numActiveDays;
    public StepTracker(int threshold)
    {
        minSteps = threshold;
        totalSteps = 0;
        numDays = 0;
        numActiveDays = 0;
    }
    public void addDailySteps(int steps)
    {
        totalSteps += steps;
        numDays++;
        if (steps >= minSteps)
        {
            numActiveDays++;
        }
    }
    public int activeDays()
    {
        return numActiveDays;
    }
    public double averageSteps()
    {
        if (numDays == 0)
        {
            return 0.0;
        }
        else
        {
            return (double) totalSteps / numDays;
        }
    }
}