Home30 Days Of CodeHackerRank Day 12 : Inheritance 30 days of code solution

HackerRank Day 12 : Inheritance 30 days of code solution

Today we are going to solve HackerRank Day 12 : Inheritance 30 days of code solution in C++ , Java , Python & Javascript.

Objective

Today, we’re delving into Inheritance. 

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:

  • Student class constructor, which has 4 parameters:
    1. A string, firstName.
    2. A string, lastName.
    3. An integer, idNumber.
    4. An integer array (or vector) of test scores, scores.
  • char calculate() method that calculates a Student object’s average and returns the grade character representative of their calculated average:

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.

Sample Input

Heraldo Memelli 8135627
2
100 80

Sample Output

 Name: Memelli, Heraldo
 ID: 8135627
 Grade: O

Explanation

This student had 2 scores to average: 100 and 80. The student’s average grade is (100 + 800) / 2 = 90. An average grade of 90 corresponds to the letter grade O, so the calculate() method should return the character'O'.


HackerRank Day 12 : Inheritance 30 days of code solution

Inheritance HackerRank Solution in C ++

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';
    }
};

Inheritance HackerRank Solution in Java

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
		}
	}
}

Inheritance HackerRank Solution in Python 3

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'

Inheritance HackerRank Solution in JavaScript

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";
    }
  }
    
}

NEXT : HackerRank Day 12 : Inheritance 30 days of code solution

30 Days of Code HackerRank Solutions List – Day 0 to Day 29


Read More –

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

- Advertisment -

Categories