Skip to content

Commit aed43f6

Browse files
authored
Create HackerRankInheritance.java
1 parent 9b79232 commit aed43f6

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import java.util.*;
2+
3+
class Person {
4+
protected String firstName;
5+
protected String lastName;
6+
protected int idNumber;
7+
8+
// Constructor
9+
Person(String firstName, String lastName, int identification){
10+
this.firstName = firstName;
11+
this.lastName = lastName;
12+
this.idNumber = identification;
13+
}
14+
15+
// Print person data
16+
public void printPerson(){
17+
System.out.println(
18+
"Name: " + lastName + ", " + firstName
19+
+ "\nID: " + idNumber);
20+
}
21+
22+
}
23+
24+
class Student extends Person{
25+
private int[] testScores;
26+
27+
/*
28+
* Class Constructor
29+
*
30+
* @param firstName - A string denoting the Person's first name.
31+
* @param lastName - A string denoting the Person's last name.
32+
* @param id - An integer denoting the Person's ID number.
33+
* @param scores - An array of integers denoting the Person's test scores.
34+
*/
35+
// Write your constructor here
36+
37+
/*
38+
* Method Name: calculate
39+
* @return A character denoting the grade.
40+
*/
41+
// Write your method here
42+
43+
int[] scores;
44+
45+
public Student(String firstName, String lastName, int id, int[] scores) {
46+
super(firstName,lastName,id);
47+
this.scores = scores;
48+
}
49+
50+
public char calculate() {
51+
int totalScore = 0;
52+
int average = 0;
53+
int id = String.valueOf(idNumber).length();
54+
for(int i = 0; i < scores.length; i++) {
55+
totalScore += scores[i];
56+
}
57+
58+
average = totalScore/scores.length;
59+
60+
if(firstName.length() >= 1 &&
61+
lastName.length() <=10 &&
62+
id == 7 &&
63+
totalScore >=0 &&
64+
average <= 100)
65+
{
66+
if (average >= 90 && average <= 100)
67+
return 'O';
68+
else if (average >= 80 && average <= 90)
69+
return 'E';
70+
else if (average >= 70 && average <= 80)
71+
return 'A';
72+
else if (average >= 55 && average <= 70)
73+
return 'P';
74+
else if (average >= 40 && average <= 55)
75+
return 'D';
76+
else
77+
return 'T';
78+
} else {
79+
return Character.MIN_VALUE;
80+
}
81+
82+
}
83+
}
84+
85+
class Solution {
86+
public static void main(String[] args) {
87+
Scanner scan = new Scanner(System.in);
88+
String firstName = scan.next();
89+
String lastName = scan.next();
90+
int id = scan.nextInt();
91+
int numScores = scan.nextInt();
92+
int[] testScores = new int[numScores];
93+
for(int i = 0; i < numScores; i++){
94+
testScores[i] = scan.nextInt();
95+
}
96+
scan.close();
97+
98+
Student s = new Student(firstName, lastName, id, testScores);
99+
s.printPerson();
100+
System.out.println("Grade: " + s.calculate() );
101+
}
102+
}

0 commit comments

Comments
 (0)