We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
class Person {
String firstName;
String lastName;
int id;
public Person(String firstName, String lastName, int id) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
}
}
class Student extends Person {
private int[] testScores;
public Student(String firstName, String lastName, int id, int[] testScores) {
super(firstName, lastName, id);
this.testScores = testScores;
}
public char calculate() {
int sum = 0;
for (int score : testScores) {
sum += score;
}
int avg = sum / testScores.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';
}
}
public class Solution {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
String firstName = scn.next();
String lastName = scn.next();
int id = scn.nextInt();
int numScores = scn.nextInt();
int[] scores = new int[numScores];
for (int i = 0; i < numScores; i++) {
scores[i] = scn.nextInt();
}
Student std = new Student(firstName, lastName, id, scores);
System.out.println("Name: "+lastName+", "+firstName);
System.out.println("ID: "+id);
System.out.println("Grade: "+std.calculate());
scn.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 12: Inheritance
You are viewing a single comment's thread. Return to all comments →
import java.util.Scanner;
class Person { String firstName; String lastName; int id;
}
class Student extends Person { private int[] testScores;
}
public class Solution { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String firstName = scn.next(); String lastName = scn.next(); int id = scn.nextInt(); int numScores = scn.nextInt(); int[] scores = new int[numScores];
}