Day 12: Inheritance In Java | 30 Days Of Code | Hackerrank Programming Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Day 12: Inheritance in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Link for the ProblemDay 12: Inheritance – Hacker Rank Solution

Day 12: Inheritance – Hacker Rank Solution

Problem:

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:

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

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 , , and , separated by a space. The second line contains the number of test scores. The third line of space-separated integers describes .

Constraints

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  scores to average:  and . The student’s average grade is . An average grade of  corresponds to the letter grade , so the calculate() method should return the character'O'.

Day 12: Inheritance – Hacker Rank Solution
public class Student extends Person {
	private int[] testScores;

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

		this.testScores = testScores;
	}

	public char calculate() {
		int sum = 0;
		for (int i = 0; i < testScores.length; i++) {
			sum += testScores[i];
		}
		int avg = (sum) / testScores.length;

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

	}

}

14 thoughts on “Day 12: Inheritance In Java | 30 Days Of Code | Hackerrank Programming Solutions”

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker🙏.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock