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:
- A Student class constructor, which has parameters:
- A string, .
- A string, .
- An integer, .
- An integer array (or vector) of test scores, .
- A char calculate() method that calculates a Student object's average and returns the grade character representative of their calculated average:
Input Format
The locked stub code in your editor calls your Student class constructor and passes it the necessary arguments. It also calls the calculate method (which takes no arguments).
You are not responsible for reading the following input from stdin:
The first line contains , , and , respectively. The second line contains the number of test scores. The third line of space-separated integers describes .
Constraints
Output Format
This is handled by the locked stub code in your editor. 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 our calculate() method should return the character'O'
.
How I solved the problem
# 생성자와 inheritance(상속)에 대해서 잘 이해할 것
# 생성자 : a constructor is a block of codes similar to the method. (매서드와 비슷하게 코드의 블럭임)
# 상속 : hierarchical(계층) 구조에 따라서 부모의 특성을 물려받아 더 발전시킬 수 있는 구조
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | 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; /* * Class Constructor * * @param firstName - A string denoting the Person's first name. * @param lastName - A string denoting the Person's last name. * @param id - An integer denoting the Person's ID number. * @param scores - An array of integers denoting the Person's test scores. */ // Write your constructor here Student (String firstName, String lastName, int id, int[] scores) { super(firstName, lastName, id); //Person의 생성자에 넣어줌 this.testScores = scores; //추가된 파라미터에 넣어줌 } /* * Method Name: calculate * @return A character denoting the grade. */ // Write your method here public String calculate() { int sum = 0; for (int i = 0; i < testScores.length; i++) { sum += testScores[i]; } double average = Math.round(sum / testScores.length); if (average >= 90 && average <= 100) { return "O"; } else if (average >= 80 && average < 90) { return "E"; } else if (average >= 70 && average < 80) { return "A"; } else if (average >= 55 && average < 70) { return "P"; } else if (average >= 40 && average < 55) { return "D"; } else { 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() ); } } | cs |
[출처 : https://www.hackerrank.com ]
'1 Day 1 Algorithms' 카테고리의 다른 글
[2018.12.28] Scope (0) | 2018.12.28 |
---|---|
[2018.12.27] Abstract Classes (0) | 2018.12.27 |
[2018.12.21] Binary Numbers (0) | 2018.12.21 |
[2018.12.20] Recursion 3 (0) | 2018.12.21 |
[2018.12.19] Mini-Max Sum (0) | 2018.12.19 |
댓글