Introduction

    In this blog post, we'll explore a Java program that allows you to input student details, read their marks for five subjects, calculate the total marks, and then assign a grade based on the average. This program is a great example of how object-oriented programming (OOP) principles can be applied to a real-world scenario.


Program Breakdown

The program consists of three main classes:

  • student: Handles basic student information like roll number and name.
  • Result: Extends the student class and adds functionality to read marks, calculate the total, and determine the grade.
  • mainstud: The main class that drives the program, creating instances of the Result class and invoking methods to display student details and results.

        
        import java.io.*;

        class student {
            int rollno;
            String name;
        
            void readStudent() throws IOException {
                DataInputStream s = new DataInputStream(System.in);
                System.out.println("Enter the roll number:");
                rollno = Integer.parseInt(s.readLine());
                System.out.println("Enter name:");
                name = s.readLine();
            }
        
            void displayStudent() {
                System.out.println("\nName: " + name);
                System.out.println("Roll number: " + rollno);
            }
        }
        
        class Result extends student {
            int m1, m2, m3, m4, m5;
        
            void readResult() throws IOException {
                DataInputStream st = new DataInputStream(System.in);
                System.out.println("Enter mark of subject 1:");
                m1 = Integer.parseInt(st.readLine());
                System.out.println("Enter mark of subject 2:");
                m2 = Integer.parseInt(st.readLine());
                System.out.println("Enter mark of subject 3:");
                m3 = Integer.parseInt(st.readLine());
                System.out.println("Enter mark of subject 4:");
                m4 = Integer.parseInt(st.readLine());
                System.out.println("Enter mark of subject 5:");
                m5 = Integer.parseInt(st.readLine());
            }
        
            void displayResult() {
                int total = m1 + m2 + m3 + m4 + m5;
                System.out.println("Total marks: " + total);
                float avg = total / 5.0f;
        
                if (avg > 90)
                    System.out.println("Grade is A");
                else if (avg > 80)
                    System.out.println("Grade is B");
                else if (avg > 70)
                    System.out.println("Grade is C");
                else if (avg > 60)
                    System.out.println("Grade is D");
                else if (avg > 50)
                    System.out.println("Grade is E");
                else
                    System.out.println("Failed!!!");
            }
        }
        
        class mainstud {
            public static void main(String args[]) throws IOException {
                Result rs = new Result();
                rs.readStudent();
                rs.readResult();
                rs.displayStudent();
                rs.displayResult();
            }
        }
                                    
        
        


Output


        Enter the roll number:
        10
        Enter name:
        Shafah
        Enter mark of subject 1:
        90
        Enter mark of subject 2:
        85
        Enter mark of subject 3:
        80
        Enter mark of subject 4:
        79
        Enter mark of subject 5:
        95

        Name: Shafah
        Roll number: 10
        Total marks: 429
        Grade is B

               
        



Explanation

1. Student Details Input:
    The program begins by prompting the user to enter the student's roll number and name using the readStudent() method in the student class.
2. Marks Input:
    The readResult() method in the Result class is then used to input the marks for five subjects. This method reads the marks as integers and stores them in the instance variables m1, m2, m3, m4, and m5.
3. Displaying Student Details:
    The displayStudent() method prints the student's name and roll number.
4. Calculating Total Marks and Grade:
    The displayResult() method computes the total marks by summing up the marks for all five subjects.
It then calculates the average and determines the grade based on predefined ranges:
A: Average > 90
B: Average > 80
C: Average > 70
D: Average > 60
E: Average > 50
Failed: Average ≤ 50
5. Output:
    The program outputs the student's total marks and the corresponding grade.  

Conclusion

    This Java program showcases how to manage student data, perform calculations, and make decisions based on conditions, all using the principles of object-oriented programming. It’s a great way to get comfortable with inheritance, method overriding, and user input handling in Java.

This kind of program is not only useful for academic purposes but also forms the basis for more complex student management systems and educational software.

Happy coding!