Answer to Question #279447 in Java | JSP | JSF for hab1bti

Question #279447

A constructor which takes in a



String


, the player’s name.



If the



String



given is



null



or the



empty string (


""


), set the player’s name to a



unique



non-empty value (in my reference code,



I use ”Player 1”, ”Player 2”, etc).



Otherwise, set the name to the given



String


.



Score and



number of wins should both start at zero.




A constructor which takes no arguments. It sets the name to a



unique



non-empty string. (In



my reference code, I use ”Player 1”, ”Player 2”, etc).




1
Expert's answer
2021-12-14T09:30:55-0500
public class Player {
    private String name;
    private int score;
    private int wins;
    private static int counter = 1;

    public Player(String name) {
        if (name == null) {
            this.name = "Player " + counter++;
        } else {
            this.name = name;
        }
        score = 0;
        wins = 0;
    }

    public Player() {
        this(null);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog