In: Computer Science
Create a Java application to simulate a “GradeBook”. A teacher
has five students who have taken four exams. The teacher uses the
following grading scale to assign a letter grade to a student,
based on the average of his or her four exam scores:
Average |
Letter Grade |
90 – 100 |
A |
80 – 89 |
B |
70 – 79 |
C |
60 – 69 |
D |
0 – 59 |
F |
Write logic to create a String array to hold student names, a
character array to hold student letter grades, and a
two-dimensional array to hold each of the five students’ test
scores for each of the four exams completed during the
semester.
Use nested for loop logic to fill the names and test
scores arrays. Do not accept test scores less than zero or greater
than 100. Make sure you test both ends of the range!
Use another nested for loop to compute the average test
score for each student and then assign the corresponding letter the
letter grade array. Make sure you test each letter grade
value!
Use while loop logic to display a table showing each
student’s name and letter grade.
Code - Main.java
import java.util.*;
public class Main
{
public static void main(String[] args) {
//Scanner to read user input
Scanner sc = new Scanner(System.in);
//array declared and variables
String sName[] = new String[5];
char grade[] = new char[5];
int testScore[][] = new int[5][4];
int i,j;
int average[] = new int[5];
//Nested for loop to get the student and marks for the
corresponding student in 4 subject
for(i = 0;i<sName.length;i++){
System.out.println("Enter student name "+(i+1)+":
");
sName[i] = sc.next();
for(j=0;j<testScore[i].length;){
System.out.println("Enter marks in "+(j+1)+" subject:
");
testScore[i][j] = sc.nextInt();
//if marks is greater than 100 and less than 0
if(testScore[i][j]<0 ||
testScore[i][j]>100)
System.out.println("Pls enter marks between 0 to
100");
else
j++;
}
System.out.println();
}
//for loop to calculate the average
int sum;
for(i = 0;i<testScore.length;i++){
sum = 0;
for(j=0;j<testScore[i].length;j++){
sum += testScore[i][j];
}
average[i] = sum / testScore[i].length;
}
//for loop to calculate the grade
for(i = 0;i<average.length;i++){
if(average[i]>=90 &&
average[i]<=100){
grade[i] = 'A';
}
else if(average[i]>=80 &&
average[i]<=89){
grade[i] = 'B';
}
else if(average[i]>=70 &&
average[i]<=79){
grade[i] = 'C';
}
else if(average[i]>=60 &&
average[i]<=69){
grade[i] = 'D';
}
else if(average[i]>=0 &&
average[i]<=59){
grade[i] = 'F';
}
}
int count = 0;
//while loop to print the result out
while(count<sName.length){
System.out.println("Student name: "+sName[count]+"
grade: "+grade[count]);
count++;
}
}
}
Screenshots -