Unit 2 Using Objects
Unit 2 Using Objects
Java is an object-oriented programming language.
Everything in Java is associated with classes and objects, along with its attributes and methods.
An object is a member / instance of a Java class.
A class is a template or blueprint from which objects are created.
An object is the instance of a class.
Java has 8 primitive data types/ differet types of objects: the byte, short, int, long, float, double, char and boolean.
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("Lightning McQueen");
goblin1.setHP(12);
goblin1.setDMG(2);
goblin1.setHitChance(0.5);
Goblin goblin2 = new Goblin();
goblin2.setName("Mater");
goblin2.setHP(4);
goblin2.setDMG(1);
goblin2.setHitChance(0.75);
fight(goblin1, goblin2);
}
}
Duel.main(null);
Concatenation: The combination of two or more strings together by using the + operator.
String a = "Chicken";
String b = "Duck";
String c = "Goose";
String d = a + " " + b + " " + c;
System.out.println(d);
Math Class: Provides capailites of the utilization of Math operators.
double x = 6;
double y = 9;
int nice = (int) (y/x);
int brain = (int) (x*y);
System.out.println(nice);
System.out.println(brain)
Comparison of Numbers: Numbers can be compared when the operator "==" is used, however ".string()" must be used to compare strings
int a = 3;
int b = 90;
System.out.println(a == b);