• Precondition: condition that must be true before execution of a code block Methods don't need to check preconditions
  • Postcondition: condition that is ALWAYS true after the execution of a code block Outcome State of instance variables
-Allows other objects outside of the class to obtain values of instance variables or static variables
-Non-void method returns a single value
-Header includes return type (String, int, Long etc)
"return by value": return expression at the end of a method will specify that value that will be returned (must correspond to the type)
-Must be used when different classes need to access the instance variables
// always public
// must specify return type
// getVariable (typical naming convention)
// no parameters

public String getVariable(){

}
  • Overridden method that provides description of a specific object (ie values of instance variables)
  • If System.out.print or System.out.println() is passed an object, that object's toString() method is called ==> returned string is printed Java default toString() will return a hashcode, but you can manually write your own toString() method

Wrapper Classes:: Wrapper classes allow converting primitives into objects and vice versa, which allows java to be object oriented, as primitives aren’t an object by default

  • Someone may use a wrapper class on int to convert it to an object as they contain many methods to deal with an int value like converting it to a string representation, this is the same for doubles

Math Classes: java math class provides many math methods that allow finding different things, such as a max or min function math.random will return a number between 0.0 and 1.0, or you can multiply it to get 1 to 100 (seen in code)

Example

for (int i = 0; i <5; i++) {
    int randomNum = (int)(Math.random() * 101);
    System.out.println(randomNum);
}

FRQ Answers

public class StepTracker {
   
    private int totalSteps;
    private int minimumSteps;
    private int days;
    private int activeDays;
    
    public StepTracker(int min){
        minimumSteps = min;
        totalSteps = 0;
        days = 0;
        activeDays = 0;
    }

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

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

        else{
            return (double) totalSteps / days;
        }
    }


    public int getActiveDays(){
        return days;
    }

}
public class StepTracker {

    private double Steps = 0;
    private int activeDays = 0;
    private int totalDays = 0;
    private double cutoff = 0;

    public StepTracker (double Steps) {
        this.cutoff = Steps;
    }

    public void addDailySteps (int Steps) {
        this.Steps = this.Steps + Steps;

        if (Steps >= cutoff) {
            this.activeDays++;
        }

        this.totalDays++;

    }

    public int activeDays () {
        return activeDays;
    }

    public double averageSteps() {
        return this.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);
        tr.addDailySteps(5000);
        System.out.println(tr.activeDays());
        System.out.println(tr.averageSteps());
        tr.addDailySteps(13000);
        System.out.println(tr.activeDays());
        System.out.println(tr.averageSteps());
        tr.addDailySteps(23000);
        tr.addDailySteps(1111);
        System.out.println(tr.activeDays());
        System.out.println(tr.averageSteps());
    }
}

StepTracker.main(null);