Calling Class Methods Homework
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.
public class Robot {
private String name;
private int power;
private int health;
private String weapon;
private static double fightDuration = 0.0;
public Robot(String name, int power, int health, String weapon) {
this.name = name;
this.power = power;
this.health = health;
this.weapon = weapon;
}
public void attack(Robot target) {
System.out.println(this.name + " attacks " + target.name + " with " + this.weapon + "!");
target.health -= this.power;
if (target.health < 0) target.health = 0;
fightDuration += 2.5;
}
public void printStatus() {
System.out.println(name + " | Power: " + power + " | Health: " + health + " | Weapon: " + weapon);
}
public void repair() {
health += 15;
System.out.println(name + " repairs itself and regains 15 health!");
}
public static int strongerFighter(Robot r1, Robot r2) {
if (r1.power > r2.power) return 1;
else if (r2.power > r1.power) return 2;
else return 0;
}
public static void beginBattle(Robot r1, Robot r2) {
System.out.println("⚔️ The Ultimate Battle Begins Between " + r1.name + " and " + r2.name + "! ⚔️");
fightDuration = 0.0;
while (r1.health > 0 && r2.health > 0) {
r1.attack(r2);
if (r2.health > 0) r2.attack(r1);
}
System.out.println("\nBattle Over!");
if (r1.health > 0)
System.out.println(r1.name + " wins with " + r1.health + " health left!");
else if (r2.health > 0)
System.out.println(r2.name + " wins with " + r2.health + " health left!");
else
System.out.println("Both fighters have fallen!");
System.out.printf("Total fight duration: %.1f seconds%n", fightDuration);
}
public static void main(String[] args) {
Robot alpha = new Robot("AlphaX", 25, 100, "Laser Cannon");
Robot beta = new Robot("BetaBot", 20, 120, "Plasma Blade");
System.out.println("=== Initial Status ===");
alpha.printStatus();
beta.printStatus();
System.out.println("\n=== Power Comparison ===");
int result = strongerFighter(alpha, beta);
if (result == 1) System.out.println(alpha.name + " is stronger!");
else if (result == 2) System.out.println(beta.name + " is stronger!");
else System.out.println("They are equally matched!");
System.out.println("\n=== Battle Start ===");
beginBattle(alpha, beta);
}
}
Robot.main(null);
=== Initial Status ===
AlphaX | Power: 25 | Health: 100 | Weapon: Laser Cannon
BetaBot | Power: 20 | Health: 120 | Weapon: Plasma Blade
=== Power Comparison ===
AlphaX is stronger!
=== Battle Start ===
⚔️ The Ultimate Battle Begins Between AlphaX and BetaBot! ⚔️
AlphaX attacks BetaBot with Laser Cannon!
BetaBot attacks AlphaX with Plasma Blade!
AlphaX attacks BetaBot with Laser Cannon!
BetaBot attacks AlphaX with Plasma Blade!
AlphaX attacks BetaBot with Laser Cannon!
BetaBot attacks AlphaX with Plasma Blade!
AlphaX attacks BetaBot with Laser Cannon!
BetaBot attacks AlphaX with Plasma Blade!
AlphaX attacks BetaBot with Laser Cannon!
Battle Over!
AlphaX wins with 20 health left!
Total fight duration: 22.5 seconds
Popcorn Hack: Which is an instance method or static method
What to do
You’ve learned that static methods belong to the class and instance methods belong to the object.
Look at the code below and decide which lines will work — and which will cause an error.
Then fix the wrong ones.
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) {
// Works — static method called correctly
Bro.sayCatchphrase();
// sayHi(); → must be called on an object
Bro alex = new Bro("Alex");
// Works — instance method called on object
alex.sayHi();
// Works — static method can be called this way (but class name is preferred)
alex.sayCatchphrase();
}
}
Bro.main(null);
Sup bro?
Hey, I'm Alex
Sup bro?