Basic Class Creation
Four independent classes demonstrating basic OOP concepts
Vehicle
Basic class with private attributes and public methods.
- Private properties have getters and setters
- Public methods to control behavior
⚠️ Important: Without proper validation in accelerate() and decelerate() methods, the currentSpeed could:
- Go below zero (if decelerate is called too many times or with large values)
- Exceed
maxSpeed(if accelerate is called without checking the limit)
💡 Click to see validation implementation
Good practice: These methods should include validation:
public void accelerate(int speed) {
currentSpeed += speed;
if (currentSpeed > maxSpeed) {
currentSpeed = maxSpeed; // Cap at maxSpeed
}
}
public void decelerate(int speed) {
currentSpeed -= speed;
if (currentSpeed < 0) {
currentSpeed = 0; // Cannot go below 0
}
}
Computer
Demonstrates encapsulation with memory management.
- Private properties have getters and setters
- Methods to add/remove memory with validation against maxMemory
- Shows how to manage state with constraints
⚠️ Important: Without proper validation in addMemory() and removeMemory() methods, the memory could:
- Go below zero (if removeMemory is called with amount greater than current memory)
- Exceed
maxMemory(if addMemory is called without checking the limit)
💡 Click to see validation implementation
Good practice: These methods should include validation:
public void addMemory(int amount) {
memory += amount;
if (memory > maxMemory) {
memory = maxMemory; // Cap at maxMemory
}
}
public void removeMemory(int amount) {
memory -= amount;
if (memory < 0) {
memory = 0; // Cannot go below 0
}
}
City
Multiple data types (String, double, boolean), constructor and comparison method.
- population: In million
- category:
- If not a capital: population > 6 = "Big", otherwise: "Small"
- If capital: population > 7 = "Big", otherwise: "Small"
- Private properties have getters and setters
- Comparison method takes another City object as parameter
Orc
Constructor overloading: 2 constructors with different parameters.
- One constructor with (health, force)
- Another constructor with (name, health, force)
- Private properties have getters and setters
- Method to attack another Orc
Practice Exercise: Orc Battle
Objective: Create two Orc instances and make them fight in a loop until one is defeated.
Requirements:
- Instantiate two Orcs with different names, health, and force values
- Create a loop where they attack each other alternately
- Display the health of each Orc after each attack
- Stop when one Orc's health reaches 0 or below
- Announce the winner
💡 Click to see suggested solution
public class OrcBattle {
public static void main(String[] args) {
// Create two orcs
Orc orc1 = new Orc("Grommash", 100, 15);
Orc orc2 = new Orc("Thrall", 120, 12);
System.out.println("=== Battle begins! ===");
System.out.println(orc1.getName() + " (Health: " + orc1.getHealth() + ", Force: " + orc1.getForce() + ")");
System.out.println(orc2.getName() + " (Health: " + orc2.getHealth() + ", Force: " + orc2.getForce() + ")");
System.out.println();
int round = 1;
// Battle loop
while (orc1.getHealth() > 0 && orc2.getHealth() > 0) {
System.out.println("--- Round " + round + " ---");
// Orc1 attacks Orc2
orc1.attack(orc2);
System.out.println(orc1.getName() + " attacks " + orc2.getName());
System.out.println(orc2.getName() + " health: " + orc2.getHealth());
// Check if orc2 is defeated
if (orc2.getHealth() <= 0) {
break;
}
// Orc2 attacks Orc1
orc2.attack(orc1);
System.out.println(orc2.getName() + " attacks " + orc1.getName());
System.out.println(orc1.getName() + " health: " + orc1.getHealth());
System.out.println();
round++;
}
// Announce winner
System.out.println("=== Battle ended! ===");
if (orc1.getHealth() > 0) {
System.out.println(orc1.getName() + " wins with " + orc1.getHealth() + " health remaining!");
} else {
System.out.println(orc2.getName() + " wins with " + orc2.getHealth() + " health remaining!");
}
}
}
Expected Output:
=== Battle begins! ===
Grommash (Health: 100, Force: 15)
Thrall (Health: 120, Force: 12)
--- Round 1 ---
Grommash attacks Thrall
Thrall health: 105
Thrall attacks Grommash
Grommash health: 88
--- Round 2 ---
Grommash attacks Thrall
Thrall health: 90
Thrall attacks Grommash
Grommash health: 76
...
=== Battle ended! ===
Grommash wins with 4 health remaining!
Key Points:
- The
attack()method reduces the opponent's health by the attacker's force - The loop continues while both orcs have health > 0
- We check after each attack if the defender is defeated
- Getters are used to access private properties