Skip to the content.

Big Idea 1 Lesson 1.10

Big Idea 1 Lesson 1.10

Homework

public class UltimateBattle {
    // Instance variables
    String name;
    int power;
    int health;

    // Static variable
    static double fightDuration = 0;

    // Constructor
    public UltimateBattle(String name, int power, int health) {
        this.name = name;
        this.power = power;
        this.health = health;
    }

    // Instance method: attack
    public void attack(UltimateBattle opponent) {
        System.out.println(this.name + " attacks " + opponent.name + "!");
        opponent.health -= this.power;
        if (opponent.health < 0) opponent.health = 0;
        fightDuration += 1.5; // pretend each attack takes 1.5 seconds
    }

    // Instance method: printStatus
    public void printStatus() {
        System.out.println(name + " | Power: " + power + " | Health: " + health);
    }

    // Static method: compare who’s stronger
    public static int strongerFighter(UltimateBattle f1, UltimateBattle f2) {
        if (f1.power > f2.power) return 1;
        else if (f2.power > f1.power) return 2;
        else return 0;
    }

    // Static method: beginBattle
    public static void beginBattle(UltimateBattle f1, UltimateBattle f2) {
        System.out.println("=== Battle Begins Between " + f1.name + " and " + f2.name + " ===");
        while (f1.health > 0 && f2.health > 0) {
            f1.attack(f2);
            if (f2.health <= 0) break;
            f2.attack(f1);
        }

        System.out.println("=== Battle Over! ===");
        if (f1.health <= 0 && f2.health <= 0) {
            System.out.println("It’s a draw!");
        } else if (f1.health <= 0) {
            System.out.println(f2.name + " wins!");
        } else {
            System.out.println(f1.name + " wins!");
        }

        System.out.println("Battle lasted " + fightDuration + " seconds.");
    }

    // Main method
    public static void main(String[] args) {
        // Create two fighters
        UltimateBattle robot = new UltimateBattle("Robot Rex", 25, 100);
        UltimateBattle dinosaur = new UltimateBattle("Dino Doom", 30, 90);

        // Print initial status
        robot.printStatus();
        dinosaur.printStatus();

        // Compare who’s stronger
        int stronger = UltimateBattle.strongerFighter(robot, dinosaur);
        if (stronger == 1) System.out.println(robot.name + " is stronger!");
        else if (stronger == 2) System.out.println(dinosaur.name + " is stronger!");
        else System.out.println("They’re equally strong!");

        // Begin the battle
        UltimateBattle.beginBattle(robot, dinosaur);
    }
}

// Run main
UltimateBattle.main(null);

Robot Rex | Power: 25 | Health: 100
Dino Doom | Power: 30 | Health: 90
Dino Doom is stronger!
=== Battle Begins Between Robot Rex and Dino Doom ===
Robot Rex attacks Dino Doom!
Dino Doom attacks Robot Rex!
Robot Rex attacks Dino Doom!
Dino Doom attacks Robot Rex!
Robot Rex attacks Dino Doom!
Dino Doom attacks Robot Rex!
Robot Rex attacks Dino Doom!
=== Battle Over! ===
Robot Rex wins!
Battle lasted 10.5 seconds.