HackerRank Day 12 Inheritance 30 days of code solution YASH PAL, 31 July 2024 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. Problem solution in Python 2 programming. 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
Using lengthy if else is not ideal. Grades can be stored in an object to return the grade. `calculate() { let sum = this.scores.reduce((acc, num) => { acc += num; return acc; }); let avg = sum / this.scores.length, grades = {'90':'O', '80':'E', '70': 'A', '55': 'P', '40': 'D'}, grade = 'T'; for(let key in grades) { if(avg >= parseInt(key)) { grade = grades[key]; } } return grade; }`