In: Computer Science
Write a program that asks the user to enter five test scores.
The program should display a letter grade for each score and the
average test score. Write the following methods in the
program:
calcAverage:
This method should accept five test scores as arguments and return
the average of the scores.
determineGrade:
This method should accept a test score as an argument and return a
letter grade for the score, based on the following grading
scale:
Score Letter Grade
90-100 A
80-89 B
70-79 C
60-69 D
Below 60 F
Sample Run
java TestAvgAndGrade
·Enter·test·grade·for·student·1:55↵
·Enter·test·grade·for·student·2:65↵
·Enter·test·grade·for·student·3:75↵
·Enter·test·grade·for·student·4:85↵
·Enter·test·grade·for·student·5:95↵
·The·letter·grades·are·as·follows:↵
Student·1:·F↵
Student·2:·D↵
Student·3:·C↵
Student·4:·B↵
Student·5:·A↵
The·average·grade·was:·75.00↵
import java.util.*;
public class Main{
public void calcAverage(int b[]){
int j,temp=0;
for(j=0;j<b.length;j++){
temp+=b[j];
}
temp=temp/5;
System.out.print("The·average·grade·was:·");
System.out.print(temp);
}
public void determineGrade(int c[]){
int k;
for(k=0;k<c.length;k++){
if(c[k]>=90&&c[k]<=100){
System.out.println("Student·1:·A↵");
}
else if(c[k]>=80&&c[k]<=89){
System.out.println("Student·1:·B↵");
}
else if(c[k]>=70&&c[k]<=79){
System.out.println("Student·1:·C↵");
}
else if(c[k]>=60&&c[k]<=69){
System.out.println("Student·1:·D↵");
}
else if(c[k]<=60){
System.out.println("Student·1:·F↵");
}
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=5;
int a[]=new int[n];
for(int i=1;i<=5;i++){
System.out.print("\nEnter.test.grade.for.student.");
System.out.print(i);
System.out.print(":");
a[i-1]=sc.nextInt();
}
Main obj=new Main();
obj.determineGrade(a);
obj.calcAverage(a);
}
}
OUTPUT: