In this challenge, you are given two classes, Person and Student. Person is the base class (superclass) and Student is the derived class (subclass). A derived class means that Student inherits all the methods and behaviors from the Person class. It can also declare it’s own new methods (as well as override superclass methods). In this case, the Person superclass might take a name and an age. Every person created has a name and an age. The Student class inherits these from the Person class because each Student also has a name and an age! However, the Student has it’s own new method that calculates a person’s grade as well.
Sample Input:
Heraldo Memelli 8135627
2
100 80
Sample Output:
Name: Memelli, Heraldo
ID: 8135627
Grade: O
Below is the Person class given to us. Each Person created has a first name, a last name, and an ID number.
class Person: #superclass
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)
line = input().split() #Sara Pesavento 3301872
firstName = line[0] #Sara
lastName = line[1] #Pesavento
idNum = line[2] #3301872
scores = list( map(int, input().split()) ) #90 100 80 85
s = Student(firstName, lastName, idNum, scores) #we will create this Student subclass later
s.printPerson() #a method in the Person class
print("Grade:", s.calculate()) #a method in the Student subclass
When we create our Student subclass, we will have it inherit all the methods and parameters of the Person superclass by defining it with class Student(Person):
class Person: #superclass
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):
pass
line = input().split() #Sara Pesavento 3301872
firstName = line[0] #Sara
lastName = line[1] #Pesavento
idNum = line[2] #3301872
scores = list( map(int, input().split()) ) #90 100 80 85
s = Student(firstName, lastName, idNum, scores) #we will create this Student subclass later
s.printPerson() #a method in the Person class
print("Grade:", s.calculate()) #a method in the Student subclass
Now, when we create our Student class constructor, we must include all the parameters inherited from the Person class (firstName, lastName, idNumber), plus our own that are unique to the Student subclass (scores).
class Person: #superclass
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
line = input().split() #Sara Pesavento 3301872
firstName = line[0] #Sara
lastName = line[1] #Pesavento
idNum = line[2] #3301872
scores = list( map(int, input().split()) ) #90 100 80 85
s = Student(firstName, lastName, idNum, scores) #we will create this Student subclass later
s.printPerson() #a method in the Person class
print("Grade:", s.calculate()) #a method in the Student subclass
Within our student subclass, we can create a unique method calculate that calculates a student’s grade based on their average score.
class Person: #superclass
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):
average = sum(self.scores)/len(self.scores)
if average <= 100 and average >= 90:
return "O"
elif average >= 80 and average < 90:
return "E"
elif average >= 70 and average < 80:
return "A"
elif average >= 55 and average < 70:
return "P"
elif average >= 40 and average < 55:
return "D"
else:
return "T"
line = input().split() #Sara Pesavento 3301872
firstName = line[0] #Sara
lastName = line[1] #Pesavento
idNum = line[2] #3301872
scores = list( map(int, input().split()) ) #90 100 80 85
s = Student(firstName, lastName, idNum, scores) #we will create this Student subclass later
s.printPerson() #a method in the Person class
print("Grade:", s.calculate()) #a method in the Student subclass
Notice at the bottom that an instance object s is created from the Student class. The printPerson() method from the Person class is called. Since the Student class inherits all the methods from the Person class, this can be done. Next, the calculate() method from the Student class is called.
Sample Solution:
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):
self.firstName = firstName
self.lastName = lastName
self.idNumber = 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 average <= 100 and average >= 90:
return "O"
elif average >= 80 and average < 90:
return "E"
elif average >= 70 and average < 80:
return "A"
elif average >= 55 and average < 70:
return "P"
elif average >= 40 and average < 55:
return "D"
else:
return "T"