The next problem is based on Inheritance. Inheritance is adapting the hierarchy of classes. The classes that inherits are called subclasses and the class inherited is called superclass.
You can read in detail what exactly is inheritance in Object Oriented Programming language from here
The few important concepts which you must be aware of while going through the concept of inheritance are:
- Different types of Inheritance java support
- Why java doesn’t support multiple inheritances?
- Concepts of superclass and subclass. Use of “super” keyword.
- What are interfaces and what are abstract classes ? etc….
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 Studentinherits all the properties of Person.
Complete the Student class by writing the following:
- A Student class constructor, which has parameters:
- A string, .
- A string, .
- An integer, .
- An integer array (or vector) of test scores, .
- A char calculate() method that calculates a Student object’s average and returns the grade character representative of their calculated average:
Heraldo Memelli 8135627 2 100 80
Name: Memelli, Heraldo ID: 8135627 Grade: O
So here the Solution you have landed here for..
Inheritance HackerRank solution in Python :
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, testScores): super().__init__(firstName, lastName, idNumber) self.testScores = testScores def calculate(self): total = 0 for testScore in self.testScores: total += testScore avg = total / len(self.testScores) if 90 <= avg <= 100: return 'O' if 80 <= avg < 90: return 'E' if 70 <= avg < 80: return 'A' if 55 <= avg < 70: return 'P' if 40 <= avg < 55: return 'D' return 'T' line = input().split() firstName = line lastName = line idNum = line numScores = int(input()) scores = list(map(int, input().split())) s = Student(firstName, lastName, idNum, scores) s.printPerson() print("Grade:", s.calculate())
Tip:- Always try to implement the own logic this will help you to solve and building a logic. Before copy the program I recommended please read this full article, this will help you to build your own logic.