Student Grading System
Student Grading System
import java.util.Scanner;
// Number of subjects
int subjectCount = 8;
double totalPercentage = 0;
double grade;
while (true) {
System.out.print("Enter the grade for " + subjectName + " (1.0 - 5.0): ");
grade = scanner.nextDouble();
if (grade >= 1.0 && grade <= 5.0) {
break;
} else {
System.out.println("Please enter a grade between 1.0 and 5.0.");
}
}
scanner.nextLine(); // Consume newline
// Display summary
System.out.println("\n--- Grade Summary ---");
System.out.println("Name: " + name);
System.out.println("Course: " + course);
System.out.println("Year Level: " + yearLevel);
System.out.println("Average Percentage: " + averagePercentage + "%");
System.out.println("Overall Status: " + overallStatus);
EXPLANATION
java
Copy code
import java.util.Scanner;
This imports the Scanner class, which we use to read input from the user.
java
Copy code
public class GradingSystem {
Declares a public class named GradingSystem. In Java, every program must have at least one class, and this is the main class where all the
code will run.
java
Copy code
public static void main(String[] args) {
Defines the main method, which is the entry point for the program. The code inside this method will be executed when the program runs.
java
Copy code
Scanner scanner = new Scanner(System.in);
Creates a Scanner object named scanner to read input from the user through the console.
java
Copy code
System.out.println("Hello, student of NEMSU! Good day!");
Prints a greeting message to the console to welcome the student.
java
Copy code
System.out.print("Enter your name: ");
String name = scanner.nextLine();
Prompts the user to enter their name and stores it in the name variable by reading a full line of text.
java
Copy code
System.out.print("Enter your course: ");
String course = scanner.nextLine();
Prompts the user to enter their course and stores it in the course variable.
java
Copy code
System.out.print("Enter your year level: ");
String yearLevel = scanner.nextLine();
Prompts the user to enter their year level and stores it in the yearLevel variable.
java
Copy code
int subjectCount = 8;
double totalPercentage = 0;
Initializes subjectCount to 8, representing the number of subjects the program will process, and totalPercentage to 0, which will be used to
accumulate each subject's percentage.
java
Copy code
for (int i = 1; i <= subjectCount; i++) {
Starts a for loop that iterates 8 times, once for each subject. i keeps track of the current subject number.
java
Copy code
System.out.print("Enter the name of subject " + i + ": ");
String subjectName = scanner.nextLine();
Prompts the user to enter the name of the current subject and stores it in subjectName.
java
Copy code
double grade;
while (true) {
Declares a double variable grade and begins a while (true) loop, which will continue until a valid grade is entered.
java
Copy code
System.out.print("Enter the grade for " + subjectName + " (1.0 - 5.0): ");
grade = scanner.nextDouble();
Prompts the user to enter a grade for the current subject. It reads the grade as a double and stores it in grade.
java
Copy code
if (grade >= 1.0 && grade <= 5.0) {
break;
} else {
System.out.println("Please enter a grade between 1.0 and 5.0.");
}
Checks if the entered grade is within the range 1.0 to 5.0. If it is, the loop ends using break. Otherwise, an error message is printed, and the
loop asks for the grade again.
java
Copy code
scanner.nextLine(); // Consume newline
Consumes any remaining newline character in the input buffer to prevent issues with subsequent nextLine() calls.
java
Copy code
String letterGrade = "";
double percentage = 0;
Initializes letterGrade as an empty string and percentage as 0. These will store the letter grade and percentage for the current grade.
java
Copy code
if (grade >= 1.0 && grade <= 1.4) {
percentage = 95 + (4 - (grade - 1.0) * 10); // Mapping to 95-100 range
letterGrade = "A - Excellent";
} else if (grade >= 1.5 && grade <= 1.9) {
percentage = 85 + (4 - (grade - 1.5) * 10); // Mapping to 85-89 range
letterGrade = "B - Very Good";
} else if (grade >= 2.0 && grade <= 2.4) {
percentage = 80 + (4 - (grade - 2.0) * 10); // Mapping to 80-84 range
letterGrade = "C - Good";
} else if (grade >= 2.5 && grade <= 2.9) {
percentage = 75 + (4 - (grade - 2.5) * 10); // Mapping to 75-79 range
letterGrade = "D - Satisfactory";
} else {
percentage = 60 + (9 - (grade - 3.0) * 10); // Mapping to below 70 range
letterGrade = "F - Did Not Pass";
}
This series of if-else statements converts the numeric grade to both a percentage and a letterGrade based on the specified grading criteria:
o 1.0 - 1.4: 95-100% (A)
o 1.5 - 1.9: 85-89% (B)
o 2.0 - 2.4: 80-84% (C)
o 2.5 - 2.9: 75-79% (D)
o 3.0 - 5.0: Below 70% (F)
java
Copy code
totalPercentage += percentage;
Adds the calculated percentage for the current subject to totalPercentage, accumulating it across subjects.
java
Copy code
System.out.println(subjectName + ": " + letterGrade + " with a percentage of " + percentage + "%");
Prints the name of the subject along with its letterGrade and calculated percentage for confirmation.
java
Copy code
double averagePercentage = totalPercentage / subjectCount;
Calculates the averagePercentage by dividing the accumulated totalPercentage by the number of subjects.
java
Copy code
String overallStatus = (averagePercentage >= 75) ? "Passed" : "Failed";
Uses a ternary operator to set overallStatus to "Passed" if averagePercentage is 75% or above; otherwise, it sets it to "Failed".
java
Copy code
System.out.println("\n--- Grade Summary ---");
System.out.println("Name: " + name);
System.out.println("Course: " + course);
System.out.println("Year Level: " + yearLevel);
System.out.println("Average Percentage: " + averagePercentage + "%");
System.out.println("Overall Status: " + overallStatus);
Prints a summary with the student’s name, course, year level, average percentage, and overall status ("Passed" or "Failed").
java
Copy code
scanner.close();
Closes the scanner to release the system resources associated with it. This is good practice to prevent resource leaks.