Create a java application that grades a multiple choice quiz. This is the instru
ID: 3810430 • Letter: C
Question
Create a java application that grades a multiple choice quiz.
This is the instructions given by my professor:
The application will do the following:
Prompt the user to enter the number of questions for the quiz, then prompt the user to enter the answer key for each question. Use an array.
Create an array of Students that will contain the student’s name and grade on the quiz.
From a file, read each student’s name and quiz answers.
By comparing the entered answers to the answer key, the program can determine if the answer is correct or incorrect. Count the correct answers and store the final score for each student.
Display a report of students' grades, the highest grade, the lowest grade, and the average score for the class.
Here is my code
import java.util.Scanner;
import java.io.*;
public class Quiz
{
public static void main(String[] args) throws IOException
{
Scanner fileScan, lineScan;
String fileName, line;
int numQuest, numStudents;
int i;
int max = 0, min = 0;
int sum = 0;
Scanner scan = new Scanner(System.in);
//Getting key array size
System.out.print("Enter the number of questions on your quiz: ");
numQuest = scan.nextInt();
System.out.println("enter your key");
//getting answer key
String[] key = new String[numQuest];
for(i=0; i {
int j=i+1;
System.out.println("What is the answer for question number " + j + "?");
key[i] = scan.next();
}
System.out.print("Enter the number of students that took the quiz: ");
numStudents = scan.nextInt();
int[] studentAns = new int[numStudents];
String []studentName = new String [numStudents];
//retrieve student data
System.out.print ("Enter the name of the input file: ");
fileName = scan.next();
fileScan = new Scanner(new File(fileName));
String[] a=new String[numStudents+1];
//cA = correct answer
int cA= 6;
while (fileScan.hasNext())
{ //opening fileScan
line = fileScan.next();
lineScan = new Scanner(line);
lineScan.useDelimiter(",");
studentName[cA] = a [0];
studentAns[cA]=0;
for(int ik=0;ik {
if(key[ik].equalsIgnoreCase(a[cA+1]))
{
studentAns[cA]++;
}
}
cA ++;
}//Closing fileScan
System.out.println("Name and score for each student:");
System.out.println("********************************");
for (i = 0; i {
System.out.print(studentName[i] + " ");
System.out.println(studentAns[i] );
}
for (i = 0; i {
if(studentAns[i] > studentAns[max])
max = i;
if(studentAns[i] > studentAns[min])
min = i;
sum += studentAns[i];
}
System.out.println("The maximum score is a " + studentAns[max] + "from Mr./Ms. " + studentName[max]);
System.out.println("The maximum score is a " + studentAns[min] + "from Mr./Ms. " + studentName[min]);
System.out.println("The average score is: " + (sum / numStudents));
fileScan.close();
scan.close();
}
}
Here is my data file
John, Brocksmith, A,B,C,E,A,C,A,B,A,D
Thomas, Richey, B,D,C,A,C,A,C,C,D,B
Jeffrey, Cargill,D,C,D,A,B,A,C,A,D,A
Kendall Ziller, D,D,B,C,B,B,D,A,B,C
Brittney, Nowak, C,C,B,C,D,A,C,D,C,B
Jovita, Moore, D,C,A,A,A,C,D,C,D,A
My issue is that i keep getting a "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at Quiz.main(Quiz.java:54)" error after entering the name of my input file.
How would I get around this issue?
Explanation / Answer
You have initialized cA to 6;
int cA = 6;
No. of students in the given file is 6;
So, size of studentAns[] and studentName[] is 6.
Calling studentAns[6] and studentName[6] is giving ArrayOutOfBoundException as till studentAns[5] is only allowed.
Solution:::
Initialize it to 0;
int cA = 0;
If other parts are correct, the it should work
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.