Class GradeCalculator
GradeCalculator
class calculates the grades of students based on their marks.
It takes input for the number of subjects, accepts marks for each subject, calculates the total and average marks, and assigns a grade based on predefined ranges.
The program also handles invalid inputs and allows the user to calculate grades repeatedly until they choose to exit.
- Version:
- 1.0.0
- Author:
- Anurag Zete
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enum
Enum representing the grade system based on marks ranges. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static double
Stores the calculated average marks.private static final Scanner
Scanner object for reading user input.private static double
Stores the cumulative total marks of all subjects.private static int
Stores the total number of subjects entered by the user. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static double
calculateGrade
(Scanner sc, int totalSubjects) Accepts marks for each subject and calculates the total marks.private static void
Displays the final results, including: Total marks obtained out of the maximum possible marks. Average marks calculated. The grade obtained based on the average.static void
The main method to run the grade calculator program.private static void
program()
Runs the grade calculation program loop.
-
Field Details
-
sc
Scanner object for reading user input. -
totalMarks
private static double totalMarksStores the cumulative total marks of all subjects. -
totalSubjects
private static int totalSubjectsStores the total number of subjects entered by the user. -
averageMarks
private static double averageMarksStores the calculated average marks.
-
-
Constructor Details
-
GradeCalculator
public GradeCalculator()
-
-
Method Details
-
main
The main method to run the grade calculator program.It displays a banner, starts the grade calculation loop, and exits the program when the user chooses to stop.
- Parameters:
args
- Command-line arguments (not used).
-
program
private static void program()Runs the grade calculation program loop.- Prompts the user for the number of subjects.
- Calls thecalculateGrade()
method to accept marks.
- Displays the result usingdisplayResult()
.
- Allows the user to repeat the grade calculation or exit. -
calculateGrade
Accepts marks for each subject and calculates the total marks.- Marks are stored in an array for validation purposes.
- Accepts "ab" or empty input as an indicator of absence (assigns 0.0).
- Ensures valid marks range (0 - 100) and handles incorrect input formats.- Parameters:
sc
- TheScanner
object for user input.totalSubjects
- The total number of subjects.- Returns:
- The cumulative total marks.
-
displayResult
private static void displayResult()Displays the final results, including:- Total marks obtained out of the maximum possible marks.
- Average marks calculated.
- The grade obtained based on the average.
-