Java 4.7.8 how many players in the game

Java 4.7.8 how many players in the game?
i keep getting the same 3 errors: “grader.java: line 24: constructor player in class player cannot be applied to given types;”

it does work when i run it though.

the check code finds the errors and won’t let me turn it in.

public class player
{
private string name;

private int level;

public static int totalplayers = 0;

public static int maxplayers = 10;

public player(string username)
{
name = username;
totalplayers++;
}

public static int totalplayers()
{
return totalplayers;
}

public static boolean gamefull()
{
return totalplayers >= maxplayers;
}

public string tostring()
{
return “online: ” + name;
}
}

The Correct Answer and Explanation is:

The main issue here is in the constructor and method declarations. The constructor and some method names are not using correct Java conventions for data types and case-sensitivity. Here’s the corrected code along with an explanation:

public class Player {
    private String name;
    private int level;

    public static int totalPlayers = 0;
    public static int maxPlayers = 10;

    public Player(String username) {
        name = username;
        totalPlayers++;
    }

    public static int getTotalPlayers() {
        return totalPlayers;
    }

    public static boolean isGameFull() {
        return totalPlayers >= maxPlayers;
    }

    @Override
    public String toString() {
        return "Online: " + name;
    }
}

Explanation

1. Correcting Class and Method Names

Java is case-sensitive, so string should be corrected to String, as String is the correct class name in Java. Additionally, class names in Java typically start with an uppercase letter by convention, so player should be changed to Player.

2. Constructor Issue

The error message you were seeing is due to the constructor Player(String username) not matching the expected format. In Java, a constructor must have the same name as the class, and constructors are case-sensitive. Since the class is now named Player, the constructor also needs to be Player. Previously, the lowercase player in the constructor declaration did not match the class name.

3. Static Methods and Method Naming Conventions

In Java, naming conventions suggest that method names should start with a lowercase letter, so I changed totalplayers and gamefull to getTotalPlayers and isGameFull, respectively. This makes the code more readable and adheres to standard naming conventions. Also, the totalPlayers method was updated to getTotalPlayers() for consistency with Java conventions.

4. Adding @Override Annotation for toString Method

The toString method in Java typically provides a string representation of the object. Since you want to override Java’s built-in toString() method for custom output, adding @Override helps the compiler recognize this intent, improving readability and reducing errors.

5. Static Variables

totalPlayers and maxPlayers are static, meaning they belong to the class rather than any instance. This is why these variables keep track of the total across all player instances and allow checks on game capacity.

With these fixes, the code should work correctly, allowing you to check player count and verify if the game is full.

Scroll to Top