-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGradeBook4.java
More file actions
80 lines (68 loc) · 2.19 KB
/
GradeBook4.java
File metadata and controls
80 lines (68 loc) · 2.19 KB
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
package src;
public class GradeBook4 {
private String courseName;
private final int[] grades;
private int total;
// parametrized src.constructor.
public GradeBook4(String courseName, int[] grades) {
this.courseName = courseName;
this.grades = grades;
}
// course name setter method.
public void setCourseName(String courseName) {
this.courseName = courseName;
}
// course name getter method.
public String getCourseName() {
return courseName;
}
// display banner method.
public void diplayMessage() {
System.out.println("src.welcome to: " + getCourseName());
}
// average finder method.
public double getAvg() {
// enhanced for loop (also known as for-each loop)
for (int grade : grades) {
total = total + grade;
}
// returning average by explicit type casting.
return (double) total / grades.length;
}
// minimum grade finder method.
public int getMin() {
int lowerGrade = grades[0];
// enhanced for-loop
for (int grade : grades) {
if (grade < lowerGrade)
lowerGrade = grade;
}
// returning lower grade.
return lowerGrade;
}
// showing grades.
public void processGrades() {
OutputGrades();
System.out.println("Class Average is : " + getAvg());
System.out.println("Lowest Grade is : " + getMin());
}
// private method for showing grades.
private void OutputGrades() {
System.out.println("The grades are: ");
for (int student = 0; student < grades.length; student++) {
// showing in format "Student1 87"
System.out.printf("Student%d%5d\n", (student + 1), grades[student]);
}
}
// main driven function
public static void main(String[] args) {
// grades src.array
int[] gradeArray = {87, 68, 94, 100, 83, 78, 85, 91, 76, 87};
// creating object.
var gb = new GradeBook4("CS-586 MPL", gradeArray);
// calling displayMessage method.
gb.diplayMessage();
// calling processGrade method.
gb.processGrades();
}
}