Java program of Average and Grade Calculation

Question:-

Develop a smart application as Student Grade Calculator(SGC).

Create a class Student with following private attribute :

int id, String name, marks(integer array), float average and char grade. Include appropriate getters and setters methods and a 3 argument constructor with arguments in the order id, name and marks(int array).

public  void calculateAvg()– This method should calculate average and set average mark for the current student.

public void findGrade()– This method should set the grade based on the average calculated. If the average is between 80 and 100 then, then return grade as ‘O’, else ‘A’ .If the student gets less than 50 in any of the subjects then return grade as ‘F’. Using appropriate setter method set the grade to the student.

(Note : number of subject should be greater than zero, if not display as ‘Invalid number of subject’ and get number of subject again, Assume mark for a subject should be in the range 0 – 100.  If not display a message “Invalid Mark” and get the mark again)

Write a class StudentMain and write the main method.

In this class, write a method

public static Student getStudentDetails() – this method should get the input from the user for a student, create a student object with those details and return that object.

In main create student’s object by invoking the getStudentDetails method.  Also calculate average and grade for that student object using appropriate methods.

SGC app should get the input and display the output as specified in the snapshot:

Sample Input 1:
Enter the id:
123
Enter the name:
Tom
Enter the no of subjects:
3
Enter mark for subject 1:
95
Enter mark for subject 2:
80
Enter mark for subject 3:
75

Sample Output 1:

Id:123
Name:Tom
Average:83.33
Grade:O

Sample Input 2:

Enter the id:
123
Enter the name:
Tom
Enter the no of subjects:
0

Invalid number of subject

Enter the no of subjects:

3
Enter mark for subject 1:
75
Enter mark for subject 2:
49
Enter mark for subject 3:
90

Sample Output 2:

Id:123
Name:Tom
Average:71.33
Grade:F

Code:-

StudentMain.java

import java.util.Scanner;
public class StudentMain
{
    public static void main(String[] args)
    {
        Student s=getStudentDetails();
        s.calculateAvg();
        s.findGrade();
        System.out.println("Id:" +s.getId());
        System.out.println("Name:" +s.getName());
        System.out.println("Average:"+String.format("%.2f",s.getAverage()));
        System.out.println("Grade:" +s.getGrade());
    }
    public static Student getStudentDetails()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the id:");
        int id=Integer.parseInt(sc.nextLine());
        String name=sc.nextLine();
        System.out.println("Enter the no of subjects:");
        int n=sc.nextInt();
        if(n<=0)
        {
            while(n<=0)
            {
                System.out.println("Invalid number of subject");
                System.out.println("Enter the no of subjects");
                n=sc.nextInt();
            }
        }
        int arr[]=new int[n];
        for(int a=0;a<n;a++)
        {
            System.out.println("Enter mark for subject "+(a+1)+":");
            int b=sc.nextInt();
            if(b<0||b>100)
            {
                System.out.println("Invalid Mark");
                System.out.println("Enter mark for subject "+(a+1)+":");
                b=sc.nextInt();
            }
            arr[a]=b;
        }
        Student obj=new Student(id,name,arr);
        obj.setId(id);
        obj.setName(name);
        return obj;
    }
}

Student.java

public class Student
{
    private int id;
    private String name;
    private int marks[];
    private float average;
    private char grade;
    public Student(int a,String b,int[] c)
    {
        id=a;
        name=b;
        marks=c;
    }
    public void setId(int n)
    {
        id=n;
    }
    public int getId()
    {
        return id;
    }
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    public int[] getMarks()
    {
        return marks;
    }
    public void setName(String n)
    {
        name=n;
        
    }
    public String getName()
    {
        return name;
    }
    public void setAverage(float n)
    {
        average=n;
    }
    public float getAverage()
    {
        return average;
    }
    public void setGrade(char n)
    {
        grade=n;
    }
    public char getGrade()
    {
        return grade;
    }
    public void calculateAvg()
    {
        float a1=0.0F;
        for(int a=0;a<this.getMarks().length;a++)
        {
            a1=a1+this.marks[a];
        }
        this.setAverage(a1/getMarks().length);
    }
    public void findGrade()
    {
        int min=this.marks[0];
        for(int b=0;b<this.getMarks().length;b++)
        {
            if(this.marks[b]<min) 
                {
                    min=this.marks[b];
                }
        }
        if(min<50)
        {
            this.setGrade('F');
        }
        else if(this.getAverage()>=80 && this.getAverage()<=100)
        {
            this.setGrade('O');
        }
        else
        {
            this.setGrade('A');
        }
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *