Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Day 12 Inheritance 30 days of code solution

YASH PAL, 31 July 202419 October 2025

HackerRank Day 12 Inheritance solution – In this HackerRank Day 12 Inheritance 30 days of code problem set, we have two classes, Person and Student. where the Person is the base class and the Student is the derived class. we need to inherit all the data from Person class and use them in Student class.

Objective
Today, we’re delving into Inheritance. Check out the attached tutorial for learning materials and an instructional video.

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:
A string, firstName.
A string, lastName.
An integer, idNumber.
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:

  • O 90 <= a <= 100
  • E 80 <= a < 90
  • A 70 <= a < 80
  • P 55 <= a < 70
  • D 40 <= a < 55
  • T a < 40

Input Format

The locked stub code in the editor reads the input and calls the Student class constructor with the necessary arguments. It also calls the calculate method which takes no arguments.

The first line contains firstName, lastName, and idNumber, separated by a space. The second line contains the number of test scores. The third line of space-separated integers describes scores.

Constraints
1 <= length of firstName, length of lastName <= 10
length of idNumber = 7
0 <= score <= 100

Output Format

Output is handled by the locked stub code. Your output will be correct if your Student class constructor and calculate() method are properly implemented.

HackerRank Day 12 Inheritance
HackerRank Day 12 Inheritance

Inheritance Problem solution in Python 2.

class Student(Person):
    def __init__(self, firstName, lastName, idNumber, scores):
        self.firstName = firstName
        self.lastName = lastName
        self.idNumber = idNumber
        self.scores = map(int, scores)

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

Problem solution in Python 3 programming.

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

    def __init__(self,firstName, lastName, idNum , scores):

        self.firstName = firstName
        self.lastName = lastName
        self.idNum = idNum
        self.scores = scores
    
    def printPerson(self):
        print("Name: " + lastName + ", " + firstName)
        print("ID:", idNum)
    
    def calculate(self):
        x = len(scores)
        n = 0
        for i in range(x):
            n+=scores[i]
        
        y = int(n)//int(x)

        if 90<= y <= 100:
            return 'O'
        elif 80<= y < 90:
            return 'E'
        elif 70<= y < 80:
            return 'A'
        elif 55<= y < 70:
            return 'P'
        elif 40<= y < 55:
            return 'D'
        else:
            return 'T'
            

Problem solution in java programming.

class Student extends Person{
    private int[] testScores;
    
    Student(String firstName, String lastName, int identification, int[] scores){
		super(firstName, lastName, identification);
		this.testScores = scores;
	}
	
	public char calculate(){
		int average = 0;
		for(int i = 0; i < testScores.length; i++){
			average += testScores[i];
		}
		average = average / testScores.length;
		
		if(average >= 90){
			return 'O'; // Outstanding
		}
		else if(average >= 80){
			return 'E'; // Exceeds Expectations
		}
		else if(average >= 70){
			return 'A'; // Acceptable
		}
		else if(average >= 55){
			return 'P'; // Dreadful
		}
		else if(average >= 40){
			return 'D'; // Dreadful
		}
		else{
			return 'T'; // Troll
		}
	}
}

Problem solution in c++ programming.

class Student : public Person{
	private:
    vector<int> testScores;
    
	public:
    // Write your constructor
    Student(string firstName, string lastName, int id, vector<int> scores) : Person(firstName, lastName, id) {
        this->testScores = scores;
    }
    
    // Write char calculate()
    char calculate() {
        int sumScore = 0;
        for (int i = 0; i < testScores.size(); i++) {
            sumScore += testScores[i];
        }
        int averageScore = sumScore / testScores.size();
        
        if (averageScore <= 100 && averageScore >= 90) {
            return 'O';
        } else if (averageScore < 90 && averageScore >= 80) {
            return 'E';
        } else if (averageScore < 80 && averageScore >= 70) {
            return 'A';
        } else if (averageScore < 70 && averageScore >= 55) {
            return 'P';
        } else if (averageScore < 55 && averageScore >= 40) {
            return 'D';
        }
        return 'T';
    }
};

Problem solution in Javascript 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
  constructor(firstname, lastname, id, scores) {
    super(firstname, lastname, id);
    this.scores = scores;
  }

    /*	
    *   Method Name: calculate
    *   @return A character denoting the grade.
    */
    // Write your method here
  calculate() {
    var sum = this.scores.reduce((acc, num) => {
      acc += num;
      return acc;
    });
    var avg = sum / this.scores.length;
    
    if (avg >= 90) {
      return "O";
    }
    else if (avg >= 80) {
      return "E";
    }
    else if (avg >= 70) {
      return "A";
    }
    else if (avg >= 55) {
      return "P";
    }
    else if (avg >= 40) {
      return "D";
    }
    else {
      return "T";
    }
  }
    
}

30 days of code coding problems solutions HackerRank

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes