Question: count = 0 def __init__(self, d_code= '', d_name = '', capacity = 5, minGPA = 2.5): self.d_code = d_code self.d_name = d_name self.capacity = capacity
count = 0 def __init__(self, d_code= '', d_name = '', capacity = 5, minGPA = 2.5): self.d_code = d_code self.d_name = d_name self.capacity = capacity self.minGPA = minGPA self.num_students = 0 self.avgGPA = 0.0 self.roster = [ ]
self.reason = '' Department.count += 1 def __str__(self): return('Department ' + self.d_code + ' ' + self.d_name + ' ' + ' capacity= ' + str(self.capacity) + ' number of students= ' + str(self.num_students) + ' min GPA= ' + str(self.minGPA)) def addStudent(self, s_obj): if not s_obj or not isinstance(s_obj, Student): return False, 'add failed: obj type' else: self.qualified, self.reason = self.isQualified(s_obj) if self.qualified: self.roster.append(s_obj) self.num_students += 1 Department.univ_students += 1 self.calcAvgGPA() s_obj.setMajor(self.d_code) # set Student's major to Dept else: return False, self.reason return True, 'added' def isQualified (self, s_obj): if self.num_students >= self.capacity: # Check Dept. capacity return (False, 'add failed: over capacity') elif s_obj.gpa() min return (False, 'add failed: minGPA') elif not s_obj.isEnrolled(): # Student must be enrolled return (False, 'add failed: not enrolled') else: if len(self.roster) > 0: # Check student not already for s in self.roster: # in Dept. major roster if s.sameStudent(s_obj): return (False, 'add failed: already in Department') return (True, '') def calcAvgGPA(self): # recalculates Dept. avg GPA self.avgGPA = 0 for s in self.roster: self.avgGPA += s.gpa() return (round(self.avgGPA / len(self.roster),2)) def printRoster(self): print(' List of students in Department ', self.d_name) for s in self.roster: print(s)
class Student():
def __init__(self, g_num, name, status = 'Freshman', major = 'IST', enrolled = 'y', credits = 0, qpoints = 0): self.g_num = str(g_num) self.name = str(name) self.status = str(status) self.major = str(major) self.enrolled = str(enrolled) self.credits = credits self.qpoints = qpoints def __str__(self): return(str(self.name) + ', ' + str(self.g_num) + ', ' + str(self.status) + ', ' + str(self.major) + ', active: ' + str(self.enrolled) + ', credits = ' + str(self.credits) + ', gpa = {0:4.2f}'.format(self.gpa())) # calculate gpa, don't store def gpa (self) : if self.credits > 0: # prevent division by zero... return self.qpoints / self.credits else : return 0 # ...if zero credits, return gpa = 0 def setMajor (self, major) : self.major = major return True def sameStudent (self, s_obj): return(self.g_num == str(s_obj.g_num) and self.name == str(s_obj.name) ) # return True if g_num, name match def isEnrolled (self): return (self.enrolled == 'y') # return True if student is enrolled
print(' Start of Department and Student class demo **************') s1 = Student('G34523', 'Arian Khrosadni', status = 'sophomore', major = 'ACC', enrolled = 'y', credits = 30, qpoints = 90) s2 = Student('G21354', 'Saba Arjmand', status = 'senior', major = 'BIO', enrolled = 'y', credits = 90, qpoints = 315) s3 = Student('G42176', 'Parham Tabibi', status = 'sophomore', major = 'CSC', enrolled = 'y', credits = 45, qpoints = 160) s4 = Student('G10944', 'Ariain Ziaee', status = 'junior', major = 'IT', enrolled = 'y', credits = 70, qpoints = 225) s5 = Student('G22164', 'Mojgan Salehi', status = 'senior', major = 'ART', enrolled = 'y', credits = 105, qpoints = 290) s6 = Student('G31378', 'Anahita Khorsandi', status = 'freshman', major = 'HIS', enrolled = 'y', credits = 15, qpoints = 35) s7 = Student('G44523', 'Amirali Shirazi', status = 'junior', major = 'ENG', enrolled = 'y', credits = 65, qpoints = 250) s8 = Student('G55388', 'Shahrokh Gholi', status = 'sophomore', major = 'IT', enrolled = 'y', credits = 45, qpoints = 120) s9 = Student('G66104', 'Ali Amiri', status = 'freshman', major = 'ART', enrolled = 'y', credits = 15, qpoints = 45) s10 = Student('G11332', 'Parisa Shafiei', status = 'senior', major = 'BUS', enrolled = 'y', credits = 110, qpoints = 275) s11 = Student('G22143', 'Kimia Jafari', status = 'junior', major = 'ENG', enrolled = 'y', credits = 85, qpoints = 250) s12 = Student('G31310', 'Golsana Ziaee', status = 'sophomore', major = 'CSC', enrolled = 'y', credits = 45, qpoints = 120) s13 = Student('G31309', 'Melika Izadi', status = 'sophomore', major = 'BIO', enrolled = 'n', credits = 45, qpoints = 120) s14 = Student('G11183', 'Amir Khorsandi', status = 'freshman', major = 'ACC', enrolled = 'y', credits = 20, qpoints = 38) print('List of Students created: ') print('s2= ',s1) print('s2= ',s2) print('s3= ',s3) print('s4= ',s4) print('s5= ',s5) print('s6= ',s6) print('s7= ',s7) print('s8= ',s8) print('s9= ', s9) print('s10= ',s10) print('s11= ',s11) print('s12= ',s12) print('s13= ',s13) print('s14= ',s14) d1 = Department('ENGR', 'Engineering', 5, 2.75) d2 = Department('ARTS', 'Art and Architecture', 15, 2.0) d3 = Department('CHHS', 'College of Health and Human Sevrices', 10, 2.5) print(' Departments established:') print(d1) print(d2) print(d3) print(' Adding s1 - s5 to ENGR Department- print Roster follows') d1.addStudent(s1) d1.addStudent(s2) d1.addStudent(s3) d1.addStudent(s4) d1.addStudent(s5) d1.printRoster() a, b = d1.addStudent(s6) print(' Attempting to add ', s6.name, ' to ', d1.d_code, ' - over capacity, retvalues: ', a, b) d1.printRoster() print(' Adding ', s6.name, ' and ', s7.name, ' to ', d2.d_code, ', printRosterfollows: ') d2.addStudent(s6) d2.addStudent(s7) d2.printRoster() print(' Adding ', s8.name, ' and ', s9.name, ' to ', d3.d_code, ', printRosterfollows: ') d3.addStudent(s8) a, b = d3.addStudent(s9) print(' Attempting to add qualified student , ', s9.name, ' to ', d3.d_code, ',CHHS, return values; ', a, b) d3.printRoster() a, b = d3.addStudent(s14) print(' Attempting to add low GPA student ', s14.name, ', return values: ', a, b) a, b = d2.addStudent(s13) print(' Attempting to add non-enrolled student ', s13.name, ', return values: ', a, b) a, b = d3.addStudent(s8) print(' Attempting to add dupe student ', s8.name, ', return values: ', a, b) print(' Adding s10 to ENGR, s11 to ARTS, s12 to CHHS, then print all 3 roster') d1.addStudent(s10) d2.addStudent(s11) d3.addStudent(s12) d1.printRoster() d2.printRoster() d3.printRoster()

