Problem creating a new object
Help! Cannot figure out what is wrong with my code.
Code :
<ExamDemo.java:25: cannot find symbol
symbol : constructor DriverExam(java.lang.String[])
location: class DriverExam
DriverExam exam = new DriverExam(answers);
^
1 error>
Code :
< import java.util.Scanner;
// Input Validation:
public class ExamDemo
{
public static void main(String[] args)
{
System.out.println(" Driver's Liscense Exam ");
Scanner kb = new Scanner(System.in);
System.out.println(" 20 multiple choise questions Mark A,B,C,D ");
// Inputting string
String[] answers = new String[20];
String answer;
for (int i = 0; i < 20; i++)
{
do
{
System.out.print((i + 1) + ": ");
answer = kb.nextLine();
}
while (!isValidAnswer(answer));
answers[i] = answer;
}
// Process
DriverExam exam = new DriverExam(answers);
// Result
System.out.println(" Results ");
// outputting Total correct
System.out.println("Total Correct: " + exam.totalCorrect());
// outputting Total incorrect
System.out.println("Total Incorrect: " + exam.totalIncorrect());
String passed = exam.passed() ? "YES" : "NO";
//result pass or fail
System.out.println("Passed: " + passed);
if (exam.totalIncorrect() > 0)
{
System.out.println("The incorrect answers are:");
int missedIndex;
for (int i = 0; i < exam.totalIncorrect(); i++)
{
missedIndex = exam.questionsMissed()[i] + 1;
System.out.print(" " + missedIndex);
}
}
}
public static boolean isValidAnswer (String answer)
{
return "A".equalsIgnoreCase(answer) || "B".equalsIgnoreCase(answer) || "C".equalsIgnoreCase(answer) || "D".equalsIgnoreCase(answer) ;
}
}>
Code :
< import java.util.Scanner;
class DriverExam
{
//an array of student's answers.
private char[] correctAnswers = {
'B','D','A','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};
// Store the user answers.
private char[] studentAnswers;
int[] missed = new int[correctAnswers.length];
//Process the user answers
public DriverExam (char[] Answers)
{
studentAnswers = new char[Answers.length];
for (int i = 0; i < Answers.length; i++)
{
studentAnswers[i] = Answers[i];
}
}
//returns boolean value if correct answers > 15
public boolean passed()
{
if(totalCorrect() >= 15)
return true;
else
return false;
}
//totalCorrect answers
public int totalCorrect()
{
int correctCount = 0;
for (int i = 0; i < correctAnswers.length; i++)
{
if (studentAnswers[i] == (correctAnswers[i]))
{
correctCount++;
}
}
return correctCount;
}
// totalIncorrect answered.
public int totalIncorrect()
{
int incorrectCount = 0;
for (int i = 0; i < correctAnswers.length; i++)
{
if (studentAnswers[i] != (correctAnswers[i]))
{
missed[incorrectCount] = i;
incorrectCount++;
}
}
return incorrectCount;
}
//Missed questions.
public int[] questionsMissed()
{
return missed;
}
}
//end driver exam class
>
Also, keep getting java.lang.ArrayIndexOutOfBoundsException: 20 in my interactions tab when trying to pass e.passed()
Re: Problem creating a new object
Hello smithmar!
I haven't tested your code but i think the error occurs because the DriverExam constructor you have needs a char array as an argument and in your ExamDemo class when you try to create a new instance of DriverExam you pass it a String array (answers).