HW2NEW (1)

.py

School

University of Pennsylvania *

*We aren’t endorsed by this school

Course

350

Subject

Industrial Engineering

Date

Apr 3, 2024

Type

py

Pages

12

Uploaded by AmbassadorOryx2491

Report
# HW2 # REMINDER: The work in this assignment must be your own original work and must be completed alone. import random class Course: ''' >>> c1 = Course('CMPSC132', 'Programming in Python II', 3) >>> c2 = Course('CMPSC360', 'Discrete Mathematics', 3) >>> c1 == c2 False >>> c3 = Course('CMPSC132', 'Programming in Python II', 3) >>> c1 == c3 True >>> c1 CMPSC132(3): Programming in Python II >>> c2 CMPSC360(3): Discrete Mathematics >>> c3 CMPSC132(3): Programming in Python II >>> c1 == None False >>> print(c1) CMPSC132(3): Programming in Python II ''' def __init__(self, cid, cname, credits): # YOUR CODE STARTS HERE self.cid=cid self.cname=cname self.credits=credits def __str__(self): # YOUR CODE STARTS HERE return f"{self.cid}({self.credits}): {self.cname}" __repr__ = __str__ def __eq__(self, other): # YOUR CODE STARTS HERE if self.cid==other: return True else: return False class Catalog: ''' >>> C = Catalog() >>> C.courseOfferings {} >>> C._loadCatalog("cmpsc_catalog_small.csv") >>> C.courseOfferings {'CMPSC 132': CMPSC 132(3): Programming and Computation II, 'MATH 230': MATH 230(4): Calculus and Vector Analysis, 'PHYS 213': PHYS 213(2): General
Physics, 'CMPEN 270': CMPEN 270(4): Digital Design, 'CMPSC 311': CMPSC 311(3): Introduction to Systems Programming, 'CMPSC 360': CMPSC 360(3): Discrete Mathematics for Computer Science} >>> C.removeCourse('CMPSC 360') 'Course removed successfully' >>> C.courseOfferings {'CMPSC 132': CMPSC 132(3): Programming and Computation II, 'MATH 230': MATH 230(4): Calculus and Vector Analysis, 'PHYS 213': PHYS 213(2): General Physics, 'CMPEN 270': CMPEN 270(4): Digital Design, 'CMPSC 311': CMPSC 311(3): Introduction to Systems Programming} >>> isinstance(C.courseOfferings['CMPSC 132'], Course) True ''' def __init__(self): # YOUR CODE STARTS HERE self.courseOfferings={} def addCourse(self, cid, cname, credits): # YOUR CODE STARTS HERE if cid not in self.courseOfferings: c_obj=Course(cid, cname, credits) self.courseOfferings[cid]=(c_obj) return "Course added successfully" elif cid in self.courseOfferings: return "Course already added" def removeCourse(self, cid): # YOUR CODE STARTS HERE if cid in self.courseOfferings: del self.courseOfferings[cid] return "Course removed successfully" elif cid not in self.courseOfferings: return "Course not found" def _loadCatalog(self, file): with open(file, "r") as f: course_info = f.read() # YOUR CODE STARTS HERE list1=course_info.split('\n') for i in range(len(list1)): list1[i]=list1[i].split(',') for i in range(len(list1)): self.courseOfferings[list1[i][0]]=Course(list1[i][0],list1[i][1], int(list1[i][2])) class Semester: ''' >>> cmpsc131 = Course('CMPSC 131', 'Programming in Python I', 3) >>> cmpsc132 = Course('CMPSC 132', 'Programming in Python II', 3) >>> math230 = Course("MATH 230", 'Calculus', 4) >>> phys213 = Course("PHYS 213", 'General Physics', 2) >>> econ102 = Course("ECON 102", 'Intro to Economics', 3)
>>> phil119 = Course("PHIL 119", 'Ethical Leadership', 3) >>> spr22 = Semester() >>> spr22 No courses >>> spr22.addCourse(cmpsc132) >>> isinstance(spr22.courses['CMPSC 132'], Course) True >>> spr22.addCourse(math230) >>> spr22 CMPSC 132; MATH 230 >>> spr22.isFullTime False >>> spr22.totalCredits 7 >>> spr22.addCourse(phys213) >>> spr22.addCourse(econ102) >>> spr22.addCourse(econ102) 'Course already added' >>> spr22.addCourse(phil119) >>> spr22.isFullTime True >>> spr22.dropCourse(phil119) >>> spr22.addCourse(Course("JAPNS 001", 'Japanese I', 4)) >>> spr22.totalCredits 16 >>> spr22.dropCourse(cmpsc131) 'No such course' >>> spr22.courses {'CMPSC 132': CMPSC 132(3): Programming in Python II, 'MATH 230': MATH 230(4): Calculus, 'PHYS 213': PHYS 213(2): General Physics, 'ECON 102': ECON 102(3): Intro to Economics, 'JAPNS 001': JAPNS 001(4): Japanese I} ''' def __init__(self): # --- YOUR CODE STARTS HERE self.courses={} def __str__(self): # YOUR CODE STARTS HERE if len(self.courses.keys())==0: return "No courses" else: all_courses='; '.join(self.courses.keys()) return all_courses __repr__ = __str__ def addCourse(self, course): # YOUR CODE STARTS HERE if course.cid in self.courses: return "Course already added" else: self.courses[course.cid]=course
def dropCourse(self, course): # YOUR CODE STARTS HERE if course.cid in self.courses: del self.courses[course.cid] else: return "No such course" @property def totalCredits(self): # YOUR CODE STARTS HERE total=0 for i in self.courses.keys(): total+=self.courses[i].credits return total @property def isFullTime(self): # YOUR CODE STARTS HERE if self.totalCredits>=12: return True else: return False class Loan: ''' >>> import random >>> random.seed(2) # Setting seed to a fixed value, so you can predict what numbers the random module will generate >>> first_loan = Loan(4000) >>> first_loan Balance: $4000 >>> first_loan.loan_id 17412 >>> second_loan = Loan(6000) >>> second_loan.amount 6000 >>> second_loan.loan_id 22004 >>> third_loan = Loan(1000) >>> third_loan.loan_id 21124 ''' def __init__(self, amount): # YOUR CODE STARTS HERE self.loan_id=self.__getloanID self.amount=amount
def __str__(self): # YOUR CODE STARTS HERE return f"Balance: ${self.amount}" __repr__ = __str__ @property def __getloanID(self): # YOUR CODE STARTS HERE return random.randint(10000,99999) class Person: ''' >>> p1 = Person('Jason Lee', '204-99-2890') >>> p2 = Person('Karen Lee', '247-01-2670') >>> p1 Person(Jason Lee, ***-**-2890) >>> p2 Person(Karen Lee, ***-**-2670) >>> p3 = Person('Karen Smith', '247-01-2670') >>> p3 Person(Karen Smith, ***-**-2670) >>> p2 == p3 True >>> p1 == p2 False ''' def __init__(self, name, ssn): # YOUR CODE STARTS HERE self.name=name self.ssn=ssn def __str__(self): # YOUR CODE STARTS HERE return f"Person({self.name}, ***-**-{self.ssn.split('-')[-1]})" __repr__ = __str__ def get_ssn(self): # YOUR CODE STARTS HERE return self.ssn def __eq__(self, other): # YOUR CODE STARTS HERE if self.ssn==other: return True else: return False class Staff(Person):
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help