Answer to Question #160225 in Python for lulu

Question #160225

2.   Construct a class named student consisting of a student identification number, list of five grades, and an integer representing the total number of grades entered. The constructor for this class should initialize all student data members to zero.


Included in the class should functions/ methods for:

1.    constructor and a destructor.

2.      enter a student number

3.      enter a single test grade (used loop for entering 5 grades)

4.      set test grade

5.      compute an average grade

6.      display the student number and average grade.



1
Expert's answer
2021-01-31T11:01:14-0500
class Student:
    # Set student data members to zero.
    def __init__(self):
        self.IN=0 #identification number
        self.grades=[]
        self.numberGrades=0 #the total number of Grades
    # Destructor
    def __del__(self):
        self.IN=0
        self.numberGrades=0
    # Enter a student number
    def getStudentNumber(self):
        self.IN=int(input("Enter the identification number: "))
    # Enter a single test grade
    def getGrade(self):
        for i in range(5):
            tg=int(input("Enter the test grade "+str(i+1)+": "))
            self.setTestGrade(tg)
    # Set test grade
    def setTestGrade(self,tg):
        self.grades.append(tg)
        self.numberGrades+=1
        
    # Calculate an average grade
    def calculateAG(self):
        # calculate sum of grades    
        sumGrade = 0
        for i in range(5):
            sumGrade=sumGrade+self.grades[i]
        return sumGrade/self.numberGrades
        
    # Print the student number and average grade.
    def printStudentInformation(self):
        print("The student number: "+str(self.IN))
        print("The average grade: "+str(self.calculateAG()))


newStudent= Student()
newStudent.getStudentNumber()
newStudent.getGrade()
newStudent.printStudentInformation()
del newStudent 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS