w3resource

Java Student Grade System Project


Java Project - Solutions to Calculate and Store Student Grades:

Student Grade System :

Calculate and store student grades for various subjects.

This project stores student grades for various subjects, calculates their average, and determines their final grade. The user can input subject marks, and the system calculates the overall result (pass/fail).

Input: Student name and grades for multiple subjects.
Output: Student’s average grade and status (pass/fail).

Example:

  • Input: Name: Abaddon, Grades: 85, 90, 78
  • Output: "Average grade: 84.33, Status: Pass"

Solution 1: Using Arrays to Store Grades

Code:

import java.util.Scanner;

public class StudentGradeSystemArray {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Get student name
        System.out.println("Enter student name:");
        String studentName = scanner.nextLine();

        // Get number of subjects
        System.out.println("Enter number of subjects:");
        int numSubjects = scanner.nextInt();

        // Array to store grades
        double[] grades = new double[numSubjects];
        double total = 0;

        // Loop to input grades and calculate total
        for (int i = 0; i < numSubjects; i++) {
            System.out.println("Enter grade for subject " + (i + 1) + ":");
            grades[i] = scanner.nextDouble();
            total += grades[i]; // Adding grades to total
        }

        // Calculate average
        double average = total / numSubjects;

        // Determine pass or fail status
        String status = (average >= 50) ? "Pass" : "Fail";

        // Display result
        System.out.println("Student: " + studentName);
        System.out.println("Average grade: " + average);
        System.out.println("Status: " + status);

        scanner.close();
    }
}

Output:

Enter student name:
 Arndt Adolfas
Enter number of subjects:
 5
Enter grade for subject 1:
 70
Enter grade for subject 2:
 80
Enter grade for subject 3:
 90
Enter grade for subject 4:
 91
Enter grade for subject 5:
 89
Student: Arndt Adolfas
Average grade: 84.0
Status: Pass

Explanation :

  • The program first prompts the user for the student's name.
  • It asks for the number of subjects the student has grades for.
  • It stores the grades in an array and calculates the total sum of the grades.
  • The average grade is calculated by dividing the total sum by the number of subjects.
  • The program then determines whether the student has passed or failed based on an average grade of 50.
  • It prints the student's name, average grade, and pass/fail status.

Solution 2: Using Object-Oriented Approach

Code:

import java.util.Scanner;

class Student {
    String name;
    double[] grades;
    int numSubjects;

    // Constructor to initialize the student object
    public Student(String name, int numSubjects) {
        this.name = name;
        this.numSubjects = numSubjects;
        grades = new double[numSubjects]; // Array to store grades
    }

    // Method to input grades
    public void inputGrades(Scanner scanner) {
        for (int i = 0; i < numSubjects; i++) {
            System.out.println("Enter grade for subject " + (i + 1) + ":");
            grades[i] = scanner.nextDouble();
        }
    }

    // Method to calculate average
    public double calculateAverage() {
        double total = 0;
        for (double grade : grades) {
            total += grade;
        }
        return total / numSubjects;
    }

    // Method to determine pass or fail
    public String determineStatus(double average) {
        return average >= 50 ? "Pass" : "Fail";
    }

    // Method to display the result
    public void displayResult() {
        double average = calculateAverage();
        String status = determineStatus(average);
        System.out.println("Student: " + name);
        System.out.println("Average grade: " + average);
        System.out.println("Status: " + status);
    }
}

public class StudentGradeSystemOOP {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input student details
        System.out.println("Enter student name:");
        String studentName = scanner.nextLine();
        System.out.println("Enter number of subjects:");
        int numSubjects = scanner.nextInt();

        // Create a Student object
        Student student = new Student(studentName, numSubjects);

        // Input grades and display result
        student.inputGrades(scanner);
        student.displayResult();

        scanner.close();
    }
}  

Output:

 Enter student name:
 Marika Ichabod
Enter number of subjects:
 5
Enter grade for subject 1:
 90
Enter grade for subject 2:
 91
Enter grade for subject 3:
 88
Enter grade for subject 4:
 89
Enter grade for subject 5:
 85
Student: Marika Ichabod
Average grade: 88.6
Status: Pass

Explanation:

  • A Student class is created with attributes for the student's name, number of subjects, and an array of grades.
  • The inputGrades() method allows input of grades.
  • The calculateAverage() method computes the student's average grade.
  • The determineStatus() method checks if the student passed or failed based on the average grade.
  • The displayResult() method shows the student's name, average grade, and pass/fail status.
  • In the main class, a Student object is created, and the program calls its methods to perform tasks.

Java Code Editor:




Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/java/java-project-student-grade-system.php