OOP Lesson Hacks
Hacks from the presentation
import java.util.*;
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;
}
}
public class Duel {
public static void fight(Goblin goblin1, Goblin goblin2) {
while (goblin1.isAlive() && goblin2.isAlive()) {
// goblin1 hit chance tester
if (Math.random() < goblin2.getHitChance()) {
goblin2.takeDMG(goblin2.getDMG());
System.out.println(goblin1.getName() + " takes " + goblin2.getDMG() + " damage");
}
else {
System.out.println(goblin2.getName() + " missed!");
}
// print hp of goblin1
System.out.println(goblin1.getName() + " HP: " + goblin1.getHP());
if (!goblin1.isAlive()) {
System.out.println(goblin1.getName() + " has perished");
break;
}
// if statement for goblin2 hit chance
if (Math.random() < goblin1.getHitChance()) {
goblin2.takeDMG(goblin1.getDMG());
System.out.println(goblin2.getName() + " takes " + goblin1.getDMG() + " damage");
}
else {
System.out.println(goblin1.getName() + " missed!");
}
// print hp of goblin2
System.out.println(goblin2.getName() + " HP: " + goblin2.getHP());
if (!goblin2.isAlive()) {
System.out.println(goblin2.getName() + " has perished");
break;
}
}
}
public static void main(String[] args) {
Goblin goblin1 = new Goblin();
goblin1.setName("Thomas Jefferson");
goblin1.setHP(12);
goblin1.setDMG(2);
goblin1.setHitChance(0.5);
Goblin goblin2 = new Goblin();
goblin2.setName("George Washington");
goblin2.setHP(4);
goblin2.setDMG(1);
goblin2.setHitChance(0.75);
fight(goblin1, goblin2);
}
}
Duel.main(null);
// #1
public class WordMatch {
String secret;
public WordMatch(String secret) {
this.secret = secret;
}
public void scoreGuess(String guess) {
int counter = 0;
// for loop for first substring index
for (int i = 0; i < this.secret.length(); i++){
// loop for second substring index
for (int j = i + 1; j < this.secret.length() + 1; j++) {
// if statement
if (guess.equals(this.secret.substring(i,j))) {
counter++;
}
}
}
// Returning a point value
int points = counter * guess.length() * guess.length();
System.out.println("" + guess + " = " + points);
// return points;
}
}
WordMatch game = new WordMatch("mississippi");
game.scoreGuess("i"); game.scoreGuess("iss"); game.scoreGuess("issipp"); game.scoreGuess("mississippi");
WordMatch game = new WordMatch("aaaabb");
game.scoreGuess("a"); game.scoreGuess("aa"); game.scoreGuess("aaa"); game.scoreGuess("aabb"); game.scoreGuess("c");
Vocab in this unit
- Creating a class - Classes are capitalized, while methods should be camelcase (lowercase first then capitalize beginning of each word after). Should include a constructor and then any other methods that are necessary.
- Mutator Methods - Seen in the Goblin class above, a mutator method is a public method that changes the attributes of the object by running the method with a parameter, this is like a setter as it is responsible for changing a variable.
- Accessor Methods - Seen in the Goblin class above, an accessor method is a public method that returns the value of an attribute of the object, this is like a getter as it is responsible for returning a variable.
- Static methods - Seen in the Duel class above, static methods are not in the instance of an object but rather are accessed by each object. They are used to perform a task that is not specific to an object.
- Access modifiers - Private is used to hide an attribute or method to not be accessible by anything outside of the class itself. Public is used to make an attribute or method accessible by everything outside of the class. Protected is used to make an attribute or method accessible by anything outside of the class itself and any subclasses. This is seen in the various attributes of the Goblin class.
- Nested loops - Seen in WordMatch.scoreGuess(), nested loops can be used to compare multiple values of two lists, or iterate through multidimensional arrays. In this case, the nested for loop is used to iterate through the secret word and the guess.
- Comparing Strings - Also seen in WordMatch.scoreGuess(), the iteration compare each letter of the word and gives points for correctness.
- Main/Tester Methods - The main program is intended to be run as a tester for the rest of the class. It is used to test the methods of the class and make sure they are working properly. In this case, the main method of Duel is used to test the Duel class with predefined values.
- Math.random() - This allows the creation of a random float from 0 to 1, and then can be multiplied to create a certain random integer value. This is used in the Duel class to try and randomly have a critical hit while still allowing for a predefined chance of a critical hit.