Course Content
Java Data Structures
Java Data Structures
Challenge (Character Class)
Task
Create a Java program that uses an enum
called CharacterClass
to represent different character classes in a game. Each character class should have associated attributes such as health points (hp
) and attack points (atk
). Implement a method printStats()
that prints the statistics of the character.
Note
The
this
keyword is used to refer to the current instance of the enum (character class) within theprintStats()
method. SinceprintStats()
is an instance method, it operates on a specific instance of the enum. Using this clarifies that the attributes being accessed are specific to the current enum instance.
main
enum CharacterClass { WARRIOR, MAGE, ARCHER, ROGUE, //write your code here } public class Main { public static void main(String[] args) { // Test the enum methods CharacterClass warrior = CharacterClass.WARRIOR; CharacterClass mage = CharacterClass.MAGE; warrior.printStats(); mage.printStats(); } }
Thanks for your feedback!