OOP

  • Programming paradigm around objects
  • Compartmentalize data and function in such a way that data and the functions that operate on the data are bound together ## Classes
  • Classes are a template
  • Objects in the same class will share common methods and attributes
  • Fruit example ## Creating an Object
  • Members methods constructors
  • Initialized by calling the class constructor
  • Access modifier
  • return type
  • method name
  • parameter list
  • exception list
  • method body
Painter mypainter = new Painter();

Example of declaring methods

public int max (int x, int y){
    if(x>y)
        return x;
    else
        return y;
}

Calling Methods

  • Allows code reuse for optimization and organization
  • Remember semicolons
  • objectReference.methodName(parameters);
  • Import math class to use the functions in that class

Vocab

  • Signature: constructor name and list of parameters
  • Overloaded constructor: multiple constructors with same name but different parameters
  • No arg constructor: no arguments (parameters) have been inputted into a method

Void

  • Don't return anything
  • uses null reference to call

Non void

  • Return the type of data requested in the signature of the method

Homework

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

    public String getName() {
        return name;
    }

    public int getHP() {
        return HP;
    }

    public int getDMG() {
        return DMG;
    }

    public double getHitChance() {
        return hitChance;
    }

    public double getCriticalHitChance(){
        return criticalHitChance;
    }

    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;
    }

    public void setCriticalHitChance(double newCriticalHitChance) {
        this.criticalHitChance = newCriticalHitChance;
    }
}
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()) {
            if (Math.random() < attackerGoblin.getCriticalHitChance()){
                attackeeGoblin.takeDMG(2*attackerGoblin.getDMG());
                System.out.println(attackerGoblin.getName() + " lands a critical hit!");
                System.out.println(attackeeGoblin.getName() + " takes " + 2*attackerGoblin.getDMG() + " damage");
            }
            else{
                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 perished");
                break;
            }

            attack(goblin2, goblin1);

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

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

        Goblin goblin2 = new Goblin();
        goblin2.setName("Gunther the great");
        goblin2.setHP(4);
        goblin2.setDMG(1);
        goblin2.setHitChance(1);
        goblin1.setCriticalHitChance(0.5);

        fight(goblin1, goblin2);
    }
}

Duel.main(null);
jeffrey attacks Gunther the great!
jeffrey misses...
Gunther the great HP: 4

Gunther the great attacks jeffrey!
Gunther the great hits!
jeffrey takes 1 damage
jeffrey HP: 11

jeffrey attacks Gunther the great!
jeffrey hits!
Gunther the great takes 2 damage
Gunther the great HP: 2

Gunther the great attacks jeffrey!
Gunther the great hits!
jeffrey takes 1 damage
jeffrey HP: 10

jeffrey attacks Gunther the great!
jeffrey misses...
Gunther the great HP: 2

Gunther the great attacks jeffrey!
Gunther the great hits!
jeffrey takes 1 damage
jeffrey HP: 9

jeffrey attacks Gunther the great!
jeffrey lands a critical hit!
Gunther the great takes 4 damage
Gunther the great HP: -2

Gunther the great attacks jeffrey!
Gunther the great hits!
jeffrey takes 1 damage
jeffrey HP: 8

Gunther the great has perished