I am having trouble with this question: IN PYTHON PLEASE Modify the Student clas
ID: 3822144 • Letter: I
Question
I am having trouble with this question:
IN PYTHON PLEASE
Modify the Student class from problem 4 to compute grade point averages. Methods are needed to add a grade and get the current GPA. Specify grades as elements of a class Grade. Supply a constructor that constructs a grade from a string, such as “B+”. You will also need a method that translates grades into their numeric values (for example “B+” becomes 3.3). Below is the program that runs the Student and Grade classes.
# Create a new student.
s = Student("Bob")
# Add some quiz grades.
s.addQuiz(Grade("D"))
s.addQuiz(Grade("A-"))
s.addQuiz(Grade("B"))
# Show that the total and the average are computed correctly.
print(s.getName(), "had a total of %.1f grade points." % s.getTotalScore())
print("The GPA was %.1f." % s.getAverageScore())
My code for problem 4 is:
class Student:
#__init__() is a special method which will intialize all the variable of Student Class
def __init__(self, name):
self.name = name
self.totalQuizScore = 0
self.totalQuiz = 0
#This method will return the name of student
def getName(self):
return(self.name)
#This method will add up the score and increase the totalQuiz counter which holds total number of quiz student took
def addQuiz(self,score):
self.totalQuizScore=score+self.totalQuizScore
self.totalQuiz=self.totalQuiz+1
#This method will return total quiz score of particular student
def getTotalScore(self):
return(self.totalQuizScore)
#This method will return average quiz score i.e total score divided by number of quiz
def getAverageScore(self):
return(self.totalQuizScore/self.totalQuiz)
# Create a new student.
s = Student("Bob")
# Add some quiz scores.
s.addQuiz(50)
s.addQuiz(90)
s.addQuiz(75)
# Show that the total and the average are computed correctly.
print(s.getName(), "had a total score of %f." % s.getTotalScore())
print("The average score was %.1f." % s.getAverageScore())
Explanation / Answer
#pastebin link for cde: https://pastebin.com/69ZJAZgU
class Grade:
def __init__(self, grade):
self._grade = grade
self._grade_dict = {"A+" : 4.33,
"A" : 4.00,
"A-" : 3.77,
"B+" : 3.33,
"B" : 3.00,
"B-" : 2.67,
"C+" : 2.33,
"C" : 2.00,
"C-" : 1.67,
"D+" : 1.33,
"D" : 1.00,
"D-" : 0.67,
"F" : 0}
def numericalGrade(self):
if self._grade in self._grade_dict:
return self._grade_dict[self._grade]
class Student:
#__init__() is a special method which will intialize all the variable of Student Class
def __init__(self, name):
self.name = name
self.totalQuizScore = 0
self.totalQuiz = 0
#This method will return the name of student
def getName(self):
return(self.name)
#This method will add up the score and increase the totalQuiz counter which holds total number of quiz student took
def addQuiz(self,grade):
self.totalQuizScore=grade.numericalGrade() + self.totalQuizScore
self.totalQuiz=self.totalQuiz+1
#This method will return total quiz score of particular student
def getTotalScore(self):
return(self.totalQuizScore)
#This method will return average quiz score i.e total score divided by number of quiz
def getAverageScore(self):
return(self.totalQuizScore/self.totalQuiz)
# Create a new student.
s = Student("Bob")
# Add some quiz grades.
s.addQuiz(Grade("D"))
s.addQuiz(Grade("A-"))
s.addQuiz(Grade("B"))
# Show that the total and the average are computed correctly.
print(s.getName(), "had a total of %.1f grade points." % s.getTotalScore())
print("The GPA was %.1f." % s.getAverageScore())
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.