In: Computer Science
LAB3
YearGradeBook
A teacher has 5 students who each will have 4 marking period scores. The teacher uses the following grading scale to assign a year end letter grade to a student, based on the average of his or her 4 marking period scores.
Marking Period Score Letter Grade
92-100
A
83-91
B
74-82
C
65-73
D
0-64
F
Write a class that uses a String array or an ArrayList object to
hold the students’ names, an array of five characters to hold the
five student’s letter grades, and five arrays of four doubles each
to hold each student’s set of marking period scores. You may find
using a single 5x4 multi-dimensional array easier to manage instead
of a separate array for each set of marking period scores.
The class should have methods that return a specific student’s name, the year end score (average marking period score), and a letter grade based on the average. Although averages are often floating-point values, you should cast the year end score (average marking period score) to an integer when comparing with the grading scale. This reduces the possibility of error. Demonstrate the class in a program that allow the user to enter each student’s name and his or her four marking period scores. It should then display each student’s year end score and letter grade.
Input Validation: Do not accept marking period score less than zero or greater than 100.
Refer to “GradeBook solution.docx” for examples of how to complete this lab.
Your submission to BB should be your “YearGradeBook.java” file only, not included in a .zip file or a .gpj file.
the starting code is below
import java.util.Scanner;
public class GradeBook {
String[] studentNames;
char[] letterGrades;
double[][] testGrades;
public GradeBook() {
studentNames = new String[5];
letterGrades = new char[5];
testGrades = new double[5][4];
return;
}
public String getStudentName(int number) {
if ((number >= 1) && (number <= 5))
return studentNames[number - 1];
return null;
}
public double getAvgTestScore(int number) {
if ((number >= 1) && (number <= 5)) {
int sum = 0;
for (int i = 0; i < 4; i++)
sum += testGrades[number - 1][i];
return (sum / 4.0);
}
return -1.0;
}
public char getLetterGrade(double avgScore) {
int avg = (int) avgScore;
if ((avg >= 90) && (avg <= 100))
return 'A';
else if ((avg >= 80) && (avg <= 89))
return 'B';
else if ((avg >= 70) && (avg <= 79))
return 'C';
else if ((avg >= 60) && (avg <= 69))
return 'D';
else return 'F';
}
public static void main(String[] args) {
GradeBook record = new GradeBook();
Scanner input = new Scanner(System.in);
int score;
for (int i = 1; i <= 5; i++) {
System.out.printf("Enter student %d name: ", i);
record.studentNames[i - 1] = input.nextLine();
for (int j = 1; j <= 4; j++) {
while (true) {
System.out.printf("Enter student %s's"
+ " grade for test %d: ",
record.studentNames[i - 1], j);
score = input.nextInt();
input.nextLine();
if ((score >= 0) && (score <= 100)) {
record.testGrades[i - 1][j - 1] = score;
break;
}
System.out.printf("Score cannot be less than 0"
+ " or greater than 100!"
+ " Please try again.%n");
}
}
}
double avg;
System.out.printf("%n=== Grade Book Data ===%n%n");
for (int i = 1; i <= 5; i++) {
avg = record.getAvgTestScore(i);
System.out.printf("Student name: %s%n"
+"\tAverage test score: %.2f%n"
+ "\tLetter grade: %c%n= = = = =%n",
record.getStudentName(i),
avg, record.getLetterGrade(avg));
}
return;
}
}
Code
import java.util.Scanner;
public class GradeBook
{
String[] studentNames;
char[] letterGrades;
double[][] testGrades;
public GradeBook() {
studentNames = new String[5];
letterGrades = new char[5];
testGrades = new double[5][4];
}
public String getStudentName(int number) {
if ((number >= 1) && (number <= 5))
return studentNames[number - 1];
return null;
}
public double getAvgTestScore(int number)
{
if ((number >= 1) && (number <= 5)) {
int sum = 0;
for (int i = 0; i < 4; i++)
sum += testGrades[number - 1][i];
return (sum / 4.0);
}
return -1.0;
}
public char getLetterGrade(double avgScore) {
int avg = (int) avgScore;
if ((avg >= 90) && (avg <= 100))
return 'A';
else if ((avg >= 80) && (avg <= 89))
return 'B';
else if ((avg >= 70) && (avg <= 79))
return 'C';
else if ((avg >= 60) && (avg <= 69))
return 'D';
else return 'F';
}
public static void main(String[] args)
{
GradeBook record = new GradeBook();
Scanner input = new Scanner(System.in);
int score;
for (int i = 1; i <= 5; i++)
{
System.out.printf("Enter student %d name: ", i);
record.studentNames[i - 1] = input.nextLine();
for (int j = 1; j <= 4; j++)
{
while (true)
{
System.out.printf("Enter student %s's"+ " grade for test %d:
",record.studentNames[i - 1], j);
score = input.nextInt();
input.nextLine();
if ((score >= 0) && (score <= 100))
{
record.testGrades[i - 1][j - 1] = score;
break;
}
System.out.printf("Score cannot be less than 0 or greater than 100!
Please try again.%n");
}
}
}
double avg;
System.out.printf("%n=== Grade Book Data ===%n%n");
for (int i = 1; i <= 5; i++)
{
avg = record.getAvgTestScore(i);
System.out.printf("Student name: %s%n"
+"\tAverage test score: %.2f%n"
+ "\tLetter grade: %c%n= = = = =%n",
record.getStudentName(i),
avg, record.getLetterGrade(avg));
}
}
}
output
your program is running well ..
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.