Unit 5 Writing Classes Notes

  • Classes are an essential aspect of OOP
  • Models real world objects but in code
  • State, attributes, behavior, Instance of a class, Represented by an instance in the program
  • Methods: Accessors/Getters, Mutators/Setters, Main method (tester)
  • Objects: instances of a class
  • control access and visibilty of classes, data and methods:
  • public: no restricted access (can be accessed outside the declaring class)
  • private: restrict access to declaring class
  • Concept of OOP: 1. Variables and methods are wrapped in a unit (class) 2. Implementation details of the class are hidden from the user
public class Sport {
    private String name;
    private int numAthletes;

    public Sport(){
        name = "";
        numAthletes = 0;
    }
    public Sport(String n, int numAth){
        name = n;
        numAthletes = numAth;
    }
    public Sport(String n){
        name = n;
        numAthletes = 0;
    }
}
  • Procedural abstratction allows us to name a block of code as a method and call it whenever we need it, abstracting away the details of hit it works
  • The idea is that we have code which can cope with a variety of different situations, depending on how its parameters are set and when it is called.
  • 1 Object of the Class, 2 Method Call, 3 Method Definition
  • Default Constructor: no parameters
  • Sets instance variables equal to default values
  • String: null
  • Int/Double: 0
  • Java provides a no-argument default constructor if there are no constructors inside the class
  • Instance variables set to default values
// Step 1
Classname objectName = new Classname();

// Step 2
objectName();

//Step 3
    // method header
    public void methodName() {
        // method body for the code
    }

Step Tracker Class

Code

Prompt- StepTracker, object with a parameter that defines the minimum steps to be active

Required methods- addDailySteps activeDays averageSteps

public class StepTracker {
    private int days;
    private int activeDays;
    private int totalSteps;
    private int minActive;

    public StepTracker(int m) {
        minActive = m;
        days = 0;
        activeDays = 0;
        totalSteps = 0;
    }

    public int activeDays() {
        return activeDays;
    }

    public double averageSteps() {
        if (days == 0) {
            return 0.0;
        }
        return (double) totalSteps / days;
    }

    public void addDailySteps(int steps) {
        days++;
        totalSteps += steps;
        if (steps >= minActive) {
            activeDays++;
        }
    }
}

Instance Variables: keep track of how many total days we’ve recorded, how many total steps have been taken, and how many days are considered “active".

private int days;
private int activeDays;
private int totalSteps;
private int minActive;

Explicit sets the variables to 0

public StepTracker(int m) {
    minActive = m;
    days = 0;
    activeDays = 0;
    totalSteps = 0;
}

active days method: accessor method

public int activeDays() {
    return activeDays; 
}

average steps method: returns the average number of steps per day

public double averageSteps() {
    if (days == 0) {
        return 0.0;
    }
    return (double) totalSteps / days;
}

method to record number of steps per day

  • increments number of days
  • increments total steps by steps
  • if steps is greater than minActive increment days active
public void addDailySteps(int steps) {
    days++;
    totalSteps += steps;
    if (steps >= minActive) {
        activeDays++;
    }
}