Homework: Ultimate Battle
Scenario
You are programming a game where 2 objects of your choosing (e.g., Robots, Dinosaurs, People) battle. Each object has health and power.
Instructions
-
Create a class representing your object with:
Instance variables:
String nameint powerint healthMAKE YOUR OWN
Static variables:
double fightDuration
Instance methods:
void attack()void printStatus()MAKE YOUR OWN
Class methods:
static int strongerFighter()static void beginBattle()MAKE YOUR OWN
-
In
main:- Create two objects.
- Use instance methods to attack and print status.
- Use static methods to compare, print a fact, and start the battle.
class Warrior {
String name;
int power;
int health;
String weapon;
static double fightDuration = 3.5;
public Warrior(String name, int power, int health, String weapon) {
this.name = name;
this.power = power;
this.health = health;
this.weapon = weapon;
}
public void attack(Warrior enemy) {
System.out.println(name + " attacks " + enemy.name + " with " + weapon + "!");
enemy.health -= this.power;
if (enemy.health < 0) enemy.health = 0;
}
public void printStatus() {
System.out.println(name + " → Health: " + health + ", Power: " + power + ", Weapon: " + weapon);
}
public void heal(int amount) {
health += amount;
System.out.println(name + " healed for " + amount + " points!");
}
public static Warrior strongerFighter(Warrior w1, Warrior w2) {
if (w1.power > w2.power) {
System.out.println(w1.name + " is the stronger fighter!");
return w1;
} else if (w2.power > w1.power) {
System.out.println(w2.name + " is the stronger fighter!");
return w2;
} else {
System.out.println("They are equally strong!");
return null;
}
}
public static void beginBattle(Warrior w1, Warrior w2) {
System.out.println("\n⚔️ The battle between " + w1.name + " and " + w2.name + " begins!");
System.out.println("Estimated duration: " + fightDuration + " minutes\n");
while (w1.health > 0 && w2.health > 0) {
w1.attack(w2);
if (w2.health <= 0) break;
w2.attack(w1);
}
if (w1.health > 0)
System.out.println("\n🏆 " + w1.name + " wins the battle!");
else
System.out.println("\n🏆 " + w2.name + " wins the battle!");
}
public static void funFact() {
System.out.println("Fun Fact: Every battle lasts around " + fightDuration + " minutes!");
}
public static void main(String[] args) {
Warrior robot = new Warrior("RoboMax", 30, 100, "Laser Blaster");
Warrior dino = new Warrior("Rexzilla", 25, 120, "Tail Whip");
robot.printStatus();
dino.printStatus();
System.out.println();
strongerFighter(robot, dino);
funFact();
beginBattle(robot, dino);
}
}
Warrior.main(null);
RoboMax → Health: 100, Power: 30, Weapon: Laser Blaster
Rexzilla → Health: 120, Power: 25, Weapon: Tail Whip
RoboMax is the stronger fighter!
Fun Fact: Every battle lasts around 3.5 minutes!
⚔️ The battle between RoboMax and Rexzilla begins!
Estimated duration: 3.5 minutes
RoboMax attacks Rexzilla with Laser Blaster!
Rexzilla attacks RoboMax with Tail Whip!
RoboMax attacks Rexzilla with Laser Blaster!
Rexzilla attacks RoboMax with Tail Whip!
RoboMax attacks Rexzilla with Laser Blaster!
Rexzilla attacks RoboMax with Tail Whip!
RoboMax attacks Rexzilla with Laser Blaster!
🏆 RoboMax wins the battle!
Popcorn Hack
class Bro {
String name;
static String catchphrase = "Sup bro?";
public Bro(String name) {
this.name = name;
}
public void sayHi() {
System.out.println("Hey, I'm " + name);
}
public static void sayCatchphrase() {
System.out.println(catchphrase);
}
public static void main(String[] args) {
sayCatchphrase();
Bro alex = new Bro("Alex");
alex.sayHi();
alex.sayCatchphrase();
Bro.sayCatchphrase();
}
}
Bro.main(null);
Sup bro?
Hey, I'm Alex
Sup bro?
Sup bro?