• class plan for create objects
  • object is the instances of a class
  • public mens anyone can access while private means that there is restricted access
  • constructors - initialize instance variables when object is called
  • set an initial object state and initial instance variables

There are 3 steps to creating and calling methods:

1) declare object of the class in the main method 2) call the method 3) write the method header and body

Creating a Class

  • creating classes provides templates for creating objects, which can bind code into data
  • has definitions of methods and data
  • Class names should be nouns, in mixed cases with the first letter of each internal word capitalized.
  • Interfaces names should also be capitalized just like class names.
  • Use whole words and must avoid acronyms and abbreviations

Constructors

  • used to create the instance of the class
  • name is same as class name and has no return type and only returns object to class; adding return would make it a method

Accessor Method

  • Accessor methods return the value of a private method
  • getter returns the value of data types using the accessor method (instance method to get or set values of an object)

Mutator Method

  • Mutator methods reset the value of a private variable
  • relates to the setter because it updates the value of the variable
  • void type represents void return type as a class and contains a public value and is assigned the value of null since it isn’t instantiable

  • this keyword refers to current object in a method or constructor

public String getVariable(){
//accessor method
}

public void setVariable(String variable){
    this.variable = variable; //example of mutator method
}

Static & Class Methods and Variables

  • Static vars are also called class variables because they belong to a class and not an instance
  • Static method belongs to a class rather than an instance of class
  • class methods are called on the class itself not a specific object

ACCESS MODIFIERS:

  • Keywords to control visibility of methods and constructors in classes
  • Public means it can be accessed from anywhere
  • Private means only within the same class
  • protected means that it is accessed within the same package and outside with a child class
class Data {
    // private variable
    private String name;
}

public class Main {
    public static void main(String[] main){

        // create an object of Data
        Data d = new Data();

        // access private variable and field from another class
        d.name = "Hello";
    }
} //error because private
Data.main(null);
|           d.name = "Hello";
name has private access in Data
class Animal {
    // protected method
    protected void display() {
        System.out.println("I am an animal");
    }
}

class Dog extends Animal {
    public static void main(String[] args) {

        // create an object of Dog class
        Dog dog = new Dog();
         // access protected method
        dog.display();
    }
}
// public class
public class Animal {
    // public variable
    public int legCount;

    // public method
    public void display() {
        System.out.println("I am an animal.");
        System.out.println("I have " + legCount + " legs.");
    }
}

// Main.java
public class Main {
    public static void main( String[] args ) {
        // accessing the public class
        Animal animal = new Animal();

        // accessing the public variable
        animal.legCount = 4;
        // accessing the public method
        animal.display();
    }
}

Main Method & Tester Method

  • Main contains code to execute and call other methods, usually the entry point to execute Java program
  • Tester method evaluates code to see if it fulfills requirements

Inheritance & Subclasses

  • Method to create hierarchy between classes by inheriting from other classes
  • parent and child classes
  • extends extend a class to show its inherited and extends functionality
  • Subclass inherits all methods and fields from superclass, and constructor can be carried out from subclass
  • super refers to superclass or parent objects
class Animal {
    // methods and fields
  }
  
  // use of extends keyword
  // to perform inheritance
  class Dog extends Animal { //dog subclass
  
    // methods and fields of Animal
    // methods and fields of Dog
  }

Overload & Override

  • Overload happens when there are two methods with same name but different arguments or parameters
  • Method override happens when child class has the same method that is present in a parent class, overwrites base class method

Abstract Class & Method

  • Abstract class is a restricted class that cant be used to create objects and can only access if inherited from other class
  • method can only be used in abstract class and has no implementation
// create an abstract class
abstract class Language {
    // fields and methods
  }
  ...
  
  // try to create an object Language
  // throws an error
  Language obj = new Language();
|     ...
illegal start of expression
abstract class Language {

    // abstract method
    abstract void method1();
  
    // regular method
    void method2() {
      System.out.println("This is regular method");
    }
  }

Late Binding

  • Late binding is that compiler shouldn’t check arguments and no type checks on method call, if method names are mapped at runtime its late

Polymorphism

  • Ability of class to provide different implementations of methods, such as overloading, overriding, late binding

Big O notation

  • How long an algorithm takes to run
  • HashMap has O(1) which is same amount of time to execute
  • Binary Search has O(log N) which means it takes an additional step each time data doubles
  • Single and Nested loop is O(1)

Write the complete StepTracker class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.

public class StepTracker

{
    private int minStepsActive; //instance variable declarations
    private int activeDays;
    private int days;
    private int totalStep;

    public StepTracker(int minStepsActive) //constructors with default parameters
    { 
    this.minStepsActive= minStepsActive;
    activeDays = 0;
    days = 0;
    totalSteps = 0;
    }

    //accessor method
    public int activeDays()
    {
        return activeDays;
    }

    //mutator method
    public void addDailySteps(int steps)
    { 
        if(steps >= minStepsActive)
            activeDays++;
            days++;
            totalSteps += steps;
    }

    //average steps

    public double averageSteps() //used because dividing
    {
        if(days==0)
            return = 0;

        return totalSteps / (double)days;
    }

    public static void main(String[] args)
    {
        StepTracker tr = new StepTracker;
        System.out.println(tr.activeDays())
        tr.addDailySteps(9000);
        tr.addDailySteps(5000);
        System.out.println(tr.activeDays());
        tr.addDailySteps(13000); //only active day because it holds at least 10000 steps
        System.out.println(tr.activeDays());
    }
}