## Algorithm

Problem Name: 30 days of code - Day 12: Inheritance

In this HackerRank in 30 Days of Code Day 12: Inheritance problem solution,

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

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 4 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:

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 & 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
``````

Explanation

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

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````
#include <iostream>
#include <vector>

using namespace std;

class Person {
protected:
string firstName;
string lastName;
int id;
public:
Person(string firstName, string lastName, int identification) {
this->firstName = firstName;
this->lastName = lastName;
this->id = identification;
}

void printPerson() {
cout << "Name: " << lastName << ", " << firstName << "\nID: " << id << "\n";
}

};

class Student : public Person {
private:
vector<int> testScores;
public:
Student(string firstName, string lastName, int id, vector<int> scores) : Person(firstName, lastName, id) {
this->testScores = scores;
}

// Write char calculate()
char calculate() {
int total = 0;

for (int i = 0; i  <  this->testScores.size(); i++)
total += this->testScores[i];

int avg = (int) (total / testScores.size());

if (avg >= 90 && avg  < = 100) return 'O';
if (avg >= 80 && avg < 90) return 'E';
if (avg >= 70 && avg  <  80) return 'A';
if (avg >= 55 && avg < 70) return 'P';
if (avg >= 40 && avg  <  55) return 'D';
return 'T';
}
};

int main() {
string firstName;
string lastName;
int id;
int numScores;
cin >> firstName >> lastName >> id >> numScores;
vector<int> scores;
for (int i = 0; i  <  numScores; i++) {
int tmpScore;
cin >> tmpScore;
scores.push_back(tmpScore);
}
Student *s = new Student(firstName, lastName, id, scores);
s->printPerson();
cout << "Grade: " << s->calculate() << "\n";
return 0;
}
``````
Copy The Code &

### #2 Code Example with C# Programming

```Code - C# Programming```

``````
using System;

class Person
{
protected string firstName;
protected string lastName;
protected int id;

public Person() { }
public Person(string firstName, string lastName, int identification)
{
this.firstName = firstName;
this.lastName = lastName;
this.id = identification;
}
public void printPerson()
{
Console.WriteLine("Name: " + lastName + ", " + firstName + "\nID: " + id);
}
}

class Student : Person
{
int[] testScores;

public Student(string firstName, string lastName, int identification, int[] testScores) : base(firstName, lastName, identification)
{
this.testScores = testScores;
}

public char Calculate()
{
int total = 0;

foreach (int testScore in testScores) total += testScore;

int avg = total / testScores.Length;

if (avg >= 90 && avg  < = 100) return 'O';
if (avg >= 80 && avg < 90) return 'E';
if (avg >= 70 && avg  <  80) return 'A';
if (avg >= 55 && avg < 70) return 'P';
if (avg >= 40 && avg  <  55) return 'D';
return 'T';
}
}

class Solution
{
static void Main()
{
string firstName = inputs[0];
string lastName = inputs[1];
int id = Convert.ToInt32(inputs[2]);
int[] scores = new int[numScores];
for (int i = 0; i  <  numScores; i++)
{
scores[i] = Convert.ToInt32(inputs[i]);
}

Student s = new Student(firstName, lastName, id, scores);
s.printPerson();
Console.WriteLine("Grade: " + s.Calculate() + "\n");
}
}
``````
Copy The Code &

### #3 Code Example with Java Programming

```Code - Java Programming```

``````
import java.util.Scanner;

class Person {
protected String firstName;
protected String lastName;
protected int idNumber;

// Constructor
Person(String firstName, String lastName, int identification) {
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = identification;
}

// Print person data
public void printPerson() {
System.out.println("Name: " + lastName + ", " + firstName + "\nID: " + idNumber);
}
}

class Student extends Person {
private int[] testScores;

Student(String firstName, String lastName, int identification, int[] testScores) {
super(firstName, lastName, identification);

this.testScores = testScores;
}

char calculate() {
int total = 0;

for (int testScore : testScores) total += testScore;

int avg = total / testScores.length;

if (avg >= 90 && avg  < = 100) return 'O';
if (avg >= 80 && avg < 90) return 'E';
if (avg >= 70 && avg  <  80) return 'A';
if (avg >= 55 && avg < 70) return 'P';
if (avg >= 40 && avg  <  55) return 'D';
return 'T';
}
}

public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String firstName = scan.next();
String lastName = scan.next();
int id = scan.nextInt();
int numScores = scan.nextInt();
int[] testScores = new int[numScores];
for (int i = 0; i  <  numScores; i++) {
testScores[i] = scan.nextInt();
}
scan.close();

Student s = new Student(firstName, lastName, id, testScores);
s.printPerson();
}
}
``````
Copy The Code &

### #4 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
'use strict';

var _input = '';
var _index = 0;
process.stdin.on('data', (data) => {
_input += data;
});
process.stdin.on('end', () => {
_input = _input.split(new RegExp('[ \n]+'));
main();
});

return _input[_index++];
}

/**** Ignore above this line. ****/

class Person {
constructor(firstName, lastName, identification) {
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = identification;
}

printPerson() {
console.log(
"Name: " + this.lastName + ", " + this.firstName +
"\nID: " + this.idNumber
)
}
}

class Student extends Person {

constructor(firstName, lastName, identification, scores) {
super(firstName, lastName, identification);
this.testScores = scores;
}

calculate() {
let average = this.testScores.reduce(
function (a, b) {
return a + b
},
0
) / this.testScores.length

if (average >= 90) {
return 'O'
} else if (average >= 80) {
return 'E'
} else if (average >= 70) {
return 'A'
} else if (average >= 55) {
return 'P'
} else if (average >= 40) {
return 'D'
} else {
return 'T'
}
}
}

function main() {
let testScores = new Array(numScores)

for (var i = 0; i  <  numScores; i++) {
}

let s = new Student(firstName, lastName, id, testScores)
s.printPerson()
s.calculate()
}
``````
Copy The Code &

### #5 Code Example with Python Programming

```Code - Python Programming```

``````
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[0]
lastName = line[1]
idNum = line[2]
numScores = int(input())
scores = list(map(int, input().split()))
s = Student(firstName, lastName, idNum, scores)
s.printPerson()