-
Notifications
You must be signed in to change notification settings - Fork 183
/
13 - Day 12 - Inheritance.cs
90 lines (75 loc) · 2.38 KB
/
13 - Day 12 - Inheritance.cs
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
90
// ========================
// Information
// ========================
// Direct Link: https://www.hackerrank.com/challenges/30-inheritance/problem
// Difficulty: Easy
// Max Score: 30
// Language: C#
// ========================
// Solution
// ========================
using System;
using System.Linq;
class Person {
protected string firstName;
protected string lastName;
protected int id;
public Person() {}
public Person(string firstName, string lastName, int identification) {
this.firstName = firstName;
this.lastName = lastName;
this.id = identification;
}
public void printPerson() {
Console.WriteLine("Name: " + lastName + ", " + firstName + "\nID: " + id);
}
}
class Student: Person {
private int[] testScores;
// Class Constructor
/* Parameters:
* firstName - A string denoting the Person's first name.
* lastName - A string denoting the Person's last name.
* id - An integer denoting the Person's ID number.
* scores - An array of integers denoting the Person's test scores. */
// Write your constructor here
public Student(string firstName, string lastName, int id, int[] scores) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
this.testScores = scores;
}
// Method Name: Calculate
// Return: A character denoting the grade.
// Write your method here
public string Calculate() {
var average = testScores.Average();
var res = IsWithin(average, 90, 100) ? "O" :
IsWithin(average, 80, 89) ? "E" :
IsWithin(average, 70, 79) ? "A" :
IsWithin(average, 50, 69) ? "P" :
IsWithin(average, 40, 54) ? "D" :
"T";
return res;
}
private bool IsWithin(double value, int minimum, int maximum) {
return value >= minimum && value <= maximum;
}
}
class Solution {
static void Main() {
string[] inputs = Console.ReadLine().Split();
string firstName = inputs[0];
string lastName = inputs[1];
int id = Convert.ToInt32(inputs[2]);
int numScores = Convert.ToInt32(Console.ReadLine());
inputs = Console.ReadLine().Split();
int[] scores = new int[numScores];
for (int i = 0; i < numScores; i++) {
scores[i] = Convert.ToInt32(inputs[i]);
}
Student s = new Student(firstName, lastName, id, scores);
s.printPerson();
Console.WriteLine("Grade: " + s.Calculate() + "\n");
}
}