For this section, you will be implementing a University class. We have provided an edited version of the Student class from lecture.
Shoutout Suh Young Choi, a former TA and instructor for CSE 163, for this material!
Student Class¶
class Student:
'''
The Student class represents a single student. It has a name and
contains functionality to store and give access to a student's
schedule and credit-load.
'''
def __init__(self, name, schedule):
'''
Initializes the Student object with the given name
and schedule file.
'''
self._name = name
self._courses = {}
with open(schedule) as f:
for line in f.readlines():
course, credits = line.split()
self._courses[course] = int(credits)
def get_name(self):
'''
Returns the name of this student.
'''
return self._name
def __getitem__(self, course):
'''
Returns the credit-worth of the given course. If the
student is not enrolled in the course, return None.
'''
if course in self._courses:
return self._courses[course]
else:
return None
def get_courses(self):
'''
Returns a list that contains the names of all the classes
the student is currently enrolled in.
'''
return list(self._courses.keys())
reggie = Student("reggie", "students/reggie.txt")University Class¶
Problem 1: __init__¶
This function takes in a name and a directory name. This initializer should set-up the class with the tools that the other methods might need to implement their own behavior. It might be helpful to write down a simpler solution here and then come back to flesh it out!
Don’t forget that all fields you define in your
__init__should be private and begin with a_!Remember that the name of the student is in the file name. This means you might need to extract the name from the file name string. For example, reggie.txt should be transformed into reggie!
Problem 2: get_mean_credits¶
This method will return the average total credits of all students enrolled in the University. Should return None if no students are enrolled.
import os
class University:
'''
A University represents a collection of Students. It has a name and
contains functionality to compute aggregate statistics about its enrolled
students.
'''
def __init__():
'''
Implement an initializer for the class University. It should take in
a string name representing the name of this University and the directory
of schedule text files.
'''
def get_mean_credits():
'''
Returns the mean amount of credits that students are taking.
If there are no students enrolled, return None.
'''
uw = University("University of Washington", "students")
uw.get_mean_credits()