-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12-Inheritance
84 lines (71 loc) · 2.46 KB
/
Day12-Inheritance
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
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 4 parameters:
A string, firstName .
A string, lastName .
An integer, id.
An integer array (or vector) of test scores, scores.
A char calculate() method that calculates a Student object's average and returns the grade character
representative of their calculated average:
Grading scale:
Letter Average(a)
O 90 <= a <= 100
E 80 <= a < 90
A 70 <= a < 80
P 55 <= a < 70
D 40 <= a < 55
T a < 40
Solution:
class Student extends Person{
private int[] testScores;
public int[] getTestScores() {
return testScores;
}
public void setTestScores(int[] testScores) {
this.testScores = testScores;
}
public Student(String firstName, String lastName, int id, int[] testScores) {
super(firstName, lastName, id);
setTestScores(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
/*
* Method Name: calculate
* @return A character denoting the grade.
*/
// Write your method here
public String calculate() {
int sum = 0;
int[] testScorestoCalc = Student.this.testScores;
for (int i = 0; i < testScorestoCalc.length; i++) {
sum += testScores[i];
}
int average = sum / testScorestoCalc.length;
if (average < 40) {
return "T";
} else if (average >= 40 && average < 55) {
return "D";
} else if (average >= 55 && average < 70) {
return "P";
} else if (average >= 70 && average < 80) {
return "A";
} else if (average >= 80 && average < 90) {
return "E";
} else {
return "O";
}
}
}