Game Character System in Java: Explanation & Practice
Create a game character hierarchy
Problem summary
Create GameCharacter base with Warrior, Mage, Archer. Each has different attack() damage.
Starter code
// GameCharacter: name, health, attack() abstract
// Warrior: attack() deals strength-based damage
// Mage: attack() deals magic-based damage
// Archer: attack() deals dexterity-based damage
public class Main {
public static void main(String[] args) {
// Create character and attack
// Print: <name> attacks for <damage> damage
}
}Expected output and test cases
- Warrior attack
Conan attacks for 50 damage
- Mage attack
Gandalf attacks for 75 damage
- Archer attack
Legolas attacks for 40 damage
Hints
- Each character type has unique stats
- Override attack() with type-specific logic
- Store base stats in subclass
Related Inheritance exercises
Practice all Inheritance exercises · Run this idea in the Java compiler