Java:
A 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:
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. The class should have methods that return a specific student’s name, average test score, and a letter grade based on the average.
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.
The Java Program for taking input for and calculating the average score and letter grades for five students is given below. Comments have been added for ease of understanding. If you have any other queries/doubts, please leave a comment and I will get back to you.
Student_Grades.java
import java.util.Scanner;
public class Student_Grades {
//declare private variables for student name, letter grades
//and a 2D 5x4 Matrix for storing the scores of 5 students in 4 subjects
private String studentNames[];
private char letterGrades[];
private double scores[][];
//Initialize the private variables in default constructor
public Student_Grades() {
studentNames = new String[5];
letterGrades = new char[5];
scores = new double[5][4];
}
//Take user input for each student's name and test scores, after which calculate and show their
//average score and letter grade
public void getInput() {
Scanner in = new Scanner(System.in);
for(int i = 0; i < 5; i++) {
System.out.println();
System.out.println("**Enter data for student "+(i+1)+"***");
System.out.println("******************************");
System.out.print("Name : ");
studentNames[i] = in.next();
System.out.println("Enter Test scores of four subjects one by one");
for(int j = 0; j < 4; j++) {
//Do input validation, use a do while loop to check if the scores entered are in the
//range 0-100. If not make the user enter input until a vali score is entered
do {
System.out.print("Test "+(j+1)+": ");
scores[i][j] = in.nextDouble();
if(scores[i][j] < 0 || scores[i][j] > 100)
System.out.println("Invalid score. Has to be between 0 and 100. Try again!");
} while(scores[i][j] < 0 || scores[i][j] > 100);
}
//calculate average score and letter grade
double avgScore = averageScore(scores[i]);
letterGrades[i] = getLetterGrade(avgScore);
System.out.println("Average Score : "+avgScore);
System.out.println("Letter Grade : "+letterGrades[i]);
System.out.println();
}
in.close();
}
//takes an double array as its argument and returns the mean score
private double averageScore(double scores[]) {
double total = 0;
for(int i = 0; i < scores.length; i++)
total += scores[i];
return total/scores.length;
}
//returns the letter grade given the average score
private char getLetterGrade(double avgScore) {
if(avgScore >= 90 && avgScore <= 100)
return 'A';
else if(avgScore >= 80 && avgScore <= 89)
return 'B';
else if(avgScore >= 70 && avgScore <= 79)
return 'C';
else if(avgScore >= 60 && avgScore <= 69)
return 'D';
else
return 'F';
}
//returns the name of the student at index ind if ind is valid (between 0 and 4)
//if ind is invalid it returns an error message
public String getStudentName(int ind) {
if(ind >= 0 && ind < 5) {
return studentNames[ind];
}
else
return "ERROR: Index Out of range";
}
//returns the average score of the student at index ind if ind is valid (between 0 and 4)
//if ind is invalid it returns -1.0
public double getStudentAvgScore(int ind) {
if(ind >= 0 && ind < 5) {
return averageScore(scores[ind]);
}
else {
System.out.println("ERROR: Index Out of range");
return -1.0;
}
}
//returns the letter grade of the student at index ind if ind is valid (between 0 and 4)
//if ind is invalid it returns 'X'
public char getStudentLetterGrade(int ind) {
if(ind >= 0 && ind < 5) {
return getLetterGrade(averageScore(scores[ind]));
}
else {
System.out.println("ERROR: Index Out of range");
return 'X';
}
}
public static void main(String args[]) {
System.out.println();
System.out.println("Creating a Student_Grades Object");
Student_Grades sg = new Student_Grades();
System.out.println("Now taking user input...");
sg.getInput();
System.out.println("Testing the public methods");
System.out.println("Get the details of student 1");
System.out.println("Name: "+sg.getStudentName(0));
System.out.println("Average Score: "+sg.getStudentAvgScore(0));
System.out.println("Letter Grade: "+sg.getStudentLetterGrade(0));
System.out.println("Get the details of student 4");
System.out.println("Name: "+sg.getStudentName(3));
System.out.println("Average Score: "+sg.getStudentAvgScore(3));
System.out.println("Letter Grade: "+sg.getStudentLetterGrade(3));
System.out.println("Get the details of student 6");
System.out.println("Name: "+sg.getStudentName(5));
System.out.println("Average Score: "+sg.getStudentAvgScore(5));
System.out.println("Letter Grade: "+sg.getStudentLetterGrade(5));
}
}
Sample Run:
Get Answers For Free
Most questions answered within 1 hours.