here is the error message:
Exception in thread "main" java.lang.NullPointerException
at DriverClass.DriverExam.questionsMissed(DriverExam.java:48)
at Chapter7ALabDemo.main(Chapter7ALabDemo.java:33)
program is to read a answer key txt and a student's answers txt, then the number of questions would be entered to correspond with the txt file.
and here is my code in two files:
import java.util.Scanner;
import java.io.*;
import DriverClass.DriverExam;
public class Chapter7ALabDemo
{
public static void main (String [] args) throws IOException
{
File file;
Scanner readKey;
Scanner readAnswers;
String str;
int numQuestions;
DriverExam student;
int [] missedQuestions;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the name of the file that is the test key ");
str = keyboard.nextLine();
file = new File(str);
readKey = new Scanner(file);
System.out.println("Enter the name of the file with the student answers.");
str = keyboard.nextLine();
file = new File(str);
readAnswers = new Scanner(file);
System.out.println("How many test questions are there?");
numQuestions = keyboard.nextInt();
student = new DriverExam(readKey, readAnswers, numQuestions);
missedQuestions = student.questionsMissed();
System.out.println(student);
if (student.passed())
System.out.println("The student passed.");
else
System.out.println("The student did not pass.");
System.out.println("The student got " + student.totalCorrect() + " answers correct.");
System.out.println("The student got " + student.totalIncorrect() + " answers incorrect.");
System.out.println("The following questions were missed by the student: ");
student.printMissed(missedQuestions);
}
}
package DriverClass;
import java.util.Scanner;
public class DriverExam
{
private char[] key;
private char[] answers;
//constructor that instantiates the arrays to a given size and stores data read from
// a file into the answer key array and stores data read from another file into
// the student answer array
public DriverExam(Scanner readKey, Scanner readAnswers, int numQuestions)
{
}
// “grades the test“ and returns the total number of correct answers
public int totalCorrect()
{
int correct = 0;
for (int i = 0; i < key.length; i++)
{
if (key[i] == answers[i])
correct++;
}
return correct;
}
//that returns the total number of incorrect answers
public int totalIncorrect()
{
int tmissed = 0;
tmissed = key.length - totalCorrect();
return tmissed;
}
//returns true if the student passed the exam and false if the student failed the exam
//the student needs to get 75% of the questions correct in order to pass
public boolean passed ()
{
double percentage = 0.75;
for(int i = 0; i < key.length ;i++)
{
if (totalCorrect() > percentage * answers[i])
return true;
}
return false;
}
//instantiates an integer array that contains the question numbers missed by the student.
//the number of elements in the array should be the number of questions missed.
public int[] questionsMissed()
{
int size = key.length - totalCorrect();
int[] missed = {};
if (size < 1)
return missed;
else
missed = new int [size];
int pos = 0;
for (int i = 0; i < key.length; i++)
{
if (key[i] != answers[i])
{
missed[pos] = (i +1);
pos = pos + 1;
}
}
return missed;
}
//displays the contents of the array instantiated in questionsMissed
public void printMissed(int[] missedQuestions)
{
System.out.println(questionsMissed());
}
//displays the correct answers and the student answers stored in the arrays referenced
//by the class fields — the output should be labelled
public String toString()
{
String str = "The correct answers are: " + key.toString() + " and the student answers were: " + answers.toString() ;
return str;
}
}