Unit 5 Lesson: Hack (Putting it all together)

class Pie {
    private static int numPies = 0;
    private String type;

    public Pie(String type) {
        this.type = type;
        numPies++;
    }

    public static int getNumPies() {
        return numPies;
    }
    
   private String getPieType() {
           return type;
     }

    public static void main(String[] args) {
        Pie pie1 = new Pie("Apple Pie");
        Pie pie2 = new Pie("Cherry Pie");
        Pie pie3 = new Pie("Peach Pie");

        System.out.println("Types of Pies Count: " + Pie.getNumPies());
        System.out.println("Type of Pie 1: " + pie1.getPieType());
    }
}
Pie.main(null);
Types of Pies Count: 3
Type of Pie 1: Apple Pie

Unit 9 Lesson:

Hack 1 (Superclass/Subclass Practice)

class Vehicle {
    private String name;

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

    public String getName() {
        return this.name;
    }

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

}

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

    public static void main(String[] args) {
        Car Toyota = new Car("Toyota");

        System.out.println(Toyota.getName());
        Toyota.honk();
    }
}
Car.main(null);
Toyota
honk!

Hack 2 (Polymorphism, Super, Override)

public class Food {
    private String name; 

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

    public String getName() {
        return this.name;
    }

    public String toString() {
        return ("Food Name: " + this.getName());
    }
}

public class SpicyFood extends Food {
    private int spiceLevel = 9;

    public SpicyFood(String name) {
        super(name);
    }

    // override superclass method
    public String toString() {
        return(super.toString() + ", spiceLevel: " + this.spiceLevel);
    }
}

public class SweetFood extends Food {
    private int spiceLevel = 2;

    public SweetFood(String name) {
        super(name);
    }

    // override superclass method
    public String toString() {
        return(super.toString() + ", spiceLevel: " + this.spiceLevel);
    }
}

public class Tester {
    public static void main(String[] args) {
        Food goldenRetriever = new SpicyFood("Hot Wings");
        System.out.println(goldenRetriever.toString());
        Food ostrich = new SweetFood("Ice Cream");
        System.out.println(ostrich.toString());
    }
}


Tester.main(null);
Food Name: Hot Wings, spiceLevel: 9
Food Name: Ice Cream, spiceLevel: 2

Unit 10 Lesson: Hack (Recursive Algorithm)

public class Main {
    public static void main(String[] args) {
      int result = sum(10);
      System.out.println("The sum of all the numbers is: " + result);
    }
    public static int sum(int k) {
      if (k > 0) {
        return k + sum(k - 1);
      } else {
        return 0;
      }
    }
  }
Main.main(null);
The sum of all the numbers is: 55