A teacher has five students who have taken four tests. The teacher uses the foll
ID: 3681887 • Letter: A
Question
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:
A=90-100
B=80-89
C=70-79
D= 60-69
F = 0-59
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 calss in a program that allows the user to enter each students name and his or her four test scores. It should display each students average test score and letter grade.
Do not accept test scores less than zero or greater than 100.
Explanation / Answer
GradeBookDemo.java
import java.util.*; //needed for scanner
import java.io.*; //needed for File and IO Exception
public class GradeBookDemo
{
public static void main(String[] args) throws FileNotFoundException
{
//Objects
Scanner keyboard = new Scanner(System.in); //Scanner for reading input
File inputFile; //File for opening student info file
Scanner classList; //Scanner for reading student info file
GradeBook testScores; //GradeBook for holding students names and test scores
//Variables
String selection; //String for temp. holding user selected options
String file; //String for holding name of student info file
int i; //Int used for array index a.k.a student number
//Display welcome
System.out.println("Welcome to the GradeBook demo");
//Select default or custom mode
do{
System.out.print("Do you want to use default settings [y,n]? ");
selection = keyboard.nextLine();
}while(Check.isYesNo("v",selection));
//Default settings
if(Check.isYes(selection))
{
file = "StudentInfo.txt";
testScores = new GradeBook();
}
//Custom settings
else
{
int classSize;
do{
System.out.print(" How many students are in the class[>0]? ");
try{
classSize = keyboard.nextInt();
}
catch(InputMismatchException e)
{
classSize = -1; //Invalidate class size
}
//Clear buffer
keyboard.nextLine();
//Validate classSize
}while(Check.isGreaterThan("v",classSize,0));
//Get name of custom input file
System.out.print(" Enter the name of the student info file: ");
selection = keyboard.nextLine();
//Set custom settings
file = selection;
testScores = new GradeBook(classSize,4);
}//End custom settings
//Initlise input file
inputFile = new File(file);
//Check if input file exists
while(!(inputFile.exists()))
{
//Get new / correct file name
System.out.println(" Error: student info file "" + file + "" does not exist");
System.out.print("Re-enter file name or q to quit: ");
selection = keyboard.nextLine();
//quit program if user enters "q"
if(selection.equals("q"))
System.exit(1);
//Reinitlise input file
file = selection;
inputFile = new File(file);
}//End check input file exists
//Initlise Scanner classList to read the input file
classList = new Scanner(inputFile);
//Read the input file
try{
for(i=0;classList.hasNext();i++)
{
testScores.setName(i,classList.nextLine());
for(int n=0; n<4; n++)
testScores.setTestScore(n,i,classList.nextDouble());
if(classList.hasNext())
classList.nextLine(); //Clear end of line character after 4th test score
}
}
catch(InvalidStudentNumberException e)
{
System.out.println();
System.out.println(e.getMessage());
System.out.println("Please check the number of entries in the student info file "" + file + """);
}
catch(InvalidTestScoreException e)
{
System.out.println();
System.out.println(e.getMessage());
System.out.println("Please check the student info file "" + file + "" for errors");
System.exit(1);
}
catch(InvalidTestNumberException e)
{
System.out.println();
System.out.println("Error in code: Test does not exist");
System.exit(1);
}
catch(InputMismatchException e)
{
System.out.println();
System.out.println("Error in student info file "" + file +""");
System.out.println("Please ensure format is as follows:");
System.out.println("Student Name");
System.out.println("Test1 Score");
System.out.println("Test2 Score");
System.out.println("Test3 Score");
System.out.println("Test4 Score");
System.exit(1);
}
catch(NoSuchElementException e)
{
System.out.println();
System.out.println("Error: Data missing from student info file "" + file +""");
System.out.println("Please ensure format is as follows:");
System.out.println("Student Name");
System.out.println("Test1 Score");
System.out.println("Test2 Score");
System.out.println("Test3 Score");
System.out.println("Test4 Score");
System.exit(1);
}
//Close the student info file
classList.close();
//Display the results
System.out.println(testScores);
}//End main method
//End class GradeBookDemo
}
GradeBook.java
/*GradeBook Class*/
/*
Stores a students name and test scores in arrays.
The arrays index number is used as the student number.
Has methods to:
set the students name
set the students test score
get the students name
get the students average score
get the students letter grade bassed on their average test score
*/
import java.text.DecimalFormat; //need for DecimalFormat object
public class GradeBook
{
//Declare arrays
private String[] name;
private double[][] tests;
private char[] grade;
//No arg constructor, used by default for classes up to 5 students
public GradeBook()
{
name = new String[5];
tests = new double[4][5];
grade = new char[5];
}
//Constructor that accepts the number of students in the class as an int
public GradeBook(int classSize, int NumberOfTests)
{
name = new String[classSize];
tests = new double[NumberOfTests][classSize];
grade = new char[classSize];
}
//Sets the students name, accepts student number (>=0, <=class size) and student name
public void setName(int studentNumber, String studentName) throws InvalidStudentNumberException
{
if(studentNumber < 0 || studentNumber > name.length-1)
throw new InvalidStudentNumberException(0,(name.length-1));
name[studentNumber] = studentName;
}
//Sets the test score of the specified test
//accepts test number (>=1, <=4)
//student number (>=0, <=class size)
//and score (>=0, <=100)
public void setTestScore(int testNumber, int studentNumber, double score) throws InvalidStudentNumberException, InvalidTestScoreException, InvalidTestNumberException
{
if(studentNumber < 0 || studentNumber > name.length-1)
throw new InvalidStudentNumberException(0,(name.length-1));
if(score < 0 || score > 100)
throw new InvalidTestScoreException();
if(testNumber < 0 || testNumber > tests.length-1)
throw new InvalidTestNumberException(1,(tests.length-1));
tests[testNumber][studentNumber] = score;
}
//Returns the students name, accepts student number (>=0, <=class size)
public String getName(int studentNumber) throws InvalidStudentNumberException
{
if(studentNumber < 0 || studentNumber > name.length-1)
throw new InvalidStudentNumberException(0,(name.length-1));
return name[studentNumber];
}
//Returns average test score for given student, accepts student number (>=0, <=class size)
//Used in getLetterGrade
public double getAverage(int studentNumber) throws InvalidStudentNumberException
{
if(studentNumber < 0 || studentNumber > name.length-1)
throw new InvalidStudentNumberException(0,(name.length-1));
double average = 0;
for(int n=0;n<tests.length;n++)
average += tests[n][studentNumber];
average = average/tests.length;
return average;
}
//Returns letter grade for given student, accepts student number (>=0, <=class size)
//Uses getAverage to work out letter grade
public char getLetterGrade(int studentNumber) throws InvalidStudentNumberException
{
if(studentNumber < 0 || studentNumber > name.length-1)
throw new InvalidStudentNumberException(0,(name.length-1));
double average = getAverage(studentNumber);
if(average >= 85)
grade[studentNumber] = 'A';
else if(average >= 75)
grade[studentNumber] = 'B';
else if(average >= 65)
grade[studentNumber] = 'C';
else if(average >= 50)
grade[studentNumber] = 'D';
else
grade[studentNumber] = 'F';
return grade[studentNumber];
}
public String toString()
{
//DecimalFormat for displaying average score as 2 digit number with only 1 decimal place
DecimalFormat output = new DecimalFormat("00.0");
StringBuilder line;
System.out.println();
for(int i=0; i<name.length; i++)
{
try{
line = new StringBuilder();
//Build line
line.insert(0,"Name: ");
line.append(getName(i));
while(line.length()<40)
line.append(" ");
line.append("Average: ");
line.append(output.format(getAverage(i)));
line.append(" Grade: ");
line.append(getLetterGrade(i));
//Display line
System.out.println(line);
}
catch(InvalidStudentNumberException e)
{
//Display end of list when finished
}
}//End for loop
return "End of List ";
}
//End class GradeBook
}
StudentInfo.txt
Barry Hall
100.0
100.0
100.0
99.9
Fred Hall
85
85
85
85
Jane Hall
75
75
75
75
Jessica Hall
65
65
65
65
The nameless one
55
55
55
55
InvalidStudentNumberException.java
//InvalidStudentNumberException
//Custom exception to be thrown when
//Student number < 0 or greater than declared class size is given
public class InvalidStudentNumberException extends Exception
{
//No arg constructor
public InvalidStudentNumberException()
{
super("Error: Invalid Student Number");
}
//The following constructor accepts the min and max numbers as an input
public InvalidStudentNumberException(int min, int max)
{
super("Error: Student number between " + min + " and " + max + " expected");
}
//End class InvalidStudentNumberException
}
InvalidTestNumberException.java
//InvalidTestNumberException
//Custom exception to be thrown when
//test number is outside the range 1 to 4 inclusive
public class InvalidTestNumberException extends Exception
{
//No arg constructor
public InvalidTestNumberException()
{
super("Error: Invalid Test Number");
}
//No arg constructor
public InvalidTestNumberException(int min, int max)
{
super("Error: Invalid Test Number");
}
//End class InvalidTestNumberException
}
InvalidTestScoreException.java
//InvalidTestScoreException
//Custom exception to be thrown when
//Test score is outside the range 0 to 100 inclusive
public class InvalidTestScoreException extends Exception
{
//No arg constructor
public InvalidTestScoreException()
{
super("Error: Test scores must be between 0 and 100");
}
//End class InvalidTestScoreException
}
Check.java
public class Check
{
//Compares an inputInt to two other ints
public static boolean isInts(String error, int inputInt, int typeInt1, int typeInt2)
{
if ((inputInt == typeInt1) || (inputInt == typeInt2))
return false;
else
{
if (error.equals("v"))
System.out.println("Error: " + typeInt1 + " or " + typeInt2 + " expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Compares an inputInt to three other ints
public static boolean isInts(String error, int inputInt, int typeInt1, int typeInt2, int typeInt3)
{
if ((inputInt == typeInt1) || (inputInt == typeInt2) || (inputInt == typeInt3))
return false;
else
{
if (error.equals("v"))
System.out.println("Error: " + typeInt1 + ", " + typeInt2 + " or " + typeInt3 + " expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Compares an inputInt to four other ints
public static boolean isInts(String error, int inputInt, int typeInt1, int typeInt2, int typeInt3, int typeInt4)
{
if ((inputInt == typeInt1) || (inputInt == typeInt2) || (inputInt == typeInt3) || (inputInt == typeInt4))
return false;
else
{
if (error.equals("v"))
System.out.println("Error: " + typeInt1 + ", " + typeInt2 + ", " + typeInt3 + " or " + typeInt4 +" expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Compares an inputInt to five other ints
public static boolean isInts(String error, int inputInt, int typeInt1, int typeInt2, int typeInt3, int typeInt4, int typeInt5)
{
if ((inputInt == typeInt1) || (inputInt == typeInt2) || (inputInt == typeInt3) || (inputInt == typeInt4) || (inputInt == typeInt5))
return false;
else
{
if (error.equals("v"))
System.out.println("Error: " + typeInt1 + ", " + typeInt2 + ", " + typeInt3 + ", " + typeInt4 + " or " + typeInt5 + " expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Tests if inputInt is greater than another int
public static boolean isGreaterThan(String error, int inputInt, int typeInt1)
{
if (inputInt > typeInt1)
return false;
else
{
if (error.equals("v"))
System.out.println("Error: Integer greater than " + typeInt1 + " expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Tests if inputDouble is greater than another double
public static boolean isGreaterThan(String error, double inputDouble, double typeDouble1)
{
if (inputDouble > typeDouble1)
return false;
else
{
if (error.equals("v"))
System.out.println("Error: Number greater than " + typeDouble1 + " expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Tests if inputInt between ( >= X and <= Y) two other ints
public static boolean isBetween(String error, int inputInt, int typeInt1, int typeInt2)
{
if ((inputInt >= typeInt1) && (inputInt <= typeInt2))
return false;
else
{
if (error.equals("v"))
System.out.println("Error: Integer between " + typeInt1 + " and " + typeInt2 + " expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Compares inputString to two other strings
public static boolean isStrings(String error, String inputString, String typeString1, String typeString2)
{
if (inputString.equals(typeString1) || inputString.equals(typeString2))
return false;
else
{
if (error.equals("v"))
System.out.println("Error: " + typeString1 + " or " + typeString2 + " expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Compares inputString to three other strings
public static boolean isStrings(String error, String inputString, String typeString1, String typeString2, String typeString3)
{
if (inputString.equals(typeString1) || inputString.equals(typeString2) || inputString.equals(typeString3))
return false;
else
{
if (error.equals("v"))
System.out.println("Error: " + typeString1 + ", " + typeString2 + " or " + typeString3 + " expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Compares inputString to four other strings
public static boolean isStrings(String error, String inputString, String typeString1, String typeString2, String typeString3, String typeString4)
{
if (inputString.equals(typeString1) || inputString.equals(typeString2) || inputString.equals(typeString3) || inputString.equals(typeString4))
return false;
else
{
if (error.equals("v"))
System.out.println("Error: " + typeString1 + ", " + typeString2 + ", " + typeString3 + " or " + typeString4 + " expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Compares inputString to five other strings
public static boolean isStrings(String error, String inputString, String typeString1, String typeString2, String typeString3, String typeString4, String typeString5)
{
if (inputString.equals(typeString1) || inputString.equals(typeString2) || inputString.equals(typeString3) || inputString.equals(typeString4) || inputString.equals(typeString5))
return false;
else
{
if (error.equals("v"))
System.out.println("Error: " + typeString1 + ", " + typeString2 + ", " + typeString3 + ", " + typeString4 + " or " + typeString5 + " expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Takes a strings and checks if it's "y" "n" "yes" or "no" ignoring case
public static boolean isYesNo(String error, String inputString)
{
if (inputString.equalsIgnoreCase("y") || inputString.equalsIgnoreCase("n") || inputString.equalsIgnoreCase("yes") || inputString.equalsIgnoreCase("no"))
return false;
else
{
if (error.equals("v"))
System.out.println("Error: Yes or No answer expected");
else if (!(error.equals("v")) && !(error.equals("q")))
System.out.println(error);
return true;
}
}
//Takes a string and checks if the answer is "y" or "yes" ignoring case
public static boolean isYes(String typeString)
{
if (typeString.equalsIgnoreCase("y") || typeString.equalsIgnoreCase("yes"))
return true;
else
return false;
}
}//End of class InputChecker
sample output
Welcome to the GradeBook demo
Do you want to use default settings [y,n]? y
Name: Barry Hall Average: 100.0 Grade: A
Name: Fred Hall Average: 85.0 Grade: A
Name: Jane Hall Average: 75.0 Grade: B
Name: Jessica Hall Average: 65.0 Grade: C
Name: The nameless one Average: 55.0 Grade: D
End of List
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.