1.10 Lesson Homework
Lesson 1.10 Homework
Popcorn Hack 1
- Good lines
sayCatchphrase();— static method, can be called directly inmain()alex.sayHi();— instance method called on an objectalex.sayCatchphrase();— works (static called from object, though not recommended)
- Bad lines (cause error)
sayHi();— instance method called from static context without an object
class Bro {
String name;
static String catchphrase = "Sup bro?";
// Constructor
public Bro(String name) {
this.name = name;
}
// Instance method — requires an object to call it
public void sayHi() {
System.out.println("Hey, I'm " + name);
}
// Static method — belongs to the class
public static void sayCatchphrase() {
System.out.println(catchphrase);
}
public static void main(String[] args) {
// Fixed: Call static method using the class name (best practice)
Bro.sayCatchphrase();
// Orignal Error: sayHi() is an instance method, cannot call from static context
// sayHi();
// Fix: Create an object to call instance method
Bro alex = new Bro("Alex");
// Correct: calling instance method on the object
alex.sayHi();
// Works but not recommended: calling static method through object
alex.sayCatchphrase();
// Fix: better way to call static method
Bro.sayCatchphrase();
}
}
Bro.main(new String[]{});
Sup bro?
Hey, I'm Alex
Sup bro?
Sup bro?
Homework
import java.util.Random;
class Warrior {
// Instance variables
String name;
int power;
int health;
// Static variable
static double fightDuration = 0.0; // in seconds
// Constructor
public Warrior(String name, int power, int health) {
this.name = name;
this.power = power;
this.health = health;
}
// Instance method: attack another warrior
public void attack(Warrior opponent) {
System.out.println(this.name + " attacks " + opponent.name + " for " + this.power + " damage!");
opponent.health -= this.power;
// Increase fight duration
fightDuration += 0.5; // half a second per attack
}
// Instance method: print warrior status
public void printStatus() {
System.out.println(name + " | Health: " + health + " | Power: " + power);
}
// Class method: compare which fighter is stronger
public static Warrior strongerFighter(Warrior w1, Warrior w2) {
return (w1.power > w2.power) ? w1 : w2;
}
// Class method: begin the battle
public static void beginBattle(Warrior w1, Warrior w2) {
System.out.println("The battle begins between " + w1.name + " and " + w2.name + "!");
Random rand = new Random();
// Battle loop
while (w1.health > 0 && w2.health > 0) {
if (rand.nextBoolean()) {
w1.attack(w2);
} else {
w2.attack(w1);
}
w1.printStatus();
w2.printStatus();
System.out.println("-------------------------");
try {
Thread.sleep(500); // delay for readability
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Declare winner
if (w1.health > 0) {
System.out.println(w1.name + " wins the battle!");
} else if (w2.health > 0) {
System.out.println(w2.name + " wins the battle!");
} else {
System.out.println("It's a tie!");
}
System.out.printf("Fight duration: %.1f seconds%n", fightDuration);
}
}
public class UltimateBattle {
public static void main(String[] args) {
// Create two objects (instance of Warrior)
Warrior robot = new Warrior("SteelBot", 8, 40);
Warrior dinosaur = new Warrior("Rexosaurus", 10, 35);
// Use instance methods
robot.printStatus();
dinosaur.printStatus();
System.out.println("\nStronger fighter is: " + Warrior.strongerFighter(robot, dinosaur).name);
// Begin battle
Warrior.beginBattle(robot, dinosaur);
}
}
UltimateBattle.main(new String[]{});
SteelBot | Health: 40 | Power: 8
Rexosaurus | Health: 35 | Power: 10
Stronger fighter is: Rexosaurus
The battle begins between SteelBot and Rexosaurus!
Rexosaurus attacks SteelBot for 10 damage!
SteelBot | Health: 30 | Power: 8
Rexosaurus | Health: 35 | Power: 10
-------------------------
Rexosaurus attacks SteelBot for 10 damage!
SteelBot | Health: 20 | Power: 8
Rexosaurus | Health: 35 | Power: 10
-------------------------
Rexosaurus attacks SteelBot for 10 damage!
SteelBot | Health: 10 | Power: 8
Rexosaurus | Health: 35 | Power: 10
-------------------------
Rexosaurus attacks SteelBot for 10 damage!
SteelBot | Health: 0 | Power: 8
Rexosaurus | Health: 35 | Power: 10
-------------------------
Rexosaurus wins the battle!
Fight duration: 5.5 seconds