Create a Java application (with Javadoc comments) to simulate another “GradeBook
ID: 3796881 • Letter: C
Question
Create a Java application (with Javadoc comments) to simulate another “GradeBook”. A teacher has any number of students who have taken five tests. The teacher uses the following grading scale to assign a letter grade to a student, based on the average of his or her five test scores:
Test Score
Letter Grade
90 – 100
A
80 – 89
B
70 – 79
C
60 – 69
D
0 – 59
F
Write logic to ask the teacher how many students they have. Repeat the following logic for each student in the class:
Ask for the student name. Write the following methods in the program to process each student.
inputScores – this method should ask the user to enter five test scores and store them in an array. Do not accept test scores less than 25 or greater than 100.
calcAverage – this method should accept an array of five test scores as the argument and return the average of the scores
determineGrade – this method should accept the average test score as an argument and return a letter grade for the score based on the grade scale above.
printStudentGradeInfo – this method should accept the name of the student, the average test score, and the letter grade. This method will display each student’s information on a single line separated by two ‘tab’ widths.
Test Score
Letter Grade
90 – 100
A
80 – 89
B
70 – 79
C
60 – 69
D
0 – 59
F
Explanation / Answer
GradeBook.java
import java.util.Scanner;
public class GradeBook {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter how many students you have: ");
int n = scan.nextInt();
String names[] = new String[n];
int scores[][] = new int[n][5];
double average[] = new double[n];
char grades[] = new char[n];
inputScores(scores, names, scan);
for(int i=0; i<names.length; i++){
average[i] = calcAverage(scores[i]);
grades[i] = determineGrade(average[i]);
printStudentGradeInfo(names[i], average[i], grades[i]);
}
}
public static void inputScores (int scores[][], String name[], Scanner scan ){
for(int i=0; i<scores.length; i++){
System.out.print("Enter the student name: ");
name[i] = scan.next();
for(int j=0; j<scores[i].length; j++){
System.out.print("Enter the student "+(i+1)+" score "+(j+1)+": ");
scores[i][j] = scan.nextInt();
if(scores[i][j] <25 || scores[i][j] > 100){
System.out.println("Invalid score. Score should be between 25 and 100");
j--;
}
}
}
}
public static double calcAverage (int scores[]){
int sum = 0;
for(int i=0; i<scores.length; i++){
sum = sum + scores[i];
}
return sum/(double)scores.length;
}
public static char determineGrade (double average){
if(average>=90 && average<=100){
return 'A';
}
else if(average>=80 && average<90){
return 'B';
}
else if(average>=70 && average<80){
return 'C';
}
else if(average>=60 && average<70){
return 'D';
}
else{
return 'F';
}
}
public static void printStudentGradeInfo (String name, double average, char grade){
System.out.println(name+" "+average+" "+grade);
}
}
Output:
Enter how many students you have:
2
Enter the student name: Suresh
Enter the student 1 score 1: 50
Enter the student 1 score 2: 60
Enter the student 1 score 3: 70
Enter the student 1 score 4: 80
Enter the student 1 score 5: 22
Invalid score. Score should be between 25 and 100
Enter the student 1 score 5: 55
Enter the student name: Sekhar
Enter the student 2 score 1: 66
Enter the student 2 score 2: 77
Enter the student 2 score 3: 88
Enter the student 2 score 4: 99
Enter the student 2 score 5: 100
Suresh 63.0 D
Sekhar 86.0 B