Unit 1.10 - Using Objects
Categories: homeworkJava homework assignment covering object instantiation, method calls, and working with objects
Warrior class
public class Warrior {
// Instance variables
private String name;
private int power;
private int health;
private String weaponType; // Custom instance variable
// Static variable
private static double fightDuration = 0.0;
private static int totalWarriors = 0; // Custom static variable
// Constructor
public Warrior(String name, int power, int health, String weaponType) {
this.name = name;
this.power = power;
this.health = health;
this.weaponType = weaponType;
totalWarriors++;
}
// Instance method: attack
public void attack(Warrior opponent) {
System.out.println(this.name + " attacks " + opponent.name + " with " + this.weaponType + "!");
opponent.health -= this.power;
fightDuration += 0.5; // Each attack adds 0.5 seconds to fight duration
if (opponent.health < 0) {
opponent.health = 0;
}
}
// Instance method: printStatus
public void printStatus() {
System.out.println("=== " + name + "'s Status ===");
System.out.println("Health: " + health);
System.out.println("Power: " + power);
System.out.println("Weapon: " + weaponType);
System.out.println("========================\n");
}
// Custom instance method: heal
public void heal(int amount) {
this.health += amount;
System.out.println(this.name + " heals for " + amount + " health!");
}
// Class method: strongerFighter
public static int strongerFighter(Warrior w1, Warrior w2) {
if (w1.power > w2.power) {
return 1; // First warrior is stronger
} else if (w2.power > w1.power) {
return 2; // Second warrior is stronger
} else {
return 0; // Equal power
}
}
// Class method: beginBattle
public static void beginBattle(Warrior w1, Warrior w2) {
System.out.println("\n╔════════════════════════════════╗");
System.out.println("║ THE ULTIMATE BATTLE BEGINS ║");
System.out.println("╚════════════════════════════════╝\n");
System.out.println(w1.name + " VS " + w2.name + "\n");
fightDuration = 0.0;
int round = 1;
while (w1.health > 0 && w2.health > 0) {
System.out.println("--- Round " + round + " ---");
// Warrior 1 attacks
if (w1.health > 0) {
w1.attack(w2);
}
// Check if warrior 2 is defeated
if (w2.health <= 0) {
break;
}
// Warrior 2 attacks
if (w2.health > 0) {
w2.attack(w1);
}
System.out.println();
round++;
}
// Announce winner
System.out.println("\n╔════════════════════════════════╗");
System.out.println("║ BATTLE CONCLUDED! ║");
System.out.println("╚════════════════════════════════╝");
if (w1.health > 0) {
System.out.println("🏆 " + w1.name + " is victorious! 🏆");
} else {
System.out.println("🏆 " + w2.name + " is victorious! 🏆");
}
System.out.println("\nFight Duration: " + fightDuration + " seconds");
}
// Custom class method: getTotalWarriors
public static int getTotalWarriors() {
return totalWarriors;
}
// Custom class method: printBattleRules
public static void printBattleRules() {
System.out.println("=== Battle Rules ===");
System.out.println("1. Warriors take turns attacking");
System.out.println("2. Damage equals attacker's power");
System.out.println("3. Battle ends when one warrior reaches 0 health");
System.out.println("4. May the strongest warrior win!\n");
}
// Getters
public String getName() {
return name;
}
public int getPower() {
return power;
}
public int getHealth() {
return health;
}
public static double getFightDuration() {
return fightDuration;
}
}
Main method
public class Main {
public static void main(String[] args) {
// Print battle rules
Warrior.printBattleRules();
// Create two warrior objects
Warrior warrior1 = new Warrior("Thor the Mighty", 25, 150, "Thunder Hammer");
Warrior warrior2 = new Warrior("Shadow Blade", 30, 120, "Dual Katanas");
System.out.println("Total Warriors Created: " + Warrior.getTotalWarriors() + "\n");
// Print initial status
System.out.println("=== Initial Status ===\n");
warrior1.printStatus();
warrior2.printStatus();
// Compare fighters
int stronger = Warrior.strongerFighter(warrior1, warrior2);
if (stronger == 1) {
System.out.println(warrior1.getName() + " has more power!\n");
} else if (stronger == 2) {
System.out.println(warrior2.getName() + " has more power!\n");
} else {
System.out.println("Both warriors have equal power!\n");
}
// Demonstrate custom instance method (heal)
System.out.println("=== Pre-Battle Preparation ===");
warrior1.heal(20);
System.out.println();
// Begin the battle
Warrior.beginBattle(warrior1, warrior2);
// Print final status
System.out.println("\n=== Final Status ===\n");
warrior1.printStatus();
warrior2.printStatus();
}
}
Output:
=== Battle Rules ===
- Warriors take turns attacking
- Damage equals attacker’s power
- Battle ends when one warrior reaches 0 health
- May the strongest warrior win!
Total Warriors Created: 2
=== Initial Status ===
=== Thor the Mighty’s Status === Health: 150 Power: 25 Weapon: Thunder Hammer ========================
=== Shadow Blade’s Status === Health: 120 Power: 30 Weapon: Dual Katanas ========================
Shadow Blade has more power!
=== Pre-Battle Preparation === Thor the Mighty heals for 20 health!
╔════════════════════════════════╗ ║ THE ULTIMATE BATTLE BEGINS ║ ╚════════════════════════════════╝
Thor the Mighty VS Shadow Blade
— Round 1 — Thor the Mighty attacks Shadow Blade with Thunder Hammer! Shadow Blade attacks Thor the Mighty with Dual Katanas!
— Round 2 — Thor the Mighty attacks Shadow Blade with Thunder Hammer! Shadow Blade attacks Thor the Mighty with Dual Katanas!
— Round 3 — Thor the Mighty attacks Shadow Blade with Thunder Hammer! Shadow Blade attacks Thor the Mighty with Dual Katanas!
— Round 4 — Thor the Mighty attacks Shadow Blade with Thunder Hammer! Shadow Blade attacks Thor the Mighty with Dual Katanas!
— Round 5 — Thor the Mighty attacks Shadow Blade with Thunder Hammer! Shadow Blade attacks Thor the Mighty with Dual Katanas!
— Round 6 — Thor the Mighty attacks Shadow Blade with Thunder Hammer!
╔════════════════════════════════╗ ║ BATTLE CONCLUDED! ║ ╚════════════════════════════════╝ 🏆 Thor the Mighty is victorious! 🏆
Fight Duration: 5.5 seconds
=== Final Status ===
=== Thor the Mighty’s Status === Health: 20 Power: 25 Weapon: Thunder Hammer ========================
=== Shadow Blade’s Status === Health: 0 Power: 30 Weapon: Dual Katanas ========================