class Student(object): def __init__(self, id, firstName, lastName): """ Constructor for the student class. Parameters: id -- an integeter identifer for the student firstName -- first name of the student lastName -- last name of the student """ self.id = id self.firstName = firstName self.lastName = lastName self.courses = [] self.hobbies = [] def __str__(self): """ Gives a string representation of a student. """ result = "Name: %s %s\nID: %s \n" % (self.firstName, self.lastName, self.id) result += "Courses: " for course in self.courses: result += "%s, " % (course) result = result[:-2] # remove the extra comma and space result += "\n" result += "Hobbies: " for hobby in self.hobbies: result += "%s, " % (hobby) result = result[:-2] return result def getId(self): return self.id def getFirstName(self): return self.firstName def getLastName(self): return self.lastName def getHobbies(self): return self.hobbies def getCourses(self): return self.courses def addCourse(self, course): """ Add a course for the student. """ self.courses.append(course) def addHobby(self, hobby): """ Add a hobby for the student. """ self.hobbies.append(hobby) def getNumCourses(self): """ Returns the number of courses the student is enrolled in. """ return len(self.courses) def getNumHobbies(self): """ Returns the number of hobbies the student has. """ return len(self.hobbies) def getFreeTime(self): """ Returns the amount of free time the student has. """ return 55 - 12*self.getNumCourses() - 5*self.getNumHobbies() if __name__ == "__main__": # Test code students = [] students.append(Student("38483", "Alice", "Smith")) students.append(Student("39849", "Bob", "Jones")) students.append(Student("39889", "Jane", "Doe")) for student in students: student.addCourse("CS21") students[0].addHobby("Sleeping") students[1].addHobby("Binary Searching") students[2].addHobby("Napping") students[2].addHobby("Cycling") students[2].addHobby("Basket Weaving") for student in students: print student print "%s has %d hours of free time each week.\n" % (student.getFirstName(), student.getFreeTime()) s = Student("12345", "Sara", "Tester") s.addCourse("CS21") s.addHobby("Python coding") s.addHobby("Sorting lists") print s print "Id:", s.getId() print "First name:", s.getFirstName() print "Last name:", s.getLastName() print "Hobbies:", s.getHobbies() print "Courses:", s.getCourses() print "getNumCourses:", s.getNumCourses() print "getNumHobbies:", s.getNumHobbies() print "Free time:", s.getFreeTime()