In this assignment you will extend the 'dictionary' class by adding a new subclass and four methods. The new class should be called 'NewDict' and must be a subclass of 'dict'. The new methods will provide functions that are not directly included in the basic dictionary class.They are: displaysorted0 - displays the contents of the dictionary in ascending key order addfromlist() - adds the contents of a list to the dictionary using the first list element as the key and the rest as the value retrieve) - returns the list of values using the keys provided in a list of keys merge) - merges in the contents of an input dictionary resulting in a new dictionary containing the original plus the input Specification requirements for each method are in the table below Method Name displaysorted Depending on the value of an input Purpose Input Output/returns display D-display list R' Return st R B'- do both Default - 'D return list B- display and return list parameter, displays the contents of the dictionary in ascending key-sorted order, returns a list consisting of the key and values in sorted order, or does both. If the parameter is not D, R, or B assume the default of D addfromlist Adds the content of an input list to the Input list If the input is not a list, return 'False' and the message 'not a ist' If input is a list, return True' and the new updated dictionar If the input is not a list, return 'False' and 'not a list If input is a list, return True' and a list consisting of the keys and values. If a key is not in the dictionary, the key and 'not found dictionary using the first list element as key and the remainder of the list as the kev value. Validate that the input is a type list' Retrieve the dictionary values using an input list consisting of keys to be looked up in the dictionary.Return a list consisting of the key and its corresponding value. If a key is not in the dictionary, return the character string 'not found'. Validate that the input is type "list retrieve List of keys Merges an input dictionary into the current/'self dictionary. Validate that themerged in input is the correct type. Return 'False' and 'not a dictionary' if it isn't. If a key in the input dictionary is already in self, add the key to a 'dupes' list. When done, return 'True and the list of dupes Dictionary to be If input is not a merge dictionary, return False' and 'invalid input' message If input is a dictionary, return 'True' and the list of duplicates In this assignment you will extend the 'dictionary' class by adding a new subclass and four methods. The new class should be called 'NewDict' and must be a subclass of 'dict'. The new methods will provide functions that are not directly included in the basic dictionary class.They are: displaysorted0 - displays the contents of the dictionary in ascending key order addfromlist() - adds the contents of a list to the dictionary using the first list element as the key and the rest as the value retrieve) - returns the list of values using the keys provided in a list of keys merge) - merges in the contents of an input dictionary resulting in a new dictionary containing the original plus the input Specification requirements for each method are in the table below Method Name displaysorted Depending on the value of an input Purpose Input Output/returns display D-display list R' Return st R B'- do both Default - 'D return list B- display and return list parameter, displays the contents of the dictionary in ascending key-sorted order, returns a list consisting of the key and values in sorted order, or does both. If the parameter is not D, R, or B assume the default of D addfromlist Adds the content of an input list to the Input list If the input is not a list, return 'False' and the message 'not a ist' If input is a list, return True' and the new updated dictionar If the input is not a list, return 'False' and 'not a list If input is a list, return True' and a list consisting of the keys and values. If a key is not in the dictionary, the key and 'not found dictionary using the first list element as key and the remainder of the list as the kev value. Validate that the input is a type list' Retrieve the dictionary values using an input list consisting of keys to be looked up in the dictionary.Return a list consisting of the key and its corresponding value. If a key is not in the dictionary, return the character string 'not found'. Validate that the input is type "list retrieve List of keys Merges an input dictionary into the current/'self dictionary. Validate that themerged in input is the correct type. Return 'False' and 'not a dictionary' if it isn't. If a key in the input dictionary is already in self, add the key to a 'dupes' list. When done, return 'True and the list of dupes Dictionary to be If input is not a merge dictionary, return False' and 'invalid input' message If input is a dictionary, return 'True' and the list of duplicates
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
