Java Day 12: Inheritance
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.
- 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 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';
- }
- }
-
- 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() );
- }
- }