What is OOP

  • OOP is object oriented programming
  • programming pardigm centered around objects

Classes and Objects

  • classes are templates or a blueprint, contatin data
  • objects are instances of a class, need to be instantiated
  • methods are functions of a class
  • Method Declaration consists of 6 components: Access modifer, Return type, Method name, Parameter list, Exception list, Method body
  • Calling a method allows for code reuse, optimization, and organization To call an object's method you have to make an object reference

HW

Goblin Game

public class Goblin {
    private String name;
    private int HP;
    private int DMG;
    private double hitChance;

    public String getName() {
        return name;
    }

    public int getHP() {
        return HP;
    }

    public int getDMG() {
        return DMG;
    }

    public double getHitChance() {
        return hitChance;
    }

    public boolean isAlive() {
        if (this.HP > 0) {
            return true;
        } else {
            return false;
        }
    }

    public void setName(String newName) {
        this.name = newName;
    }

    public void setHP(int newHP) {
        this.HP = newHP;
    }

    public void takeDMG(int takenDamage) {
        this.HP -= takenDamage;
    }

    public void setDMG(int newDMG) {
        this.DMG = newDMG;
    }

    public void setHitChance(double newHitChance) {
        this.hitChance = newHitChance;
    }
}
import java.lang.Math;

public class Duel {

    public static void attack(Goblin attackerGoblin, Goblin attackeeGoblin) {

        System.out.println(attackerGoblin.getName() + " attacks " + attackeeGoblin.getName());
        if (Math.random() < attackerGoblin.getHitChance()) {
            attackeeGoblin.takeDMG(attackerGoblin.getDMG());
            System.out.println(attackerGoblin.getName() + " hits");
            System.out.println(attackeeGoblin.getName() + " takes " + attackerGoblin.getDMG() + " damage");
        } else {
            System.out.println(attackerGoblin.getName() + " misses");
        }

        System.out.println(attackeeGoblin.getName() + " HP: " + attackeeGoblin.getHP());
        System.out.println();
    }

    public static void fight(Goblin goblin1, Goblin goblin2) {
        while (goblin1.isAlive() && goblin2.isAlive()) {
            
            attack(goblin1, goblin2);

            if (!goblin1.isAlive()) {
                System.out.println(goblin1.getName() + " has died");
                break;
            }

            attack(goblin2, goblin1);

            if (!goblin2.isAlive()) {
                System.out.println(goblin2.getName() + " has died");
                break;
            }
        }
    }

    public static void main(String[] args) {
        Goblin goblin1 = new Goblin();
        goblin1.setName("bob");
        goblin1.setHP(12);
        goblin1.setDMG(2);
        goblin1.setHitChance(0.8);

        Goblin goblin2 = new Goblin();
        goblin2.setName("rob");
        goblin2.setHP(4);
        goblin2.setDMG(1);
        goblin2.setHitChance(0.8);

        fight(goblin1, goblin2);
    }
}

Duel.main(null);
bob attacks rob
bob hits
rob takes 2 damage
rob HP: 2

rob attacks bob
rob misses
bob HP: 12

bob attacks rob
bob hits
rob takes 2 damage
rob HP: 0

rob attacks bob
rob hits
bob takes 1 damage
bob HP: 11

rob has died