In: Computer Science
rite a java program that uses the methods written in this section to do the following:Prompt the user for a number of grades using the getValidInput method. Ensure the number of grades is greater than 1 and less than 30. The error message is “You must enter a number between 1 and 30, please re-enter:”Prompt the user to enter the requested grades and total marks using the calculateLetterGrade method. The grades should be between 0 and 100. Display an appropriate error message where required. Use variables to count the number of each letter grade returned.When all grades have been entered display the distribution of grades using the showGradeDistribution method.?
import java.util.*;
public class Sample{
public static int getValidInput(){
Scanner input=new Scanner(System.in);
System.out.print("Enter grade greater then 1 and lessthan
30:");
while(true){
int n=input.nextInt();
if(n>=1 && n<=30){
return n;
}else{
System.out.print("You must enter the number between 1 and
30:");
}
}
}
public static int[] calculateLetterGrade(int n){
Scanner input=new Scanner(System.in);
int[] a=new int[]{0,0,0,0,0,0};
for(int i=0;i<n;i++){
System.out.print("Enter grade:");
while(true){
int grade=input.nextInt();
if(grade>=0 && grade<=100){
if(grade>90 && grade<=100)a[0]+=1;
else if(grade>80 && grade<=90)a[1]+=1;
else if(grade>70 && grade<=80)a[2]+=1;
else if(grade>60 && grade<=70)a[3]+=1;
else if(grade>50 && grade<=60)a[4]+=1;
else a[5]+=1;
break;
}else{
System.out.print("Enter the grade between 0 to 100 only:");
}
}
}
return a;
}
public static void GradeDistribution(int a[]){
char b[]=new char[]{'A','B','C','D','E','F'};
for(int i=0;i<6;i++){
System.out.println(b[i]+" "+a[i]);
}
}
public static void main(String args[]){
int cnt=getValidInput();
int[] a=new int[6];
a=calculateLetterGrade(cnt);
GradeDistribution(a);
}
}