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

Here is the original program: class Student: def __init__(self, name, hours, qpo

ID: 3633752 • Letter: H

Question

Here is the original program:

class Student:
def __init__(self, name, hours, qpoints):
self.name = name
self.hours = float(hours)
self.qpoints = float(qpoints)

def getName(self):
return self.name

def getHours(self):
return self.hours

def getQPoints(self):
return self.qpoints

def gpa(self):
return self.qpoints/self.hours

def makeStudent(infoStr):
# infoStr is a tab-separated line: name hours qpoints
# return a corresponding Student object
name, hours, qpoints = infoStr.split(" ")
return Student(name, hours, qpoints)

def main():
# open the input file for reading
filename = input("Enter the name of the grade file: ")
infile = open(filename, 'r')

# set best to the record for the first student in the file
best = makeStudent(infile.readine())

# process subsquent lines of the file
for line in infile:
# turn the line into a student record
s = makeStudent(line)
# if this student is best so far, remember it.
if s.gpa() > best.gpa():
best = s
infile.close()
# print information aout the best student
print("The best student is:", best.getName())
print("hours:", best.getHours())
print("GPA:", best.gpa())

if __name__ == '__main__':
main()



The question is to modify the student class from above by adding a mutator method that records a grade for the student. Here is the specification of the new method:

addGrade(self, gradePoint, credits) gradePint is a float that represent grade (e.g., A = 4.0, A- = 3.7, B+ = 3.3, etc.), and credits is a float indicating the number of credit hours for the class. Modify the student object by adding this grade information.

Use the updated class to implement a simple program for calculating GPA. Your program should create a new student object that has 0 credits and the prompt the user to enter course information (grade point and credits) and then print out the final GPA achieved.

Explanation / Answer

The makestudent() and main() methods in your sample code are irrelevant to the calculation of cumulative GPA, so I have omitted them from my own code. I have included many comments in the code to help you understand how the user feedback loop works. I ran the program from a UNIX command line to obtain the following test transcript. $ python student.py Enter grade for next course, or nothing to finish: 3.0 Enter number of credit hours for this course: 10 Enter grade for next course, or nothing to finish: 4.0 Enter number of credit hours for this course: 30 Enter grade for next course, or nothing to finish: 3.7 Enter number of credit hours for this course: 20 Enter grade for next course, or nothing to finish: 3.3 Enter number of credit hours for this course: 20 Enter grade for next course, or nothing to finish: *** final GPA = 3.625 The code listing is below. Regards, leapinglizard #===begin student.py class Student: def __init__(self, name, hours, qpoints): self.name = name self.hours = float(hours) self.qpoints = float(qpoints) def getName(self): return self.name def getHours(self): return self.hours def getQpoints(self): return self.qpoints def gpa(self): return self.qpoints/self.hours # new mutator method as per specification def addGrade(self, gradePoint, credits): self.hours += credits self.qpoints += credits*gradePoint if __name__ == '__main__': # output messages prompt_grade = 'Enter grade for next course, or nothing to finish: ' prompt_credits = 'Enter number of credit hours for this course: ' error_float = 'error: expected a floating-point number' # make a new Student object stu = Student('stu', 0.0, 0.0) # user-feedback loop while 1: # prompt user to enter a grade grade_str = raw_input(prompt_grade) # quit if no grade is entered if grade_str.strip() == '': break try: # convert input to a floating-point value grade = float(grade_str) except ValueError: # if input cannot be converted, restart feedback loop print error_float continue # prompt user to enter the number of credits credits_str = raw_input(prompt_credits).strip() try: # convert input to a floating-point value credits = float(credits_str) except ValueError: # if input cannot be converted, restart feedback loop print error_float continue # update the student's grades stu.addGrade(grade, credits) # after user has entered all grades, compute the cumulative GPA if stu.getHours() == 0.0: # can't compute GPA if hours = 0 print '*** zero credit hours recorded' else: # otherwise, output cumulative GPA and finish print '*** final GPA =', stu.gpa() #===end student.py