Average and Grade Calculation Program in java

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  getters and setters methods for all the attributes.

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.

All marks should be greater than or equal to 50 to calculate the grade. If one or more marks is below 50 the grade should be set as ‘F’. If all marks are 50 and above, set the grade based on below condition, 

If the average is between 80 and 100 then, then return grade as ‘O’. If the average is between 50 and 79 then return the grade as  ‘A’ .If the average of the student is  less than 50 then return grade as ‘F’. average and grade attribute should be set with the appropriate values.

(Note : Assume the number of subject is  greater than zero and  the mark for a subject is in the range of 0 – 100.)

Write a class StudentMain and test the application.

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.333333
Grade:O

Sample Input 2:

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

3
Enter mark for subject 1:
25
Enter mark for subject 2:
30
Enter mark for subject 3:
45

Sample Output 2:

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

CODE:

import java.util.*;
public class StudentMain 
{
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        
        System.out.println("Enter the id:");
        int id=sc.nextInt();
        sc.nextLine();
        
        System.out.println("Enter the name:");
        String name=sc.nextLine();
        
        System.out.println("Enter the number of subjects:");
        int n=sc.nextInt();
        int marks[]=new int[n];
        
        for(int i=0;i<n;i++)
        {   System.out.println("Enter mark for subject "+(i+1)+":");
            marks[i] = sc.nextInt();
        }
        
        Student obj=new Student();
        obj.setName(name);
        obj.setId(id);
        obj.setMarks(marks);
        obj.calculateAvg();
        obj.findGrade();
        
        System.out.println("Id:"+obj.getId());
        System.out.println("Name:"+obj.getName());
        System.out.println("Average:"+obj.getAverage());
        System.out.println("Grade:"+obj.getGrade());
    }
}
public class Student 
{
    private int id;
    private String name;
    private int[] marks;
    private float average;
    private char grade;
    
    public void setId(int id)
    {
        this.id=id;
    }
    
    public int getId()
    {
        return this.id;
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    
    public int[] getMarks()
    {
        return this.marks;
    }
    
    public void calculateAvg()
    {   float sum=0;
        
        for(int i=0;i<this.marks.length;i++)
        {
            sum+=this.marks[i];
        }
        
        average=(float)(sum/(this.marks.length));
        setAverage(average);
    }
    
    public void findGrade()
    {int flag=0;
        for(int i=0;i<this.marks.length;i++)
        {
            if(this.marks[i]<50)
            {
                flag++;
            }
        }
        if(flag>0)
        {
            grade='F';
        }
        else
        {
            if(this.average>=80 && this.average<=100)
            {
                grade='O';
            }
            else if(this.average>=50 && this.average<=79)
            {
                grade='A';
            }
            else
            {
                grade='F';
            }
        }
        setGrade(grade);
        
    }
    
    public void setAverage(float average)
    {
        this.average=average;
    }
    
    public void setGrade(char grade)
    {
        this.grade=grade;
    }
    
    public float getAverage()
    {
        return this.average;
    }
    
    public char getGrade()
    {
        return this.grade;
    }
}
Next:
  1. Average and Grade Calculation
  2. String – Find and replace the character (first occurrence)
  3. Sort the first and second half of an array
  4. Retail Shop
  5. Palindrome
  6. Numerology
  7. InitCap
  8. Array Compatiblilty

Leave a Comment

Scroll to Top