Question: What do. Without using import. Task 1 - Course A Course represents a specific course record as those appeared on your transcript. class Course: Define

What do. Without using import.

Task 1 - Course 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).

o number :: str. Represents course number, like "CS112" or "MATH113".

o credit :: int. Represents credit hours. You can assume it will always be a positive integer.

o 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.

Task 2 - Transcript 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. Note: you can compare two lists l1 and l2 directly as l1==l2 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.

Task 3 CourseError

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 so.

Example Interactions

>>> c1 = Course("CS112",4,"A") >>> c1.is_passing() :True >>> c2 = Course("ENGH101",3,"B") >>> c1==c2 False

>>> c3 = Course("MATH113",4,"IP") >>> t = Transcript() >>> str(t) 'Transcript: ' >>> t.add_course(c1) >>> t.add_course(c2)

>>> t.add_course(c3) >>> str(t) 'Transcript: \tCS112: credit 4, grade A \tENGH101: credit 3, grade B \tMATH113: credit 4, grade IP '

>>> print(str(t))

Transcript:

CS112: credit 4, grade A

ENGH101: credit 3, grade B

MATH113: credit 4, grade IP

>>> t.add_course(Course("GAME101",3,"A")) >>> t.course_by_number("CS112") <__main__.Course object at 0x10077b208>

>>> print(t.course_by_number("CS112")) >>> t.course_by_number("CS114") >>> print(t.course_by_number("CS114")) CS112: credit 4, grade A None

>>> ans = t.course_by_grade("A") >>> len(ans) 2 >>> c = Course("CS101",2,"Y") # this should raise a CourseError and crash Traceback (most recent call last): File "", line 1, in

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!