Final answer:
To implement the class Student with the required methods and instance variables in Python, follow the provided code example. The class includes a constructor, methods to get the student's name, add quiz scores, and calculate the average score.
Step-by-step explanation:
To implement a class Student with the required methods and instance variables, you can use the following Python code:
class Student:
def __init__(self, name):
self.name = name
self.scores = []
def getName(self):
return self.name
def addQuiz(self, score):
self.scores.append(score)
def getAverageScore(self):
return sum(self.scores) / len(self.scores)
# Example usage
student = Student("John")
student.addQuiz(8)
student.addQuiz(9)
student.addQuiz(7)
average_score = student.getAverageScore()
print("Average score:", average_score)
print("Student name:", student.getName())
- Constructor: The constructor method __init__ initializes the instance variables name and scores.
- getName(): This method returns the student's name.
- addQuiz(score): This method adds a quiz score to the scores list.
- getAverageScore(): This method returns the average of the scores by summing them and dividing by the number of scores.