Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python 3 Coding Classes and Functions: A Course represents a specific course record as those appeared on your transcript. class Course: Define the Course class.
Python 3 Coding Classes and Functions:
A Course represents a specific course record as those appeared on your transcript. class Course: Define the Course class. def _init_ (self, number, credit, grade): Course constructor. All three parameters must be stored to instance variables of the same names (number, credit, and grade). number:: str. Represents course number, like "CS112" or "MATHH3". credit:: int. Represents credit hours. You can assume it will always be a positive integer. grade:: str. Represents letter grade. Can only be 'A', 'B', 'C', 'D', 'F', or 'IP'. If the provided grade is not one of these (for example, grade == "K"), then raise a CourseError with the message "bad grade ' K'. (You can skip this exception-raising part until later). def _str (self): returns a human-centric string representation. If number=="cs112", credit==4, and grade=="A", then the returned string must be "CS112: credit 4, grade A" (note the four single spaces). def _eq (self, other): we want to check that two courses are equal (our self and this other course). We compare each instance variable like so (this is the definition!) return self.number==other.number and self.credit==other.credit and self.credit==other.credit def is_passing(self): checks whether this course has passed or not according to grade. Return False if the grade is 'F' or 'IP'; return True otherwise. This represents a group of Course values as a list (named courses). We can then dig through this list for useful course information and calculations by calling the methods we're going to implement. class Transcript: Define the Transcript class. def _init (self): Transcript constructor. Create the only instance variable, a list named courses, and initialize it to an empty list. This means that we can only create an empty Transcript and then add items to it later on. def _str _(self): returns a human-centric string representation. We'll choose a multi-line representation (slightly unusual) that contains "Transcript: " on the first line, and then each successive line is a tab, the str () representation of the next Course object in self. courses, and then a newline each time. This means that the last character of the string is guaranteed to be a newline (regardless of whether we have zero or many Course values). def _eq (self, other): we want to check that two transcripts are equal (our self and this other transcript). The only things that we need to compare are their lists of courses. you can compare two lists 11 and 12 directly as 11==12 but this will rely on your implementation of eq () for Course class since we are comparing two lists of courses. def add_course (self, course): append the argument course to the end of self .courses. In order to ensure every course number is unique, if our transcript already has a course of the same number (for example 'ENGH101'), raise a CourseError with the message "duplicate course number ' ENGH101. (You can skip this exception-raising part until later). def course_by_number(self, number): Look through all stored Course objects in self .courses. Return the Course object whose number matches the number argument. If no match, return None. You can assume that every course number is unique in a transcript. def course_by_grade(self, grade): search through self.courses in order, return a list of Course object in this Transcript that is of this grade. If no match, return an empty list. def total_passing_credit(self): Look through all stored Course objects in self .courses. Return total credit hours of all courses that have a passing grade. The CourseError class extends the notion of an Exception (note the (Exception) part of the class signature line). We're making our own exception that stores a single string message. class CourseError (Exception): Define the CourseError class to be a child of Exception class (by putting Exception in the parentheses as shown). def _init _(self, msg): Constructor. Store msg to an instance variable named msg. def _str _(self): human-centric representation of CourseError is just self .msg If you skipped the two exception-raising parts above, you should now go back and add those checks/raises. The test cases for CourseError will go back and check if those parts correctly did soStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started