-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrade_softwarejavacode.java
More file actions
112 lines (91 loc) · 3.9 KB
/
Grade_softwarejavacode.java
File metadata and controls
112 lines (91 loc) · 3.9 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Number of assignments and exams
int numHomework = 8;
int numQuizzes = 5;
// Weightage for each category
double homeworkWeight = 0.15;
double quizWeight = 0.05;
double midtermWeight = 0.25;
double finalExamWeight = 0.3;
double projectWeight = 0.25;
// Input scores
System.out.println("Assume Evey Test is out of 100\n");
double homeworkScore = getValidScore("Homework", numHomework);
double quizScore = getValidScore("Quiz", numQuizzes);
double midtermScore = getValidScore("Midterm Exam");
double finalExamScore = getValidScore("Final Exam");
double projectScore = getValidScore("Final Project");
// Calculate total grade
double totalGrade = calculateTotalGrade(homeworkScore, quizScore, midtermScore, finalExamScore, projectScore,
homeworkWeight, quizWeight, midtermWeight, finalExamWeight, projectWeight);
// Determine letter grade
char letterGrade = calculateLetterGrade(totalGrade);
// Display results
System.out.println("Total Grade: " + totalGrade);
System.out.println("Letter Grade: " + letterGrade);
scanner.close();
}
// Function to get valid scores from the user (between 0 and 100)
private static double getValidScore(String category) {
int count = 0;
Scanner scanner = new Scanner(System.in);
double score;
do {
try {
if (count != 0) {
System.out.print(".........Enter Again (out of 100) : ");
} else {
System.out.print("Enter " + category + " score: ");
}
score = scanner.nextDouble();
count++;
if (!isValidScore(score)) {
throw new InputMismatchException();
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a numeric value.");
scanner.nextLine(); // consume the invalid input
score = -1; // set to an invalid value to force re-entry
}
} while (score < 0 || score > 100);
return score;
}
// Overloaded function for quizzes and homework
private static double getValidScore(String category, int count) {
Scanner scanner = new Scanner(System.in);
double total = 0;
for (int i = 1; i <= count; i++) {
total += getValidScore(category + " " + i);
}
return total / count;
}
// Function to validate the score (between 0 and 100)
private static boolean isValidScore(double score) {
return score >= 0 && score <= 100;
}
// Function to calculate the total grade
private static double calculateTotalGrade(double homework, double quizzes, double midterm, double finalExam,
double project, double hwWeight, double quizWeight, double midtermWeight, double finalExamWeight,
double projectWeight) {
return (homework * hwWeight) + (quizzes * quizWeight) + (midterm * midtermWeight) + (finalExam * finalExamWeight)
+ (project * projectWeight);
}
// Function to calculate the letter grade
private static char calculateLetterGrade(double totalGrade) {
if (totalGrade >= 90) {
return 'A';
} else if (totalGrade >= 80) {
return 'B';
} else if (totalGrade >= 70) {
return 'C';
} else if (totalGrade >= 60) {
return 'D';
} else {
return 'F';
}
}
}