Day 12: Inheritance

  • Instructions:
    • Given 2 classes, Person and Student where:
      • Person is the base class and Student is the derived class
    • Complete the Student class by writing the following:
      • A Student constructor with 4 parameters:
      • 2 strings firstName & lastName
      • an integer idNumber
      • an integer array (or vector) of test scores, scores
    • A calculate() method that calculates a Student object’s average and returns the grade character representative of their calculated average
    • Constraints:
      • 1 <= length of firstName
      • length of lastName <= 10
      • length of idNumber <= 7
      • 0 <= score <= 100
    Excercise
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    
    class Person:
        def __init__(self, firstName, lastName, idNumber):
            self.firstName = firstName
            self.lastName = lastName
            self.idNumber = idNumber
        def printPerson(self):
            print("Name:", self.lastName + ",", self.firstName)
            print("ID:", self.idNumber)
    
    class Student(Person):
        #   Class Constructor
    
        #   Parameters:
        #   firstName - A string denoting the Person's first name.
        #   lastName - A string denoting the Person's last name.
        #   id - An integer denoting the Person's ID number.
        #   scores - An array of integers denoting the Person's test scores.
    
        # Write your constructor here
        
    
        #   Function Name: calculate
        #   Return: A character denoting the grade.
        #
        # Write your function here
    
    line = input().split()
    firstName = line[0]
    lastName = line[1]
    idNum = line[2]
    numScores = int(input()) # not needed for Python
    scores = list( map(int, input().split()) )
    s = Student(firstName, lastName, idNum, scores)
    s.printPerson()
    print("Grade:", s.calculate())
    
    Solution
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    
    class Person:
        def __init__(self, firstName, lastName, idNumber):
            self.firstName = firstName
            self.lastName = lastName
            self.idNumber = idNumber
        def printPerson(self):
            print("Name:", self.lastName + ",", self.firstName)
            print("ID:", self.idNumber)
    
    class Student(Person):
        #   Class Constructor
        #   
        #   Parameters:
        #   firstName - A string denoting the Person's first name.
        #   lastName - A string denoting the Person's last name.
        #   id - An integer denoting the Person's ID number.
        #   scores - An array of integers denoting the Person's test scores.
        #
        # Write your constructor here
        def __init__(self,firstName,lastName,idNumber, scores):
            super().__init__(firstName, lastName, idNumber)
            self.scores = scores
        #   Function Name: calculate
        #   Return: A character denoting the grade.
        #
        # Write your function here
        def calculate(self):
            average = sum(self.scores)/len(self.scores)
            if 90 <= average <=100:
                return('O')
            elif 80 <= average < 90:
                return('E')
            elif 70 <= average < 80:
                return('A')
            elif 55 <= average < 70:
                return('P')
            elif 40 <= average < 55:
                return('D')
            elif average < 40:
                return('T')
    
    line = input().split()
    firstName = line[0]
    lastName = line[1]
    idNum = line[2]
    numScores = int(input()) # not needed for Python
    scores = list( map(int, input().split()) )
    s = Student(firstName, lastName, idNum, scores)
    s.printPerson()
    print("Grade:", s.calculate())