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

bookChallenge: 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.

Main.java

Main.java

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.
package com.example;

// Enum definition
enum CharacterClass {
    WARRIOR(100, 20), MAGE(80, 30), ARCHER(90, 25), ROGUE(85, 28);

    // Attributes
    private final int hp;
    private final int atk;

    // Constructor
    CharacterClass(int hp, int atk) {
        this.hp = hp;
        this.atk = atk;
    }

    // Method to print character statistics
    public void printStats() {
        System.out.println("Character Class: " + this);
        System.out.println("Health Points (HP): " + hp);
        System.out.println("Attack Points (ATK): " + atk);
        System.out.println();
    }
}

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();
    }
}
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookChallenge: 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.

Main.java

Main.java

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.
package com.example;

// Enum definition
enum CharacterClass {
    WARRIOR(100, 20), MAGE(80, 30), ARCHER(90, 25), ROGUE(85, 28);

    // Attributes
    private final int hp;
    private final int atk;

    // Constructor
    CharacterClass(int hp, int atk) {
        this.hp = hp;
        this.atk = atk;
    }

    // Method to print character statistics
    public void printStats() {
        System.out.println("Character Class: " + this);
        System.out.println("Health Points (HP): " + hp);
        System.out.println("Attack Points (ATK): " + atk);
        System.out.println();
    }
}

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();
    }
}
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
some-alt