Clsss Notes
oop is programing paradigm that organizes software design around bjects
data and functions tha toperate are bound together
- classes are a template from white objects are creates
objects under the ssame class wil share common class
an object is initiallized by calling a class constructor there could be parameters
- method declaration consists of 6 components
- access modigfier: specifies which parts of the prgram that you can call from
- return type: the data type returned
- defining and callingmethods allows for code reuse, optiminzation, and organization
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) {
if (newHitChance >= 1){
this.hitChance = (1/newHitChance);
}
else {
this.hitChance = newHitChance;
}
}
}
import java.lang.Math;
import java.util.Scanner;
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("Steelb");
goblin1.setHP(12);
goblin1.setDMG(2);
goblin1.setHitChance(0.50);
Goblin goblin2 = new Goblin();
goblin2.setName("Gunther");
goblin2.setHP(4);
goblin2.setDMG(1);
goblin2.setHitChance(24);
Goblin goblin3 = new Goblin();
goblin3.setName("Wraqe");
goblin3.setHP(4);
goblin3.setDMG(1);
goblin3.setHitChance(24);
Goblin goblin4 = new Goblin();
goblin4.setName("Blilk");
goblin4.setHP(4);
goblin4.setDMG(1);
goblin4.setHitChance(24);
Goblin goblin5 = new Goblin();
goblin5.setName("Vylme");
goblin5.setHP(4);
goblin5.setDMG(1);
goblin5.setHitChance(24);
Goblin goblin6 = new Goblin();
goblin6.setName("Ral");
goblin6.setHP(4);
goblin6.setDMG(1);
goblin6.setHitChance(24);
System.out.println("1." + goblin1.getName());
System.out.println("2." + goblin2.getName());
System.out.println("3." + goblin3.getName());
System.out.println("4." + goblin4.getName());
System.out.println("5." + goblin5.getName());
System.out.println("6." + goblin6.getName());
System.out.println("Choose your two fighters by number (1-6) ");
Scanner userInput = new Scanner(System.in);
String gobby1 = userInput.nextLine();
Scanner userInput2 = new Scanner(System.in);
String gobby2 = userInput2.nextLine();
fight(goblin2, goblin3);
}
}
Duel.main(null);
public class Person {
private String name;
private int age;
private int weight;
private int height;
public Person(String name) {
this(name, 0, 0, 0);
}
public Person(String name, int age, int height, int weight) {
this.name = name;
this.age = age;
this.weight = weight;
this.height = height;
}
// other constructors and methods
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public int getHeight() {
return this.height;
}
public void growOlder() {
this.age = this.age + 1;
}
public void setHeight(int newHeight) {
this.height = newHeight;
}
public void setWeight(int newWeight) {
this.weight = newWeight;
}
public double bodyMassIndex() {
double heightPerHundred = this.height / 100.0;
return this.weight / (heightPerHundred * heightPerHundred);
}
@Override
public String toString() {
return this.name + ", age " + this.age + " years";
}
public static void main(String[] args){
Person Bria = new Person("Bria Gilliam");
System.out.println(Bria);
for (int i=0; i<=17; i++){
Bria.growOlder();
}
System.out.println(Bria);
}
}
Person.main(null);