CMPSC-132: Programming and Computation II 2022
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CMPSC-132: Programming and Computation II
Homework 2
2022
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 oftest 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 graded.
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)
o Stores a collection of Course objects taken together during a semester.
• Loan (no dependencies)
o Represents an amount ofmoney, 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 staffmember 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 ofwhat it means for Python to be an “Object-Oriented” language
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 |
Stands for course id, uniquely identifies a course like “CMPSC132”. |
str |
cname |
Stands for course name and is the long form ofthe course title. |
int |
credits |
The number of credits a course is worth. |
Special methods
Type |
Name |
Description |
str |
__str__(self) |
Returns a formatted summary ofthe course as a string. |
str |
__repr__(self) |
Returns the same formatted summary as __str__. |
bool |
__eq__(self, other) |
Does an equality check based only on course id. |
__str__(self), __repr__(self)
Returns a formatted summary ofthe course as a string.
The format to use is: cid(credits): cname
Output |
|
str |
Formatted summary ofthe 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 ofthe 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 and their capacity as a dictionary, accessible by their ids.
Attributes
Type |
Name |
Description |
dict |
courseOfferings |
Stores courses with the id as the key and a tuple (Course, capacity) as the value. |
Methods
Type |
Name |
Description |
str |
addCourse(self, cid, cname, credits, capacity) |
Adds a course with the given information. |
str |
removeCourse(self, cid) |
Removes a course with the given id. |
addCourse(self, cid, cname, credits, capacity)
Creates a Course object with the parameters and stores it as a value in courseOfferings.
Inputs (excluding self) |
||
str |
cid |
The id ofthe course to add. |
str |
cname |
The name ofthe course to add |
int |
credits |
The number of credits the course is worth. |
int |
capacity |
The maximum number of students allowed to register in the course |
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 ofthe course to remove. |
Output |
|
str |
“Course removed successfully” |
str |
“Course not found” if a course with the given id is not in the dictionary. |
2022-02-11