-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGredit.java
107 lines (93 loc) · 3.58 KB
/
Gredit.java
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.Scanner;
public class Gredit {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("\nPlease type the student name\t: ");
String studentName = input.nextLine();
// 2D array to store grades for 4 quarters and 5 subjects
double[][] grades = new double[4][5];
// 1D array to store averages for later processing
double[] quarterAverages = new double[4];
// Loop through each quarter to get grades and calculate averages
for (int i = 0; i < 4; i++) {
quarterAverages[i] = getQuarterAverage(input, studentName, i, grades);
}
// Calculate the final average from the quarter averages
double finalAverage = calculateGrade(quarterAverages);
System.out.println("\n" + studentName.toUpperCase() + "\'s ACADEMIC YEAR AVERAGE\t: " + finalAverage + "\n");
String remarks = (finalAverage <= 74) ? "[You have failed, see you..]" : "[You have passed, congrats!]";
// Print all grades from the entire academic year
printGrades(grades, studentName);
System.out.println("AVE :\t" + finalAverage + "\t" + remarks);
input.close();
}
public static void printGrades(double[][] grades, String studentName) {
System.out.println("\n" + studentName.toUpperCase() + "\'S GRADES (LYFJSHS ACADEMIC YEAR 2024-2025)");
// Loop through each quarter and print the grades for each subject
for (int i = 0; i < grades.length; i++) {
System.out.print("QT" + (i + 1) + " :\t");
for (int j = 0; j < grades[i].length; j++) {
System.out.print(grades[i][j] + "\t");
}
System.out.println();
}
}
public static double getQuarterAverage(Scanner input, String studentName, int quarterIndex, double[][] grades) {
// Determine the quarter name based on the index
String quarter = switch (quarterIndex) {
case 0 -> "FIRST";
case 1 -> "SECOND";
case 2 -> "THIRD";
case 3 -> "FOURTH";
default -> "";
};
// Get grades for the current quarter and store them in the 2d array
grades[quarterIndex] = getGrades(input, new String[]{
"Grade in Applied Science\t: ",
"Grade in Oral Communication\t: ",
"Grade in Business Mathematics\t: ",
"Grade in Information Literacy\t: ",
"Grade in Computing Literacy\t: "
});
// Calculate the average for the current quarter
double average = calculateGrade(grades[quarterIndex]);
System.out.println("\n" + studentName.toUpperCase() + "\'S " + quarter + " QUARTER AVERAGE\t: " + average + "\n");
return average;
}
public static double[] getGrades(Scanner input, String[] prompts) {
// This method handles storing the grades from the user input
double[] grades = new double[prompts.length];
for (int i = 0; i < prompts.length; i++) {
// Calls getValidInput method
grades[i] = getValidInput(input, prompts[i]);
}
return grades;
}
public static double getValidInput(Scanner input, String prompt) {
// This method ensures valid input from the user
double value = 0;
while(true) {
System.out.print(prompt);
if (input.hasNextDouble()) {
value = input.nextDouble();
if (value >= 70 && value <= 100) {
break;
} else {
System.out.println("Please enter a mark within 70 - 100.");
}
} else {
System.out.println("Invalid input. Please enter a number.");
input.next();
}
}
return value;
}
public static double calculateGrade(double[] numbers) {
// This method calculates the average of an array of numbers
double sum = 0;
for (double number: numbers) {
sum += number;
}
return sum / numbers.length;
}
}