Need help finishing the program that uses the class for this problem. I already
ID: 660593 • Letter: N
Question
Need help finishing the program that uses the class for this problem. I already have the class done and will attach it below.
_______________________________
Problem
LA teacher has five students who have 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.
Write a class that uses a String array (or an ArrayList object) to hold the five students
Test Score Letter Grade 90-100 A 80-89 B 70-79 C 60-69 D 0-59 FExplanation / Answer
//GradeBook.java
//The class is modified in getLetterGrade method.
public class GradeBook
{
private final int NUM_STUDENTS=5;
private final int NUM_TESTS=4;
private String[]names=new String[NUM_STUDENTS];
private char[]grades=new char[NUM_STUDENTS];
private double[]scores1=new double[NUM_TESTS];
private double[]scores2=new double[NUM_TESTS];
private double[]scores3=new double[NUM_TESTS];
private double[]scores4=new double[NUM_TESTS];
private double[]scores5=new double[NUM_TESTS];
public void setName(int studentNumber,String name)
{
names[studentNumber-1]=name;
}
public void setScores(int studentNumber, double[] scores)
{
switch(studentNumber)
{
case 1:copyArray(scores1,scores);break;
case 2:copyArray(scores2,scores);break;
case 3:copyArray(scores3,scores);break;
case 4:copyArray(scores4,scores);break;
case 5:copyArray(scores5,scores);break;
default:break;
}
}
public String getName(int studentNumber)
{
return names[studentNumber-1];
}
public double getAverage(int studentNumber)
{
double avg=0.0;
switch(studentNumber)
{
case 1:avg=calcAverage(scores1);break;
case 2:avg=calcAverage(scores2);break;
case 3:avg=calcAverage(scores3);break;
case 4:avg=calcAverage(scores4);break;
case 5:avg=calcAverage(scores5);break;
default:break;
}
return avg;
}
public char getLetterGrade(int studentNumber)
{
char lettergrade;
//The modified code that consider only integer part because
//the decimal points are not covered in the average in if else case
//So type cast the average from double to integer
//will ignore the decimal value of average
//so that output will come as expected otherwise for Sam Young
//79.5 average will result F grade.
int average=(int) getAverage(studentNumber);
if(average>=90&&average<=100)
lettergrade='A';
else if(average>=80&&average<=89)
lettergrade='B';
else if(average>=70&&average<=79)
lettergrade='C';
else if(average>=60&&average<=69)
lettergrade='D';
else
lettergrade='F';
return lettergrade;
}
private void copyArray(double[]to,double[]from)
{
System.arraycopy(from,0,to,0,from.length);
}
private double calcAverage(double[]scores)
{
double sum=0;
for(int i=0; i<scores.length;i++)
sum+=scores[i];
return sum/scores.length;
}
public char determineGrade(double average)
{
char lettergrade;
if(average>=90&&average<=100)
lettergrade='A';
else if(average>=80&&average<=89)
lettergrade='B';
else if(average>=70&&average<=79)
lettergrade='C';
else if(average>=60&&average<=69)
lettergrade='D';
else
lettergrade='F';
return lettergrade;
}
public char[]getGrades()
{
return grades;
}
public void setGrades(char[]grades)
{
this.grades=grades;
}
}
----------------------------------------------------------------------------------------------------------------------------
/**
* The java program that prompts user to enter the name of the student
* and four test scores for each student and finds the letter grade
* and print the student names, average test scores and grade letter *
* */
//GradeBookDemo.java
import java.util.ArrayList;
import java.util.Scanner;
public class GradeBookDemo
{
//create static variables
private static final int NUM_STUDENTS=5;
private static final int NUM_TESTS=4;
//create an instance orf GradeBoook class
private static GradeBook gradeBook=new GradeBook();
//create an array of testScore to store 4 test scores
private static double[] testScore=new double[NUM_TESTS];
//create an arraylist of names to store student names
private static ArrayList<String>studentNames=new ArrayList<String>();
public static void main(String[] args)
{
//create an Scanner class object to read input
Scanner input=new Scanner(System.in);
//create a string variable to store student name
String name;
//Read student name and test scores for five students
for (int student = 1; student <=NUM_STUDENTS; student++)
{
System.out.printf("Name of Student : ");
//prompt for the name of the student
name=input.nextLine();
studentNames.add(student-1, name);
gradeBook.setName(student, name);
//Read 4 test scores
for (int score = 0; score < NUM_TESTS; score++)
{
System.out.printf("Enter score["+(score+1)+"] : ");
//repeat prompting for test score if the testscore is less than 0 or greater than 100
do
{
//parse the string score value to double type using parseDouble method of Double class
testScore[score]=Double.parseDouble(input.nextLine());
//print a message if score is invalid
if(testScore[score]<0 ||testScore[score]>100)
System.out.println("Invalid Test Score. Enter valid score ");
}while(testScore[score]<0 ||testScore[score]>100);
}
//call the method setScores that takes student index number and array of testScores of double type
gradeBook.setScores(student, testScore);
//reset name and test scores
name="";
for (int i = 0; i < testScore.length; i++)
testScore[i]=0.0;
}
//call the method printStudentGrades to print the student names, average test scores
//and corresponding letter grade
printStudentGrades();
}
/*The method printStudentGrades that prints the student names, average test scores
and corresponding letter grade. */
public static void printStudentGrades()
{
for (int index = 1; index <=NUM_STUDENTS; index++)
{
//print name left justified with 25 character width
//print avaerage score left justified with 10 character width and two decimal places
//print a grade letter of 10 characters width
System.out.printf("Name : %-25s Average Score : %-10.2f Grade : %-10c ",
studentNames.get(index-1),
gradeBook.getAverage(index),
gradeBook.getLetterGrade(index));
}
}//end of the method printStudentGrades
}//end of class GradeBookDemo
/*Note:
*The GradeBook class contains methods to get average and to find the grade letters
*finding methods. In thid demo program, the class GradeBook is used to read student
*data and set data to the GradeBook class. Then calling the getAverage and getLetterGrade
*methods to print the output as shown in the question.
* **/
----------------------------------------------------------------------------------------------------------------------------------------------------
sample output:
Name of Student : Joanne Smith
Enter score[1] : 98
Enter score[2] : 89
Enter score[3] : 100
Enter score[4] : 76
Name of Student : Will Jones
Enter score[1] : 67
Enter score[2] : 89
Enter score[3] : 91
Enter score[4] : 88
Name of Student : kerry McDonald
Enter score[1] : 78
Enter score[2] : 79
Enter score[3] : 88
Enter score[4] : 91
Name of Student : Sam young
Enter score[1] : 88
Enter score[2] : 98
Enter score[3] : 76
Enter score[4] : 56
Name of Student : Jill Barnes
Enter score[1] : 94
Enter score[2] : 93
Enter score[3] : 91
Enter score[4] : 98
Name : Joanne Smith Average Score : 90.75 Grade : A
Name : Will Jones Average Score : 83.75 Grade : B
Name : kerry McDonald Average Score : 84.00 Grade : B
Name : Sam young Average Score : 79.50 Grade : C
Name : Jill Barnes Average Score : 94.00 Grade : A
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.