In: Computer Science
This is python:
#Imagine you're writing a program to calculate the class
#average from a gradebook. The gradebook is a list of
#instances of the Student object.
#
#You don't know everything that's inside the Student object,
#but you know that it has a method called get_grade().
#get_grade() will return the average for the student
#represented by a given instance of Student.
#
#You don't know if get_grade() is stored in memory or if
#it's calculated when it's needed, but you don't need to.
#All you need to know is that the class Student has a method
#get_grade() that returns an integer representing the
#student's numeric grade.
#
#Write a function called class_average. class_average()
#should take as input a list of instances of Student, and it
#should return as output the average grade of those
#students, as calculated via their get_grade() method.
#Remember, average is the sum of all their individual
#grades, divided by the number of students.
#
#Hint: You do NOT need to write the Student class to
#complete this problem. You may if you want to in order to
#run and test your code, but when you submit your code for
#grading, we will test it with our Student class. Don't let
#this throw you off: throughout this course, you've been
#using lots of classes without knowing how they work: you
#don't know how the String class works, but you know what
#its upper() method does. You do not need to know how the
#Student class works to use its get_grade() method.
#Write your function here!
#If you want to write a Student class to test your code,
#you could below, and then add your own test cases below
#that. The only requirement you would need to meet is that
#your Student class would need to have a get_grade()
#method that returns an integer (in addition to any other
#usual requirements for classes).
Python Code for the provided problem statement
# Python program to calculate the Average grades of each
student
# as well as the average of class grades
# student class
class Student:
def __init__(self, roll, name, lst): #
constructor
self.name = name # name
self.roll = roll # roll
Number
self.lst = lst # list of
marks
# this class method will calculate the average marks
of current student object
# and then return the calculated value
def get_grade(self):
total = 0
list_size = len(self.lst)
for x in self.lst:
total += x
avg = total/list_size
return avg
# this method will display the each student data
# as well as calculate the Average grades of the class
# and then it will display the average_of_class_grades
def class_average(students_list):
sum_of_average_grades = 0
for obj in students_list:
print("\nStudent Name: ",obj.name)
print("Student Roll no: ",obj.roll)
# call method to get the average grades of the Student
temp = obj.get_grade()
print("Student Average Grades: ",temp)
sum_of_average_grades += temp
# average_of_class_grades
total_students = len(students_list)
average_of_class_grades =
sum_of_average_grades/total_students
print("\nAverage of Class Grades: ",average_of_class_grades)
# driver function for initial inputs
def main():
# enter number of students
no_of_students = int(input("Number of students in the class:
"))
# initialize list to store student object
students_list = []
# for each student, enter roll, name, and marks
for i in range(no_of_students):
print("\nStudent -> ",(i+1))
roll = int(input("Enter roll number: "))
name = input("Enter name: ")
# initialize list to store marks
marks_list = []
for j in range(3):
# add marks to the list
marks_list.append(int(input("Enter marks "+str(j+1)+": ")))
# add student object to the list
students_list.append(Student(roll, name, marks_list))
# call method to calculate the Average grades of the class
class_average(students_list)
# program start from here
if __name__=="__main__":
main()
Python Code Screenshots
Output Screenshot
Note:- Please take care of indentation while writing the python code.