Work

Name: Bailey Say

Unit 5: Writing Classes

  • Class Declaration
  • Class Body
    • Attributes
    • Methods
      • Constructor
      • Setters
      • Getters
  • Modifiers
    • public, private, protected, static

Unit 9: Java Inheritance

  • Parent and child classes
    • extends keyword
  • Accessing inherited methods
    • super()
    • Note: Constructors are never inherited from parent classes; if no constructor is provided then default constructor is provided that just calls super() with no parameters
  • Polymorphism
    • Overloading - same function defined with different params
    • Overriding - function changed in child class

Unit 10: Recursion

  • Function calling itself repeatedly
  • Breaks down problem into smaller and smaller parts

Hacks

Unit 5

public class Hamburger {

    public String bun;
    private String meat;
    public static int count = 0;

    Hamburger(String bun, String meat) {
        this.bun = bun;
        this.meat = meat;
        count++;
    }

    private static void makeBurger() {
        count++;
    }

    public void sayHello() {
        System.out.print("Hello. I am: ");
        System.out.println(this.toString());
    }

    public String toString() {
        return("Bun: " + this.bun + ", Meat: " + this.meat);
    }

    public void setMeat(String meat) {
        this.meat = meat;
    }

    public String getMeat() {
        return this.meat;
    }

    public static void main(String[] args) {
        Hamburger one_bu = new Hamburger("Brioche", "Pig");
        one_bu.sayHello();

        one_bu.setMeat("Donkey");
        System.out.println(one_bu.getMeat());
    }
}

Hamburger.main(null);
Hello. I am: Bun: Brioche, Meat: Pig
Donkey

Unit 9

class Vehicle {

    protected String name;

    // methods and attributes
    Vehicle(String name) {
        this.name = name;
    }

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

}

class Car extends Vehicle {

    // methods and attributes
    Car(String name) {
        super(name);
    }

}

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

Tester.main(null);
Honk!
Toyota
public class Hamburger {

    public String bun;
    private String meat;
    public static int count = 0;

    Hamburger(String bun, String meat) {
        this.bun = bun;
        this.meat = meat;
        count++;
    }

    private static void makeBurger() {
        count++;
    }

    public void sayHello() {
        System.out.println("Hello. I am a hamburger.");
    }

    public String toString() {
        return("Bun: " + this.bun + ", Meat: " + this.meat);
    }

    public void setMeat(String meat) {
        this.meat = meat;
    }

    public String getMeat() {
        return this.meat;
    }

}

public class Cheeseburger extends Hamburger {

    Cheeseburger(String bun, String meat) {
        super(bun, meat);
    }

    @Override
    public void sayHello() {
        System.out.println("Hello. I am a cheeseburger.");
    }

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

public class Tofuburger extends Hamburger {

    Tofuburger(String bun, String meat) {
        super(bun, meat);
    }

    @Override
    public void sayHello() {
        System.out.println("Hello. I am a tofuburger.");
    }

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

class Tester {
    public static void main(String[] args) {
        Hamburger[] one_bu = {new Cheeseburger("Staple", "Salami"), new Tofuburger("Sourdough", "Alligator")};

        for (Hamburger bu : one_bu) {
            bu.sayHello();
        }
    }
}

Tester.main(null);
Hello. I am a cheeseburger.
Hello. I am a tofuburger.

Unit 10

public static class Fibonacci {

    public static int calculate(int n) {
        if (n == 1) {
            return 0;
        } else if (n == 2) {
            return 1;
        } else {
            return calculate(n - 1) + calculate(n - 2);
        }
    }
}

public class Tester {
    public static void main(String[] args) {
        System.out.println(Fibonacci.calculate(5));
        System.out.println(Fibonacci.calculate(14));
        System.out.println(Fibonacci.calculate(20));
    }
}

Tester.main(null);
3
233
4181