Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Implementing Character Classes | Advanced Java Features and Techniques
Java Data Structures
course content

Course Content

Java Data Structures

Java Data Structures

1. Fundamental Data Structures in Java
2. Advanced Data Structures in Java
3. Mastering Map in Java
4. Advanced Java Features and Techniques

book
Challenge: Implementing Character Classes

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.

java

Main

copy
123456789101112131415161718
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(); } }
  • Define an enum called CharacterClass with different character classes (e.g., Warrior, Mage) and their attributes (e.g., hp, atk);
  • Include a constructor in the enum to initialize the attributes for each character class;
  • Implement a method printStats() within the enum to print the statistics of the character, including class name, health points (hp), and attack points (atk);
  • In the main program, create instances of character classes and call the printStats() method to display their statistics.
java
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
We're sorry to hear that something went wrong. What happened?
some-alt