DEV Community

M__
M__

Posted on

DAY 12: Inheritance

I’ve been MIA for a while because of I had some other things to take care of also going through the challenge I have met some concepts that have given me a tough time so I put them on hold for further research and keep going with the concepts I can grasp without so much hustle.

This challenge was focusing on inheritance of classes whereby a child class(derived) can get the properties and functions of the parent class(base). I learnt a number of new concepts while doing this challenge and as always, practice is key so that when it comes to tackling such questions or teaching the concept, things flow well.

The Task:
You are given two classes, Person and Student, where Person is the base class and Student is the derived class. Completed code for Person and a declaration for Student are provided for you in the editor. Observe that Student inherits all the properties of Person.

Complete the Student class by writing the following:
A Student class constructor, which has parameters:

  1. A string, firstName.
  2. A string, lastName.
  3. An integer, id.
  4. An integer array (or vector) of test scores, scores. A char calculate() method that calculates a Student object's average and returns the grade character representative of their calculated average:
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):
    def __init__(self, firstName, lastName, idNumber, scores):
        self.firstName = firstName
        self.lastName = lastName
        self.idNumber = idNumber
        self.scores = scores

    def calculate(self):
        self.ave = sum(self.scores)/numScores
        if self.ave >=90 and self.ave <= 100:
            return 'O'
        elif self.ave >= 80 and self.ave < 90:
            return 'E'
        elif self.ave >= 70 and self.ave < 80:
            return 'A'
        elif self.ave >= 55 and self.ave < 70:
            return 'P'
        elif self.ave >=  40 and self.ave < 55:
            return 'D'
        elif self.ave < 40:
            return 'T'

line = input().split()
firstName = line[0]
lastName = line[1]
idNum = line[2]
numScores = int(input())
scores = list( map(int, input().split()) )
s = Student(firstName, lastName, idNum, scores)
s.printPerson()
print("Grade:", s.calculate())

'''
Sample Input

Heraldo Memelli 8135627
2
100 80

Sample Output
Name: Memelli, Heraldo
ID: 8135627
Grade: O
'''
Enter fullscreen mode Exit fullscreen mode

Top comments (0)