Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 1: A teacher has five students who have taken a test. The teacher uses

ID: 642389 • Letter: E

Question

Exercise 1: A teacher has five students who have taken a test. The teacher uses the following grading scale to assign a letter grade to a student, based on the test score: Write a class called Grades that has the following attributes: . String array to hold the five students' names . an array of five characters to hold the five students' letter grades, . and an array of five doubles to hold the five test scores and the following methods: . No-args constructor (Note: the arrays are created in the declarations). . Method setName( String, int) that puts the name in the name array at a given index. . Method getName(int) that returns the name from the name array from a given index . Method setScore( double, int) that puts the test score in the scores array at a given index . Method getScore(int) that returns the score from the scores array from a given index . Method findGrade(int) that determines the grade given an index and puts it in the grades array. . Method getGrade(int) that returns the grade from the grades array from a given index. Demonstrate the class in a program that allows the user to enter each student's name and his or her test score scores. It should then display each students name, test score and letter grade. Input validation: Do not accept test scores less than zero or greater than 100.

Explanation / Answer

Program:

Student Class:

public class Student{
private String name;
private double[] test = new double[4];

public Student(){
name = " ";
}

public Student(String n, double[] t){
name = n;
test = t;
}

public void setName(String n)
{
name = n;
}

public String getName(){
return name;
}

public void setTest(double t,int i)
{
test[i] = t;
}

public double getTest(int i)
{
return test[i];
}

public double getTestAvg(){

double sum = 0;
double avg;
for(int i = 0; i < test.length; i++)
{
sum += test[i];
}
avg = sum / test.length;
return avg;
}

public char getLetterGrade(){

double average = getTestAvg();
char grade=0;

if(average >= 90)
grade = 'A';
else if (average >= 80)
grade = 'B';
else if (average >= 70)
grade = 'C';
else if (average >=60)
grade = 'D';
else if (average < 60)
grade = 'F';

return grade;
}

public String toString(){
String str = "";
str += " Name of student: " + name;
str += " Average test score: " + getTestAvg();
str += " Letter grade: " + getLetterGrade();
return str;
}
}   

Main Program:
import java.util.Scanner;
import java.io.*;

public class GradeBook {

public static void main(String[] args) throws IOException {

Student[] students = new Student[5];

getStudentData(students);

}

public static double getStudentData(Student[] array) {
Scanner scan = new Scanner(System.in);
String[] student = new String[5];
double[] test = new double[4];
for (int i = 0; i < student.length; i++) {
System.out.println("Enter the name of the student : ");
student[i] = scan.nextLine();
for (int j = 0; j < test.length; j++) {
System.out.println("Enter score " + (j + 1) + " for the student");
test[j] = scan.nextDouble();
scan.nextLine();
}
array[i] = new Student(student[i], test);

}
return 0;

}
}