Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge (Character Class) | enum & Stream API
Java Data Structures
course content

Course Content

Java Data Structures

Java Data Structures

1. Basic Data Structures
2. Additional Data Structures
3. Map
4. enum & Stream API

bookChallenge (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 the printStats() method. Since printStats() 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.

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(); } }

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
some-alt