CMPSC-132: Programming and Computation II
Homework 2 (100 points) Due date: June 13rd, 2021, 11:59 PM EST
Goal: The goal of this assignment is to reinforce the fundamental concepts of object-oriented programming in Python. Through this homework, you should gain a better idea of how to implement a system of classes that build on each other.
General instructions:
• The work in this assignment must be your own original work and be completed alone.
• The instructor and course assistants are available on Teams and with office hours to answer any questions you may have. You may also share testing code on Teams.
• A doctest is provided to ensure basic functionality and may not be representative of the full range of test cases we will be checking. Further testing is your responsibility.
• Debugging code is also your responsibility.
• You may submit more than once before the deadline; only the latest submission will be grad ed .
Assignment-specific instructions:
• Download the starter code file from Canvas. Do not change the function names or given starter code in your script. At the end of the starter code, there are instructions on how to run the doctest per class
• Each class has different requirements, read them carefully and ask questions if you need clarification. No credit is given for code that does not follow directions.
• All methods that output a string must return the string, not print it. Code will not receive credit if you use print to display the output
• If you are unable to complete a method, use the pass statement to avoid syntax errors
Submission format:
• Submit your HW2.py file to the Homework 2 Gradescope assignment before the due date.
• As a reminder, code submitted with syntax errors does not receive credit, please run your
file before submitting.
Section 1: Assignment overview
The main concept is to implement classes that represent an in-memory “school database”. There are eight classes you must implement: Course, Catalog, Semester, Loan, Person, Staff, Student and StudentAccount. Each class has its own purpose which will be explained below. You will also implement a standalone function that will help transform a Person into a Student.
• Course (no dependencies) o Represents a course, with attributes for id, name, and number of credits.
• Catalog (Course class must be implemented) o Stores a collection of Course objects through a dictionary, using id as the key.
• Semester (Course class must be implemented)
Stores a collection of Course objects taken together during a semester.
(no dependencies)
Represents an amount of money, with attributes for id and the loan amount.
• StudentAccount (Student class must be implemented)
o Represents the financial status of a student.
• Person (no dependencies)
o Represents a person, with attributes for a name and social security number.
• Staff (subclass of Person) (Person class must be implemented)
o Represents a person who is a staff member and has a supervisor.
• Student (subclass of Person) (All classes must be implemented)
o Represents a person who is a student that takes courses at the university.
A non-comprehensive list of concepts you should know to complete this assignment is:
• Basic class syntax / definition in Python (attributes, methods, definitions)
• Special/Magic methods
• Dictionaries
• Encapsulation
• Polymorphism through Operator and Method Overloading
• Inheritance
• Property methods
• Firm understanding of what it means for Python to be an “Object-Oriented” language
• Loan o
o
Section 2: The Course class
A simple class that stores the id, name, and number of credits for a class. Attributes
Type
Name
Description
str cid
str cname int credits
Special methods
str __str__(self)
str __repr__(self) bool __eq__(self, other)
__str__(self), __repr__(self)
Stands for course id, uniquely identifies a course like “CMPSC132”. Stands for course name and is the long form of the course title.
The number of credits a course is worth.
Type
Name
Description
Returns a formatted summary of the course as a string. Returns the same formatted summary as __str__.
Does an equality check based only on course id.
Returns a formatted summary of the course as a string. The format to use is: cid(credits): cname
Output
str Formatted summary of the course.
__eq__(self, other)
Determines if two objects are equal. For instances of this class, we will define equality when the course id of one object is the same as the course id of the other object. You can assume at least one of the objects is a Couse object.
Input (excluding self)
any other The object to check for equality against a Course object.
Output
bool True if other is a Course object with the same course id, False otherwise.
Section 3: The Catalog class
Stores a collection of Course objects as a dictionary, accessible by their ids. Attributes
dict courseOfferings Stores courses with the id as the key and the course as the value. Methods
str addCourse(self, cid, cname, credits) Adds a course with the given information. str removeCourse(self, cid) Removes a course with the given id.
addCourse(self, cid, cname, credits)
Creates a Course object with the parameters and stores it as a value in courseOfferings. Inputs (excluding self)
Type
Name
Description
Type
Name
Description
str cid
str cname int credits
The id of the course to add.
The name of the course to add
The number of credits the course is worth.
Output
str “Course added successfully”
str “Course already added” if course is already in courseOfferings.
removeCourse(self, cid)
Removes a course with the given id.
Input (excluding self)
str cid The id of the course to remove.
Output
str “Course removed successfully”
str “Course not found” if a course with the given id is not in the dictionary.
Section 4: The Semester class
Stores a collection of Course objects for a semester for a student. Attributes
int sem_num The semester number for this object.
dict courses Stores courses to be taken in the semester. The id of the course as the
Type
Name
Description
key and the Course object as the value.
Methods
(many) addCourse(self, course) (many) dropCourse(self, course) int totalCredits(self)
bool isFullTime(self)
Special methods
Adds a Course to the courses dictionary
Removes a course from courses.
A property method for the total number of credits.
A property method that returns True if this is full-time.
Type
Name
Description
Type
Name
Description
str __str__(self) Returns a formatted summary of the all the courses in this semester. str __repr__(self) Returns the same formatted summary as __str__.
addCourse(self, course)
Adds a Course to the courses dictionary Input (excluding self)
Course
Output None str
course The Course object to add to this semester.
(Normal operation does not output anything)
“Course already added” if the course is already in this semester.
dropCourse(self, course)
Removes a course from this semester. Input (excluding self)
Course
Output None str
course The Course object to remove from this semester.
Normal operation does not output anything
“No such course” if the course is not in this semester.
Section 4: The Semester class
totalCredits(self)
A property method (behaves like an attribute) for the total number of credits in this semester.
Outputs (normal)
int Total number of enrolled credits in this semester.
isFullTime(self)
A property method (behaves like an attribute) that checks if a student taking this semester would be considered full-time (taking 12 or more credits) or not.
Outputs (normal)
bool True if there are 12 or more credits in this semester, False otherwise.
__str__(self), __repr__(self)
Returns a formatted summary of the all the courses in this semester. Use the format: cid, cid, cid, …
Output
str Formatted summary of the courses in this semester. str “No courses” if the semester has no courses.
Section 5: The Loan class
A class that represents an amount of money, identified by a pseudo-random number. Attributes
int loan_id The id for this loan, generated pseudo-randomly by __getloanID int amount The amount of money loaned.
Type
Name
Description
Methods
int __getloanID(self)
Special methods
str __str__(self) str __repr__(self)
__str__(self), __repr__(self)
A property method that pseudo-randomly generates loan ids.
Returns a formatted summary of the loan as a string. Returns the same formatted summary as __str__.
Type
Name
Description
Type
Name
Description
Returns a formatted summary of the loan as a string. Use the format: Balance: $amount
Output
str Formatted summary of the loan.
__getloanID(self)
A property method (behaves like an attribute) that pseudo-randomly generates loan ids. Use the random module to return a number between 10,000 and 99,999. The returned value should be saved to loan_id when initializing Loan objects. randint and randrange could be helpful here!
Outputs (normal)
int Pseudo-randomly generated id.
Section 6: The StudentAccount class
This class represents a financial status of the student based on enrollment and is saved to a Student object as an attribute. This class should also contain an attribute that stores the price per credit, initially $1000/credit. This cost can change at any time and should affect the future price of enrollment for ALL students.
Attributes
Student numerical dict
Methods
numerical numerical
student balance loans
The Student object that owns this StudentAccount.
The balance that the student has to pay.
A dictionary that stores Loan objects accessible by their loan_id.
Type
Name
Description
Type
Name Description
makePayment(self, amount) Makes a payment towards the balance. chargeAccount(self, amount) Adds an amount towards the balance.
Special methods
str __str__(self) Returns a formatted summary of the loan as a string. str __repr__(self) Returns the same formatted summary as __str__.
makePayment(self, amount)
Makes a payment by subtracting amount from the balance.
Input (excluding self)
numerical amount The payment amount towards the balance.
Output
numerical Current balance amount.
chargeAccount(self, amount)
Adds amount towards the balance.
Inputs (excluding self)
int amount The amount to add to the balance.
Output
int Updated balance amount.
__str__(self), __repr__(self)
Returns a formatted summary of the loan as a string. The format to use is (spread out over three lines):
Type
Name
Description
Name: name ID:id
Balance: balance
Output
str Formatted summary of the account.
Section 7: The Person class
This class is a basic representation of a person, storing name and social security number. Attributes
Type
Name
Description
str name str ssn
Methods
str get_ssn(self)
Special methods
str __str__(self)
str __repr__(self) bool __eq__(self, other)
get_ssn(self)
Full name of the person.
Private attribute of social security number formatted as “123-45-6789”.
Getter method for accessing social security number.
Type
Name
Description
Type
Name
Description
Returns a formatted summary of the person as a string. Returns the same formatted summary as __str__.
Checks for equality by comparing only the ssn attributes.
Getter method for accessing the private social security number attribute. Output
str Social security number.
__str__(self), __repr__(self)
Returns a formatted summary of the person as a string. The format to use is: Person(name, ***-**-last four digits)
Output
str Formatted summary of the person.
__eq__(self, other)
Determines if two objects are equal. For instances of this class, we will define equality when the SSN of one object is the same as SSN of the other object. You can assume at least one of the objects is a Person object.
Input (excluding self)
many other The other object to check for equality with.
Output
bool True if other is a Person object with the same SSN, False otherwise.
Section 8: The Staff class
This class inherits from the Person class but adds extended functionality for staff members. Attributes, methods, and special methods inherited from Person are not listed in this section.
Attributes
Staff supervisor
Methods
Type Name
A private attribute for this person’s supervisor. By default, set to None.
Description
Property method for generating staff’s id. Updates the private supervisor attribute. Property method for getting the supervisor. Applies a hold on a student object. Removes a hold on a student object.
Sets a student’s status to not active.
Type
Name
Description
str id(self)
(many) setSupervisor(self, new_supervisor) (many) getSupervisor(self)
(many) applyHold(self, student)
(many) removeHold(self, student)
(many) unenrollStudent(self, student)
Special methods
Type
Name
Description
str __str__(self) Returns a formatted summary of the staff as a string. str __repr__(self) Returns the same formatted summary as __str__.
id(self)
Property method (behaves like an attribute) for generating staff’s id.
The format should be: 905+initials+last four numbers of ssn. (e.g.: 905abc6789). Ignore the security flaws this generation method presents and assume ids are unique.
Output
str Generated id for the staff member.
setSupervisor(self, new_supervisor)
Updates the private supervisor attribute. Input (excluding self)
Staff
Output str None
new_supervisor The new value to set for the supervisor attribute.
“Completed!”
Nothing is returned if new_supervisor is not a Staff object.
getSupervisor(self)
Property method (behaves like an attribute) for getting the supervisor. Output
Staff Current value of supervisor.
Section 8: The Staff class
applyHold(self, student)
Applies a hold on a student object (set the student’s hold attribute to True). Input (excluding self)
Student
Output str None
student The student to apply a hold to.
“Completed!”
Nothing is returned if student is not a Student object.
removeHold(self, student)
Removes a hold on a student object (set the student’s hold attribute to False). Inputs (excluding self)
Student
Output str None
student The student to remove a hold from.
“Completed!”
Nothing is returned if student is not a Student object.
unenrollStudent(self, student)
Unenrolls a student object (set the student’s active attribute to False). Inputs (excluding self)
Student
Output str None
student The student to unenroll.
“Completed!”
Nothing is returned if student is not a Student object.
__str__(self), __repr__(self)
Returns a formatted summary of the staff member as a string. The format to use is: Staff(name, id)
Outputs (normal)
str Formatted summary of the staff member.
Section 9: The Student class
This class inherits from the Person class and is heavy extended for additional functionality. Attributes, methods, and special methods inherited from Person are not listed in this section.
Attributes
str
dict
bool
bool StudentAccount
Methods
Type
str StudentAccount (many)
str
str (many)
year semesters hold active account
Name
id(self)
__create StudentAccount (self) registerSemester(self) enrollCourse(self, cid, catalog, semester)
dropCourse(self, cid) getLoan(self, amount)
Type Name
Description
A string indicating the student’s year (“Freshman”, etc.).
A collection of Semester objects accessible by sem_num. Indicates a hold on the student’s account, defaults to False. Indicates if the student is actively enrolled, defaults to True. Creates and sets a StudentAccount object with the student’s information to keep track of the student’s balance.
Description
Property method for generating student’sid. Creates a StudentAccount object.
Creates a Semester object.
Enrolls the student in a course.
Drops a course from a semester. Creates a Loan object.
Special methods
Type Name
str __str__(self) str __repr__(self)
id(self)
Description
Returns a formatted summary of the staff as a string. Returns the same formatted summary as __str__.
Property method (behaves like an attribute) for generating student’s id.
The format should be: initials+last four numbers of ssn (e.g.: abc6789). Ignore the security flaws this generation method presents and assume ids are unique.
Output
str Student’s id.
__createStudentAccount(self)
Creates a StudentAccount object. This should be saved in the account attribute during initialization.
Output
StudentAccount Created StudentAccount object linked to the student. None Nothing is returned if student is not active.
Section 9: The Student class
registerSemester(self)
Creates a Semester object and adds it as a value to the semesters dictionary if the student is active and has no holds. It also updates the student’s year attribute according to the number of semesters enrolled. ‘Freshman’ is a first-year student (semesters 1 and 2), ‘Sophomore’ is a second-year student (semesters 3 and 4), ‘Junior’ is a third-year student (semesters 5 and 6) and ‘Senior’ for any enrollment with more than 6 semesters.
Output
None (Normal operation does not output anything)
str “Unsuccessful operation” is returned if the student is inactive or has holds.
enrollCourse(self, cid, catalog, semester)
Finds a Course object with the given id from the catalog and adds it to the courses attribute of the Semester object. Charge the student’s account the appropriate amount of money.
Inputs (excluding self) str cid Catalog catalog
int semester
Course ID to search for.
Catalog to search in.
The semester number to add the course to.
Output
“Course added successfully”
“Unsuccessful operation” is returned if the student is inactive or has holds. “Course not found” is returned if no course with the given id is found. “Course already enrolled” is returned if the course is already enrolled.
dropCourse(self, cid)
Finds a Course object from the current semester with the given id and removes it. When a course is dropped, only half the course cost is refunded to the student’s account. The current semester is defined as the last added Semester object in the semester dictionary.
Inputs (excluding self)
str cid Course ID to search for.
Output
“Course dropped successfully”
str “Unsuccessful operation” is returned if the student is inactive or has holds. “Course not found” is returned if no course with the given id is found.
str
Section 9: The Student class
getLoan(self, amount)
If the student is active and currently enrolled full-time (consider the item with the largest key in the semesters dictionary the current enrollment), it creates a Loan object for the student with the given amount, adds it to the student’s account’s loans dictionary, and uses the amount to make a payment in the student’s account. Do NOT remove the line random.seed(1) from the constructor. This ensures replicable pseudo-random number generation across multiple executions, so your loan ids should match the doctest samples.
Inputs (excluding self)
int amount The amount of money to get a loan for.
Output
None (Normal operation does not return anything)
str “Unsuccessful operation” is returned if the student is inactive.
str “Not full-time” is returned if the current semester is not full-time.
Section 10: The createStudent function
The createStudent function is a separate function that should be outside of any classes.
createStudent(person)
Creates a Student object from a Person object. The new student should have the same information (name, ssn) as the person and starts out as a freshman (“Freshman”).
Input
Person person The Person object to create a Student object from.
Output
Student The new student created from the input Person object.