Java Driver License Program - Having trouble displaying which problems I missed -
i created program according instructions below. when run program, output incorrect answers missed incorrect. example, if input correct answers #19, 1 or 2 in incorrect answers section of output data. did wrong?
the local driver's license office has asked write program grades written portion of license exam. exam has 20 multiple choice questions. here correct answers:
1. b 2. d 3. 4. 5. c 6. 7. b 8. 9. c 10. d 11.b 12. c 13. d 14. 15. d 16. c 17. c 18. b 19. d 20.
a student must correctly answer 15 questions of 20 questions pass exam.
write class named driverexam holds correct answers exam in array field. class should have array field hold student's answers. class should have following methods:
passed. method returns true if student passed exam, false otherwise totalcorrect: returns total number of correctly answered questions totalincorrect: returns total number of incorrectly answered questions questionsmissed: int array containing question numbers of question student missed demonstrate class in test program asks user enter student's answers, , display results returned driverexam class's methods.
input validation: accept letters a, b, c, or d answers
here output data:
driver's license exam 20 multiple-choice questions mark a, b, c, d 1: b 2: d 3: 4: 5: c 6: 7: b 8: 9: c 10: d 11: b 12: c 13: d 14: 15: d 16: c 17: c 18: b 19: d 20: c results total correct: 19 total incorrect: 1 passed: yes incorrect answers are: 2
public class driverexam { //an array containing student's answers private string[] correctanswers = {"b", "d", "a", "a", "c", "a", "b", "a", "c", "d", "b", "c", "d", "a", "d", "c", "c", "b", "d", "a"}; //store user's answers private string[] useranswers; int[] missed = new int[correctanswers.length]; //process user's answers public driverexam (string[] answers) { useranswers = new string[answers.length]; (int = 0; < answers.length; i++) { useranswers[i] = answers[i]; } } //returns boolean value if correct answers > 15 public boolean passed() { if (totalcorrect() >= 15) return true; else return false; } //determines total correct answers public int totalcorrect() { int correctcount = 0; (int = 0; < correctanswers.length; i++) { if (useranswers[i].equalsignorecase(correctanswers[i])) { correctcount++; } } return correctcount; } //determines total incorrect answers public int totalincorrect() { int incorrectcount = 0; (int = 0; < correctanswers.length; i++) { if (!useranswers[i].equalsignorecase(correctanswers[i])) { missed[incorrectcount] = 1; incorrectcount++; } } return incorrectcount; } //missed questions public int[] questionsmissed() { return missed; } } //end of driverexam class ---------------------------------------------------------------------------- import java.util.scanner; public class driverexamapplication { public static void main(string[] args) { system.out.println(" driver's license exam "); scanner input = new scanner(system.in); system.out.println(" 20 multiple-choice questions "); system.out.println(" mark a, b, c, d "); //inputting string string[] answers = new string[20]; string answer; (int = 0; < 20; i++) { { system.out.print((i+1) + ": "); answer = input.nextline(); } while (!isvalidanswer(answer)); answers[i] = answer; } //process driverexam exam = new driverexam(answers); //results 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; (int = 0; < exam.totalincorrect(); i++) { missedindex = exam.questionsmissed()[i]+1; system.out.print(" " + missedindex); } } } //end of main function //returns true when answer valid public static boolean isvalidanswer (string answer) { return "a".equalsignorecase(answer) || "b".equalsignorecase(answer) || "c".equalsignorecase(answer) || "d".equalsignorecase(answer); } } //end of test class
the problem in function totalincorrect
assign 1
instead of i
. should work:
//determines total incorrect answers public int totalincorrect() { int incorrectcount = 0; (int = 0; < correctanswers.length; i++) { if (!useranswers[i].equalsignorecase(correctanswers[i])) { missed[incorrectcount] = i; incorrectcount++; } } return incorrectcount; }
Comments
Post a Comment