Unit 5 Writing Classes

Blue print of writing classes, constructors, mutators/accessors, etc.

Stores data in the for of variables or has functions which are used to execute logic.

Access Modifiers

Inheritance

Inheriting properties of super classes and uses keyword extends. super references to super class while this refers to the instance itself.

Constructors are never inherited, thus you need to generate your own constructors and use super(); in order to call super class constructor.

Important things to know for ap exam:

  • can add new private instance variables
  • can have new methods (any modifier basically)
  • can override inherited methods through (@Override)
  • cant redefine public method as private
  • cant override static methods of the superclass
  • could define its own constructor
  • cannot directly access private members of super class and must use accessor/mutator

Homework Hacks

Unit 5 Putting It All Together

public class Person{
    static ArrayList<Person> registry = new ArrayList<Person>();
    public String name;
    private long ssn;

    public Person(String name, long ssn){
        this.name = name;
        this.ssn = ssn;

        registry.add(this);
    }

    public void setSSN(long ssn){
        this.ssn = ssn;
    }

    public long getSSN(){
        return this.ssn;
    }

    public void filmLife(){
        System.out.print("This is: ");
        this.doDailyLife();
    }

    private void doDailyLife(){
        System.out.println("my every day life");
    }

    public void laugh(){
        System.out.println("hahahahaha");
    }
}

public class Main{
    public static void tester(String[] args){
        Person person1 = new Person("John Jackle", Long.valueOf(2232322));
        Person person2 = new Person("Jo Juckle", Long.valueOf(445454545));
        Person person3 = new Person("Jonah Jeckle", Long.valueOf(89787878));

        System.out.println("-----------Getter and Setter-----------");
        person1.setSSN(420420420);
        System.out.println(person1.getSSN());
        
        System.out.println("-----------Public Method-----------");
        person1.laugh();

        System.out.println("-----------Private Method-----------");
        person1.filmLife(); // calls public method that calls private method

        // testing static array list
        System.out.println("-----------Testing Static ArrayList-----------");
        for (int i = 0; i < Person.registry.size(); i++) {
            System.out.println(Person.registry.get(i).name);
        }
    }
}

Main.tester(null);
-----------Getter and Setter-----------
420420420
-----------Public Method-----------
hahahahaha
-----------Private Method-----------
This is: my every day life
-----------Testing Static ArrayList-----------
John Jackle
Jo Juckle
Jonah Jeckle
public class Main{
    public static void tester(String[] args){
        Person person1 = new Person("John Jackle", Long.valueOf(2232322));
        
        System.out.println("-----------Private Method Can't Be Accessed-----------");
        person1.doDailyLife(); // calls public method that calls private method
    }
}

Main.tester(null);
|           person1.doDailyLife(); // calls public method that calls private method
doDailyLife() has private access in Person

Unit 9 Hack 1 (Superclass/Subclass Practice)

public class Vehicle{
    String name;

    public Vehicle(String name){
        this.name = name;
    }

    public void honk(){
        System.out.println("honk honk");
    }
}

public class Car extends Vehicle{
    public Car(String name){
        super(name);
    }

    public static void main(String[] args){
        Vehicle car = new Car("Toyota Mirai");
        car.honk();
        System.out.println(car.name);
    }
}

Car.main(null);
honk honk
Toyota Mirai

Unit 9 Hack 2 (Polymorphism, Super, Override)

// NOTE: above class Person from unit 5 is used to inherit since it has methods and attributes i can use to demonstrate this

public class Student extends Person {
    int gradeLevel;
    String schoolGrade;

    public Student(String name, long ssn, int gradeLevel, String schoolGrade){
        super(name, ssn);
        this.gradeLevel = gradeLevel;
        this.schoolGrade = schoolGrade;
    }

    public void study(){
        System.out.println("study, study, study");
    }

    @Override
    public void laugh(){
        System.out.println("hehehehehe");
    }
}

public class Teacher extends Person {
    int yearsTeaching;
    String subject;
    
    public Teacher(String name, long ssn, int yearsTeaching, String subject){
        super(name, ssn);
        this.yearsTeaching = yearsTeaching;
        this.subject = subject;
    }

    public void teach(){
        System.out.println("teach, teach, teach");
    }

    @Override
    public void laugh(){
        System.out.println("hahahahehe");
    }
}

public class Main {
    public static void tester(String[] args){
        Person person = new Person("Bob Gob", 432432432);
        Student student = new Student("Joe Joe", 234234234, 11, "A");
        Teacher teacher = new Teacher("Mr. M", 11111111, 4, "Computer Science");

        System.out.println("------------Demonstrations of Overrides-------------");
        System.out.print("Person (parent class): ");
        person.laugh();
        System.out.print("Student: ");
        student.laugh();
        System.out.print("Teacher: ");
        teacher.laugh();

        System.out.println("------------Demonstrations of New Methods-------------");
        student.study();
        teacher.teach();
    }
}

Main.tester(null);
------------Demonstrations of Overrides-------------
Person (parent class): hahahahaha
Student: hehehehehe
Teacher: hahahahehe
------------Demonstrations of New Methods-------------
study, study, study
teach, teach, teach

Unit 10 Hack (Recursive Algorithm)

public class Fibonacci{
    public static int fibonacci(int n){
        if (n <= 1){
            return n;
        }

        return fibonacci(n-1) + fibonacci(n-2);
    }

    public static void tester(String[] args){
        for( int i = 0; i < 20; i++){
            System.out.println(fibonacci(i) + " ");
        }
    }
}

Fibonacci.tester(null);
0 
1 
1 
2 
3 
5 
8 
13 
21 
34 
55 
89 
144 
233 
377 
610 
987 
1597 
2584 
4181