In this HackerRank Day 12: Inheritance problem solution 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 the Student inherits all the properties of the Person.


HackerRank Day 12: Inheritance problem solution


Problem solution in Python programming.

class Student(Person):
    def __init__(self,firstName,lastName,idNum,scores):
        self.firstName = firstName
        self.lastName = lastName
        self.idNumber = idNum
        self.scores = [int(score) for score in scores]
        #print(scores)
        #print(self.scores)
        
    def calculate(self):
        scores_average = sum(self.scores)/len(self.scores)
        if scores_average < 40:
            return 'T'
        elif scores_average < 55:
            return 'D'
        elif scores_average < 70:
            return 'P'
        elif scores_average < 80:
            return 'A'
        elif scores_average < 90:
            return 'E'        
        else:
            return 'O'



Problem solution in Java programming.

class Student extends Person{
	private int[] testScores;
    
    Student(String firstName, String lastName, int idNumber, int[] marks){
        super(firstName, lastName, idNumber);
       // super.printPerson();
      testScores = Arrays.copyOf(marks, marks.length);
    }
    public char calculate(){
        int sum=0;
        char grade;
        for(int a: testScores){
            sum += a;
        }
        float avg = sum/testScores.length;
        if( avg <= 100 & avg >=90){
            grade = 'O';
        } else if(avg <90 & avg >=80){
            grade = 'E';
        } else if(avg <80 & avg >= 70){
            grade = 'A';
        } else if(avg < 70 & avg >=55){
            grade = 'P';
        } else if( avg < 55 & avg >= 40){
            grade = 'D';
        } else {
            grade = 'T';
        }
        return grade;
    }
    
   
}




Problem solution in C++ programming.

class Student:  public Person{
	private:
		vector<int> testScores;
	public:
  		// Write your constructor
    Student (string firstName, string lastName, int identification, vector<int> testScores) : Person (firstName, lastName, identification) {
            this->firstName = firstName;
			this->lastName = lastName;
			this->id = identification;
            this->testScores = testScores;
        }
        // Write char calculate()
        char calculate() {
            int s = this->testScores.size();
            int gradeSum = 0;
            for (int x=0; x<s; x++) {
                gradeSum += this->testScores[x];
                //cout << "x: " << x << " tSx: " << this->testScores[x] << "\n";
            }
            double average = gradeSum/s;
            //cout << "gs: " << gradeSum << " avg: " << average << "\n";
            if (average <= 100  && average >= 90) {
                return 'O';
            }
            if (average < 90  && average >= 80) {
                return 'E';
            }
            if (average < 80  && average >= 70) {
                return 'A';
            } 
            if (average < 70  && average >= 55) {
                return 'P';
            } 
            if (average < 55  && average >= 40) {
                return 'D';
            } 
            if (average < 40) {
                return 'T';
            }
            else { return 'X';}
        }
        
};



Problem solution in Java programming.

class Student extends Person {
    /*	
    *   Class Constructor
    *   
    *   @param firstName - A string denoting the Person's first name.
    *   @param lastName - A string denoting the Person's last name.
    *   @param id - An integer denoting the Person's ID number.
    *   @param scores - An array of integers denoting the Person's test scores.
    */
    // Write your constructor here

    /*	
    *   Method Name: calculate
    *   @return A character denoting the grade.
    */
    // Write your method here
    constructor(firstName, lastName, id, scores){
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.idNumber = id;
        this.scores = scores;
    }
    
    calculate() {
        var avg = this.scores.reduce((a,b) => { 
            return a + b;
        }) / this.scores.length;
        
        if (avg < 40)
            return 'T';
        else if (avg < 55)
            return 'D';
        else if (avg < 70)
            return 'P';
        else if (avg < 80)
            return 'A';
        else if (avg < 90)
            return 'E';
       
        return 'O';
    }
}