Write the definition of a class Player containing:
An instance variable name of type String , initialized to the empty String.
An instance variable score of type int , initialized to zero.
A method called setName that has one parameter, whose value it assigns to the instance variable name .
A method called setScore that has one parameter, whose value it assigns to the instance variable score .
A method called getName that has no parameters and that returns the value of the instance variable name .
A method called getScore that has no parameters and that returns the value of the instance variable score .
No constructor need be defined.
public class Player{
private String name;
private int score = 0;
public void setName(String nm)
{name = nm;}
public void setScore(int sc)
{score = sc;}
public String getName()
{return name;}
public int getScore()
{return score;}
}
thank you.
(I’m not going to be “explicit” in terms of just giving you the answer, but hopefully I can help you fix it for yourself…)
Well, here’s the start of what you’ve said you’re meant to do:
Your code:
In what way is that initialized to the empty string?
To think about it another way: what would you expect the result of calling
length()on an empty string to be? What happens if you try calling it on your variable?