Day 12: Inheritance Hackerrank Solution Java
For Explanation:
Sample Input
Heraldo Memelli 8135627
2
100 80
Sample Output
Name: Memelli, Heraldo
ID: 8135627
Grade: O
Code:
import java.util.*;
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 idNumber,int[] scores){ super(firstName,lastName,idNumber); this.testScores = scores; } // Write your constructor here char calculate(){ int sum = 0; for(int i=0;i<testScores.length;i++){ sum = sum + testScores[i]; } int avg = sum/testScores.length; if(avg>=90 && avg<=100){ return 'O'; }else if(avg<90 && avg>=80){ return 'E'; }else if(avg<80 && avg>=70){ return 'A'; }else if(avg<70 && avg>=55){ return 'P'; }else if(avg<55 && avg>=40){ return 'D'; }else if(avg<40){ return 'T'; } return 'd'; }
}
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(); System.out.println("Grade: " + s.calculate() ); }}
Comments
Post a Comment