Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the definition of a class Player containing: An instance variable name of

ID: 440859 • Letter: W

Question

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.

Explanation / Answer

class Player{
       String str = NULL ;
       int score = 0 ;
       public void setName(String s){
                  str = s ;
        }
      public void setScore(int num){
               score = num ;
        }
      public String getName(){
               return str ;
       }
      public int getScore(){
            return score;
       }
}