110k views
5 votes
Implement a class Student. The class will be used to hold the student's name and quiz scores. The student's name should be supplied when the object is created. The class should contain the following.

Constructor
1) init_(self, name)
Instance Variables
a) name - the student's name; string
b) scores - the student's quiz scores; list
Methods
i) getName () -returns the student's name
ii) addQuiz(score) adds a quiz score to the score list
iii) getAverageScore() -returns the average of the scores

1 Answer

7 votes

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())
  1. Constructor: The constructor method __init__ initializes the instance variables name and scores.
  2. getName(): This method returns the student's name.
  3. addQuiz(score): This method adds a quiz score to the scores list.
  4. getAverageScore(): This method returns the average of the scores by summing them and dividing by the number of scores.
User Rochb
by
8.0k points