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  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:

StudentMain.java

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());
    }
}

Student.java

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;
    }
}

Recommended:

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

1 thought on “Java Program of Average and Grade Calculation”

  1. 888066 378779Although you are any with the lucky enough choices, it comes evidently, although capture the fancy with the specific coveted by ly folks other valuable you you meet may possibly possibly properly have hard times this specific difficulty. pre owned awnings 361619

Leave a Comment

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

x