I'm writing this program that is a drivers license test . the main method allows the user to input 20 answers and it creates an array of those answers then it is suppose to make that a new instance of a class that checks the array of the users answers against an array of the correct answers and return true or false to tell if the user passed then I need a method to return the number of correct answers and a method to return the number of incorrect answers. My issue is that my methods for correct and incorrect are always returning 0 and the Boolean method I always false. Thanks
public class DriverExam2 { private String[] correctAnswers={"b","d","a","a","c","a","b","a","c","d","b","c","d","a","d","c","c","b","d","a"}; private String[] userAnswers=new String[20]; boolean passed; int numberCorrect; int numberIncorrect; //constructer to set userAnswers public DriverExam2(String[] entered) { userAnswers=entered; } //accessor method to return userAnswers public String[] getUserAnswers() { return userAnswers; } //mutator method to assign count and pass fail public void setInfo() { boolean pass=false; int correctCount=0; int incorrectCount=0; for(int i=0;i<userAnswers.length;i++) { if(userAnswers[i].equals(correctAnswers[i])) { correctCount++; } else { incorrectCount++; } } if(correctCount>14) {pass=true;} passed=pass; numberCorrect=correctCount; numberIncorrect=incorrectCount; } //accessor methods to return if you passed,the correct count, and the incorrect count. public boolean getPassFail() { return passed; } public int getTotalCorrect() { return numberCorrect; } public int getTotalIncorrect() { return numberIncorrect; } }
import java.util.Scanner; public class DriverExamDemo2 { public static void main(String[] args) { String[] userAnswers=new String[20]; String answer; System.out.println("please enter the testee's answers as the correspond with the question number"); for(int i=0;i<userAnswers.length;i++) { Scanner input=new Scanner(System.in); System.out.println("Q."+(i+1)); answer=input.next(); userAnswers[i]=answer; } // constructor to send the users answers DriverExam2 exam1 = new DriverExam2(userAnswers); //call the accessor methods for total correct total incorrect and System.out.println("the number of questions you answered correctly was "+exam1.getTotalCorrect()); System.out.println("the number of questions you answered incorrectly was "+exam1.getTotalIncorrect()); //get the boolean return from pass fail method and print out accordingly to wether it is true or false if(exam1.getPassFail()) System.out.println("you passed your driving exam"); else System.out.println("you failed your driving exam"); } }