I need some VERY basic help for my java homework, its all about creating classes
ID: 3629332 • Letter: I
Question
I need some VERY basic help for my java homework, its all about creating classes, which I am brand new to and I just need to see an example program and I should be good to go.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 .
Explanation / Answer
// Write the definition of a class Player containing:
public class Player
{
// An instance variable name of type String , initialized to the empty String.
private String name;
// An instance variable score of type int , initialized to zero.
private int score;
// initializations in constructor
public Player()
{
name = "";
score = 0;
}
// A method called setName that has one parameter, whose value it assigns to the instance variable name .
public void setName(String n)
{
name = n;
}
// A method called setScore that has one parameter, whose value it assigns to the instance variable score .
public int setScore(int s)
{
score = s;
}
}