Write the definition of a class Player containing: An instance variable name of
ID: 3691465 • 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 set_name that has one parameter, whose value it assigns to the instance variable name. A method called set_score that has one parameter, whose value it assigns to the instance variable score. A method called get_name that has no parameters and that returns the value of the instance variable name. A method called get_score that has no parameters and that returns the value of the instance variable score. No constructor need be defined.
PYTHON
Explanation / Answer
class Player:
name = ""
score = 0
def set_name(self,name):
self.name=name
def set_score(self,score):
self.score=score
def get_score(self):
return self.score
def get_name(self):
return self.name
e = Player()
print(e.get_score())
e.set_score(10)
print(e.get_score())
print(e.get_name())
e.set_name("MI")
print(e.get_name())
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.