(java) Grade Book A teacher has five students who have each taken four tests. Th
ID: 3852221 • Letter: #
Question
(java) Grade Book
A teacher has five students who have each taken four tests. The teacher uses the following grading
scale to assign a letter grade to a student, based on the average of his or her four test scores:
Test Score Letter Grade
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
Write a class that uses a String array or an ArrayList object to hold the five students' names,
an array of five characters to hold the five students' letter grades, and five arrays of four
doubles each to hold each student's set of test scores. You may find using a single 5x4
multi-dimensional array easier to manage instead of a separate array for each set of test scores.
The class should have methods that return a specific student's name, the average test score, and a
letter grade based on the average. Although averages are often floating-point values, you should
cast the average test score to an integer when comparing with the grading scale. This reduces the
possibility of error. Demonstrate the class in a program that allows the user to enter each
student's name and his or her four test scores. It should then display each student's average test
score and letter grade.
Input Validation: Do not accept test scores less than zero or greater than 100.
Run the code like the following one:
Enter student 1 name:Louis Bleriot
Enter student Louis Bleriot's grade for test 1:80
Enter student Louis Bleriot's grade for test 2:80
Enter student Louis Bleriot's grade for test 3:80
Enter student Louis Bleriot's grade for test 4:80
Enter student 2 name:Howard Hughes
Enter student Howard Hughes's grade for test 1:45
Enter student Howard Hughes's grade for test 2:66
Enter student Howard Hughes's grade for test 3:24
Enter student Howard Hughes's grade for test 4:68
Enter student 3 name:Amelia Earhart
Enter student Amelia Earhart's grade for test 1:87
Enter student Amelia Earhart's grade for test 2:75
Enter student Amelia Earhart's grade for test 3:66
Enter student Amelia Earhart's grade for test 4:84
Enter student 4 name:Charles Lindenergh
Enter student Charles Lindenergh's grade for test 1:66
Enter student Charles Lindenergh's grade for test 2:65
Enter student Charles Lindenergh's grade for test 3:62
Enter student Charles Lindenergh's grade for test 4:62
Enter student 5 name:Manfred von Richtofen
Enter student Manfred von Richtofen's grade for test 1:97
Enter student Manfred von Richtofen's grade for test 2:98
Enter student Manfred von Richtofen's grade for test 3:99
Enter student Manfred von Richtofen's grade for test 4:100
=== Grade Book Data ===
Student name: Louis Bleriot
Average test score: 80.00
Letter grade: B
= = = = =
Student name: Howard Hughes
Average test score: 50.75
Letter grade: F
= = = = =
Student name: Amelia Earhart
Average test score: 78.00
Letter grade: C
= = = = =
Student name: Charles Lindenergh
Average test score: 63.75
Letter grade: D
= = = = =
Student name: Manfred von Richtofen
Average test score: 98.50
Letter grade: A
= = = = =
Explanation / Answer
GradeBook.java
import java.util.ArrayList;
public class GradeBook {
//Creating an ArrayList
ArrayList<String> arl = new ArrayList<String>();
//Creating an Char array
char grades[] = new char[5];
//Creating 2-Dimensional array which stores test scores of each student
int testScores[][] = new int[5][4];
//Parameterized constructor
public GradeBook(ArrayList<String> arl, int[][] testScores) {
this.arl = arl;
this.testScores = testScores;
}
public String studentName(int index) {
return arl.get(index);
}
//Method which calculates the average of test scores of each student
public double average(int index) {
int tot = 0;
for (int j = 0; j < 4; j++) {
tot += testScores[index][j];
}
return ((double)tot / 4);
}
//Method which finds the grade letter of each student
public char gradeLetter(int index) {
if ((int)average(index) >= 90 && (int)average(index) <= 100)
grades[index] = 'A';
else if ((int)average(index) >= 80 && (int)average(index) <= 89)
grades[index] = 'B';
else if ((int)average(index) >= 70 && (int)average(index) <= 79)
grades[index] = 'C';
else if ((int)average(index) >= 60 && (int)average(index) <= 69)
grades[index] = 'D';
else if ((int)average(index) >= 0 && (int)average(index) <= 59)
grades[index] = 'F';
return grades[index];
}
}
________________________
Test.java
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Declaring variables
String name;
int marks,j;
int score;
int testScores[][]=new int[5][4];
ArrayList<String> arl=new ArrayList<String>();
//Getting the data entered by the user
for(int i=0;i<5;i++)
{
j=0;
//Getting the name entered by the user
System.out.print("Enter student "+(i+1)+" name:");
name=sc.nextLine();
arl.add(name);
//getting the test scores of each student in 4 subjects
while(j<4)
{
System.out.print("Enter student "+name+" grade for test "+(j+1)+":");
score=sc.nextInt();
if(score<0 || score>100)
{
System.out.println("** Invalid Score **");
continue;
}
else
{
testScores[i][j]=score;
j++;
}
sc.nextLine();
}
}
//Creating an GradeBook class object
GradeBook gb=new GradeBook(arl, testScores);
System.out.println("=== Grade Book Data ===");
//Displaying Each student data as output
for(int i=0;i<5;i++)
{
System.out.println("Student Name :"+gb.studentName(i));
System.out.println("Average test score: "+gb.average(i));
System.out.println("Letter grade: "+gb.gradeLetter(i));
System.out.println("= = = = =");
}
}
}
_________________________
Output:
Enter student 1 name:Louis Bleriot
Enter student Louis Bleriot grade for test 1:80
Enter student Louis Bleriot grade for test 2:80
Enter student Louis Bleriot grade for test 3:80
Enter student Louis Bleriot grade for test 4:80
Enter student 2 name:Howard Hughes
Enter student Howard Hughes grade for test 1:45
Enter student Howard Hughes grade for test 2:66
Enter student Howard Hughes grade for test 3:24
Enter student Howard Hughes grade for test 4:68
Enter student 3 name:Amelia Earhart
Enter student Amelia Earhart grade for test 1:87
Enter student Amelia Earhart grade for test 2:75
Enter student Amelia Earhart grade for test 3:66
Enter student Amelia Earhart grade for test 4:84
Enter student 4 name:Charles Lindenergh
Enter student Charles Lindenergh grade for test 1:66
Enter student Charles Lindenergh grade for test 2:65
Enter student Charles Lindenergh grade for test 3:62
Enter student Charles Lindenergh grade for test 4:62
Enter student 5 name:Manfred von Richtofen
Enter student Manfred von Richtofen grade for test 1:97
Enter student Manfred von Richtofen grade for test 2:98
Enter student Manfred von Richtofen grade for test 3:99
Enter student Manfred von Richtofen grade for test 4:100
=== Grade Book Data ===
Student Name :Louis Bleriot
Average test score: 80.0
Letter grade: B
= = = = =
Student Name :Howard Hughes
Average test score: 50.75
Letter grade: F
= = = = =
Student Name :Amelia Earhart
Average test score: 78.0
Letter grade: C
= = = = =
Student Name :Charles Lindenergh
Average test score: 63.75
Letter grade: D
= = = = =
Student Name :Manfred von Richtofen
Average test score: 98.5
Letter grade: A
= = = = =
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.