Skip to the content.

Calling Class Methods Homework

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) {
        // Will this work?
        // Yes - call static methods on the class
        Bro.sayCatchphrase();

        // What about this one?
        // No - sayHi() is an instance method, so we must call it on an object

        Bro alex = new Bro("Alex");
        alex.sayHi();
        // Prefer calling static methods on the class for clarity
        Bro.sayCatchphrase();
    }
}

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

  1. Create a class representing your object with:

    Instance variables:

    • String name
    • int power
    • int health
    • MAKE 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
  2. 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.
// Code Here

public class UltimateBattle {

    static class Fighter {
        // Instance variables
        String name;
        int power;
        int health;
        String weapon;

        // Static variable
        static double fightDuration = 0;

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

        // Instance method: attack another fighter
        void attack(Fighter opponent) {
            System.out.println(this.name + " attacks " + opponent.name + " with a " + weapon + "!");
            opponent.health -= this.power;
            if (opponent.health < 0) opponent.health = 0;
            fightDuration += 1.5; // adds time to fight duration
        }

        // Instance method: print current health status
        void printStatus() {
            System.out.println(name + " | Health: " + health + " | Power: " + power + " | Weapon: " + weapon);
        }

        // Instance method: heal
        void heal(int amount) {
            this.health += amount;
            System.out.println(name + " heals for " + amount + " points!");
        }

        // Static method: compare which fighter is stronger
        static Fighter strongerFighter(Fighter f1, Fighter f2) {
            if (f1.power > f2.power) {
                return f1;
            } else if (f2.power > f1.power) {
                return f2;
            } else {
                return null; // tie
            }
        }

        // Static method: begin a full battle simulation
        static void beginBattle(Fighter f1, Fighter f2) {
            System.out.println("\nThe Battle Begins Between " + f1.name + " and " + f2.name + "!\n");

            while (f1.health > 0 && f2.health > 0) {
                f1.attack(f2);
                if (f2.health <= 0) break;

                f2.attack(f1);
                if (f1.health <= 0) break;
            }

            System.out.println("\nBattle ended in " + fightDuration + " minutes.");
            if (f1.health > 0) {
                System.out.println(f1.name + " wins the battle!");
            } else {
                System.out.println(f2.name + " wins the battle!");
            }
        }
    }

    // Main method
    public static void main(String[] args) {
        Fighter knight = new Fighter("Arthur the Knight", 25, 120, "Sword");
        Fighter viking = new Fighter("Bjorn the Viking", 30, 110, "Axe");

        // Print their initial status
        System.out.println("Initial Fighters:");
        knight.printStatus();
        viking.printStatus();

        // Compare strength
        Fighter stronger = Fighter.strongerFighter(knight, viking);
        if (stronger != null) {
            System.out.println("\nStronger Fighter: " + stronger.name);
        } else {
            System.out.println("\nBoth fighters are equally strong!");
        }

        // Begin the battle
        Fighter.beginBattle(knight, viking);
    }
}