-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent grade management.java
More file actions
92 lines (76 loc) · 2.38 KB
/
Copy pathstudent grade management.java
File metadata and controls
92 lines (76 loc) · 2.38 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
81
82
83
84
85
86
87
88
89
90
91
92
package Salary;
class Student {
private String name;
private int id;
private int[] grades;
private int gradeCount;
public Student(String name, int id) {
this.name = name;
this.id = id;
this.grades = new int[5];
this.gradeCount = 0;
}
public void addGrade(int grade) {
if (gradeCount < grades.length) {
grades[gradeCount] = grade;
gradeCount++;
} else {
System.out.println("Cannot add more grades, array is full.");
}
}
public double calculateAverage() {
if (gradeCount == 0) return 0;
int sum = 0;
for (int i = 0; i < gradeCount; i++) {
sum += grades[i];
}
return (double) sum / gradeCount;
}
public int findHighestGrade() {
if (gradeCount == 0) return 0;
int highest = grades[0];
for (int i = 1; i < gradeCount; i++) {
if (grades[i] > highest) {
highest = grades[i];
}
}
return highest;
}
public int findLowestGrade() {
if (gradeCount == 0) return 0;
int lowest = grades[0];
for (int i = 1; i < gradeCount; i++) {
if (grades[i] < lowest) {
lowest = grades[i];
}
}
return lowest;
}
public void displayGradeReport() {
System.out.println("Grade Report for " + name + " (ID: " + id + ")");
System.out.print("Grades: ");
for (int i = 0; i < gradeCount; i++) {
System.out.print(grades[i] + " ");
}
System.out.println();
System.out.println("Average Grade: " + calculateAverage());
System.out.println("Highest Grade: " + findHighestGrade());
System.out.println("Lowest Grade: " + findLowestGrade());
System.out.println("--------------------------------");
}
}
public class GradeManager {
public static void main(String[] args) {
Student student1 = new Student("sumaan", 101);
student1.addGrade(85);
student1.addGrade(90);
student1.addGrade(78);
Student student2 = new Student("hanaan", 102);
student2.addGrade(88);
student2.addGrade(92);
student2.addGrade(80);
student2.addGrade(95);
student1.displayGradeReport();
student2.displayGradeReport();
}
}