Java program that compute the average test score for a student and assign the appropriate letter grade
Posted by Samath
Last Updated: January 17, 2024

Write a program that reads a student’s name together with his or her test scores. The program should then compute the average test score for each student and assign the appropriate grade. The grade scale is as follows: 90-100,A; 80-89,B; 70-79,C; 60-69,D; 0-59,F.

Your program must use the following methods:

a.  A value-returning method, calculateAverage, to determine and return the average of five test scores for each student. Use a loop to read and sum the five test scores. (This method does not output the average test score. That task must be done in the method main.)

b. A value-returning method, calculateGrade, to determine and return each student’s grade. (This method does not output the grade. That task must be done in the
method main.)

Solution:

package testScores;
import java.util.Scanner;

public class testScores {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String name;
        float avg;
        String l_grade;
        
        System.out.print("Enter student's name: ");
        name = scan.nextLine();
        
        avg = calculateAverage();
        l_grade = calculateGrade(avg);
        
        
        System.out.print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        System.out.print("\nName: "+name);
        System.out.print("\nLetter Grade: "+l_grade);
        System.out.print("\nAverage: "+avg);
        System.out.print("\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        
 }

 public static float calculateAverage()
 {
     float grd;
     float sum = 0;
     float average = 0;
     Scanner scan = new Scanner(System.in);
     for(int i=0;i<5;i++)
     {
         System.out.print("Enter a grade: ");
         grd = scan.nextFloat();
         sum = sum + grd;
     }
     
     average = sum / 5;
    
     return average;
  }

 public static String calculateGrade(double average)    
 {
    String gradeLetter = "";
    if (average >= 90 && average <= 100)
    {
        gradeLetter = "A";
    }
    else if (average >= 80 && average <= 89)
    {
        gradeLetter = "B";
    }
    else if (average >= 70 && average <= 79)
    {
        gradeLetter = "C";
    }
    else if (average >= 60 && average <= 69)
    {
        gradeLetter = "D";
    }
    else if (average >= 0 && average <= 59)
    {
        gradeLetter = "F";
    }
 return gradeLetter;
 }
}