Learnings

  • An object's state are attritbutes of an object and their values at a given time.
  • Static variables and methods belong to a class, not an object.
  • Public classes have no restricted access - has constructors
  • Private classes have restricted access - instance variables
  • Non void methods return a single value

ALL VOCAB SHOWN IN CODE BELOW

Creating a class, describe naming conventions

  • Created using class keyword
  • Should be defined using camelcase (first letter lower all first letters upper)

Main Method

  • Used to test a class
  • Called when class is run
  • Creates an object and test methods usually

This keyword

  • Used to access properties of the class
  • Constructor shows

Constructor

  • Called whenever object is created
  • Usually initialized fields
  • Doesn't return anything

Accessor/Getter methods

  • Used to get properties of an object outside of the class def.
  • Getters can be applied on onlhy the properties which should be accessed outside the class

Mutator/Setter Methods

  • Allow properties to be modified outside of the class definition
  • Have a "void" return type
  • Only set values

Access modifiers

  • Control whether properties and methods can be accessed outside the class
  • Public, private
class MyClass {
  int prop1;
  int prop2;

  // Constructor here
  public MyClass (int prop1input, int prop2input) {
    // setting properties using this to reference prop1 & prop2 of the object
    this.prop1 = prop1input;
    this.prop2 = prop2input;
  }

  // getter allows outside class to access prop1
  public int getProp1() {
    return this.prop1;
  }

  // setter allows outside class to set prop1
  public void setProp1 (int propVal) {
    this.prop1 = propVal;
  }

  public static void main (String[] args) {
    MyClass obj = new MyClass(1, 2);

    // using getter to access prop1
    System.out.println(obj.getProp1());

    // changing value of prop1
    obj.setProp1(10);

    // using getter to access new value of prop1
    System.out.println(obj.getProp1());
  }
}

MyClass.main(null);
1
10

Static methods/properties

  • Do not require an object, only have one instance that is same for all objects
class MyClass {
    // static method
    static String turtle (String a) {
      return a + " TURTLES";
    }
  
    // static property
    static int staticProp = 10;
  
    public static void main(String[] args) {
      // no object needed for any of this
      System.out.println(MyClass.turtle("test"));
      System.out.println(MyClass.staticProp);
    }
  }
  
  MyClass.main(null);
test TURTLES
10
// FRQ Q2 2019

public class StepTracker {
    private int lowerBound;
    private int steps;
    private int totalDays;
    private int activeDays;

    public StepTracker(int minSteps) {
        lowerBound = minSteps;
        steps = 0;
        totalDays = 0;
        activeDays = 0;
    }

    public void addDailySteps(int newSteps) {
        steps += newSteps;
        totalDays += 1;
        if (newSteps >= lowerBound) {
            activeDays += 1;
        }
    }

    public int activeDays(){
        return activeDays;
    }

    public double averageSteps() {
        if (totalDays == 0) {
            return (double) totalDays;
        }
        else {
            return (double) steps / totalDays;
        }
    }

    public static void main(String[] args){

        StepTracker tr = new StepTracker(10000);
        System.out.println(tr.activeDays());
        System.out.println(tr.averageSteps());
        tr.addDailySteps(9000);
        System.out.println(tr.averageSteps());
        tr.addDailySteps(23000);
        System.out.println(tr.activeDays());
    }
}
StepTracker.main(null);
0
0.0
9000.0
1