• OOP

    • programming paradigm that organizes software design
  • Classes and objects

    • classes : templates/ blueprint where objects are created
    • objects: instances of a class where specific objects are made from it
    • methods: functions that do a specific task assigned
    • changing one object wont change another
  • Constructors
    • initialize attribute for an object

New vocab

Wrapper Classes

  • A wrapper class in Java is a class that contains or “wraps” primitive data types as an object
  • Wrapper classes provide static methods that allow you to perform some basic number operations, such as converting data from a string to a number
  • Creating wrapper objects, such as Integer and Double objects can be done in a similar way to other objects. The constructor takes a single value and converts it into the object version of that value
  • Importance:
    • Allows null values
    • Can be used in collection such as List, Map, etc.
    • Can be used in methods which accepts arguments of Object type.
    • Can be created like Objects using new ClassName() like other objects:
    • Makes available all the functions that Object class has such as clone(), equals(), hashCode(), toString() etc. Example: Integer y = new Integer(17); Double z = new Double(3.14);

Concatenation

  • mainly used to add two strings together Ex: System.out.println(Ellie + "went to the park with" + Charlie) rules: The concatenation character is mandatory when:
  • An alphanumeric character follows a variable symbol.
  • A left parenthesis that does not enclose a subscript follows a variable symbol.
  • A period (.) is to be generated. Two periods must be specified in the concatenated string following a variable symbol.
  • The concatenation character is not required when:
  • An ordinary character string precedes a variable symbol.
  • A special character, except a left parenthesis or a period, is to follow a variable symbol.
  • A variable symbol follows another variable symbol.
  • A variable symbol is used with a subscript. The concatenation character must not be used between a variable symbol and its subscript; otherwise, the characters are considered a concatenated string and not a subscripted variable symbol.

Math Class

  • The Math class is a class that only contains static methods that are used for specific purposes
  • since the math class contains only static methods, you do not need to instantiate an object to use it. Static methods are called using the dot operator along with the class name. Ex: int x = Math.abs(-5); takes absolute value of negative 5
  • The random method takes no input and returns a random number from zero to one, inclusive of zero, but not inclusive of one. Ex: double ranNum = Math.random() * 10; this takes a random value from 0 to 10
  • You must include the multiplier inside parentheses along with the random call. If you do not, you will end up casting only the Math.random() part, and since it is always less than one, your random function will always result in zero.

Wrapper class tangibles

int x = 222;
ArrayList<Integer> list = new ArrayList<Integer>();
Integer X = new Integer(x);
list.add(x);
list.add(x);
System.out.println(list);
[222, 222]

Concatenation tangibles

String x = "Ellie";
String y = " has a dog";

System.out.println(x + y);
Ellie has a dog

Math class tangible

int ranNum = (int)Math.random() * 25 + 50;
System.out.println(ranNum);
50

2006 FRQ

2a

public double purchasePrice () {
    double purchasPrice; 
    purchasePrice = getListprice * rate;
    return purchasePrice;
}

3a

public int compareCustomer(Customer other){
    int nameCompare = getName().compareTo(other.getName());
    
    if (nameCompare != 0) {
     return nameCompare;
    }

    else {
     return getID();
    } 
   }

Goblin Fight

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 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("bob");
        goblin1.setHP(12);
        goblin1.setDMG(2);
        goblin1.setHitChance(0.50);

        Goblin goblin2 = new Goblin();
        goblin2.setName("joe the great");
        goblin2.setHP(4);
        goblin2.setDMG(1);
        goblin2.setHitChance(1);

        fight(goblin1, goblin2);
    }
}

Duel.main(null);
bob attacks joe the great!
bob misses...
joe the great HP: 4

joe the great attacks bob!
joe the great hits!
bob takes 1 damage
bob HP: 11

bob attacks joe the great!
bob misses...
joe the great HP: 4

joe the great attacks bob!
joe the great hits!
bob takes 1 damage
bob HP: 10

bob attacks joe the great!
bob hits!
joe the great takes 2 damage
joe the great HP: 2

joe the great attacks bob!
joe the great hits!
bob takes 1 damage
bob HP: 9

bob attacks joe the great!
bob misses...
joe the great HP: 2

joe the great attacks bob!
joe the great hits!
bob takes 1 damage
bob HP: 8

bob attacks joe the great!
bob misses...
joe the great HP: 2

joe the great attacks bob!
joe the great hits!
bob takes 1 damage
bob HP: 7

bob attacks joe the great!
bob misses...
joe the great HP: 2

joe the great attacks bob!
joe the great hits!
bob takes 1 damage
bob HP: 6

bob attacks joe the great!
bob hits!
joe the great takes 2 damage
joe the great HP: 0

joe the great attacks bob!
joe the great hits!
bob takes 1 damage
bob HP: 5

joe the great has perished