Question

Hello, can someone please fix my code? It runs perfect but I'm having trouble with the...

Hello, can someone please fix my code? It runs perfect but I'm having trouble with the index for the incorrect answer array; please run some tests to ensure the code runs properly, thank you! Here is the code:

___________________________________________________________________________________________________________________________

import java.util.Scanner;

public class DriverTest{

   final static int QuestionTotal = 10;

   public static void main(String[] args){  

       // scanner and answer key
       Scanner scan = new Scanner(System.in);
       final char[] answers = {'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B'};  
       final char[] studentAnswers = new char[10];

       // general prompt
       System.out.println();
       System.out.println("You are taking an exam that has 10 questions (using capital letters A, B, C, D).");
       System.out.println("You need at least 5 correct answers to pass. Enter your answers below: ");

       // user input prompt (returns char input starting at 0 index)
       for(int i = 0; i < studentAnswers.length; i++){  
           System.out.print("Question " + (i+1) + ": ");
           String userInput = scan.nextLine();
           studentAnswers[i] = userInput.charAt(0);
       }


       int incorrect = correctAnswers(answers, studentAnswers);
       int[] incorrectAnswerArray = new int[incorrect];
       int correct = QuestionTotal - incorrect;
       incorrectAnswerArray = missedAnswers(answers, studentAnswers, incorrect);

   // *************************************************************************

       // final results
       System.out.print("** Correct answers: ");
       System.out.println(correct);
       System.out.print("** Incorrect answers: ");
       System.out.println(incorrect);

       if(correct >= 5){
           System.out.println("** You passed the exam.");
       } else
           System.out.println("** You failed the exam!");

       if(correct == 10){
           System.out.println("** Perfect score!");
       } else
           System.out.println("You missed the following questions: ");
      
       // printing our array of incorrect answers
       for(int i = 0; i < incorrectAnswerArray.length; i++) {
           System.out.print(incorrectAnswerArray[i] + " ");
       }
      
           System.out.println();  
   }

   // *************************************************************************

   public static int correctAnswers(char[] answers, char[] studentAnswers){

       // counts correct answers; increments if it matches answers
       int correctCounter = 0;

       for (int i = 0; i < answers.length; i++){
           if(studentAnswers[i] == (answers[i])){
               correctCounter++;
           }
       }

       //calculating incorrect answers
       int incorrectAnswers = QuestionTotal - correctCounter;
           return incorrectAnswers;
   }

   // *************************************************************************

   public static int[] missedAnswers(char[] answers, char[] studentAnswers, int incorrect){

       // creates array of missed answers
       int[] missed = new int[incorrect];
       int incorrectCounter = 0;

       for (int i = 0; i < answers.length; i++) {
           if (studentAnswers[i] != answers[i]){
               missed[incorrectCounter] = i;
               incorrectCounter++;
           }
       }

       return missed;
   }      
}


0 0
Add a comment Improve this question Transcribed image text
Answer #1

CORRECTED PROGRAM

import java.util.Scanner;

public class DriverTest{

   final static int QuestionTotal = 10;

   public static void main(String[] args){

       // scanner and answer key
       Scanner scan = new Scanner(System.in);
       final char[] answers = {'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B'};
       final char[] studentAnswers = new char[10];

       // general prompt
       System.out.println();
       System.out.println("You are taking an exam that has 10 questions (using capital letters A, B, C, D).");
       System.out.println("You need at least 5 correct answers to pass. Enter your answers below: ");

       // user input prompt (returns char input starting at 0 index)
       for(int i = 0; i < studentAnswers.length; i++){
           System.out.print("Question " + (i+1) + ": ");
           String userInput = scan.nextLine();
           studentAnswers[i] = userInput.charAt(0);
       }


       int incorrect = correctAnswers(answers, studentAnswers);
       int[] incorrectAnswerArray = new int[incorrect];
       int correct = QuestionTotal - incorrect;
       incorrectAnswerArray = missedAnswers(answers, studentAnswers, incorrect);

   // *************************************************************************

       // final results
       System.out.print("** Correct answers: ");
       System.out.println(correct);
       System.out.print("** Incorrect answers: ");
       System.out.println(incorrect);

       if(correct >= 5){
           System.out.println("** You passed the exam.");
       } else
           System.out.println("** You failed the exam!");

       if(correct == 10){
           System.out.println("** Perfect score!");
       } else
           System.out.println("You missed the following questions: ");
    
       // printing our array of incorrect answers
       for(int i = 0; i <incorrectAnswerArray.length; i++) {
           System.out.print(incorrectAnswerArray[i] + " ");
       }
    
           System.out.println();
   }

   // *************************************************************************

   public static int correctAnswers(char[] answers, char[] studentAnswers){

       // counts correct answers; increments if it matches answers
       int correctCounter = 0;

       for (int i = 0; i < answers.length; i++){
           if(studentAnswers[i] == (answers[i])){
               correctCounter++;
           }
       }

       //calculating incorrect answers
       int incorrectAnswers = QuestionTotal - correctCounter;
           return incorrectAnswers;
   }

   // *************************************************************************

   public static int[] missedAnswers(char[] answers, char[] studentAnswers, int incorrect){

       // creates array of missed answers
       int[] missed = new int[incorrect];
       int incorrectCounter = 0;

       for (int i = 0; i < answers.length; i++) {
           if (studentAnswers[i] != answers[i]){
               missed[incorrectCounter] = (i+1);//Correction here
               incorrectCounter++;
           }
       }

       return missed;
   }    
}


Output

You are taking an exam that has 10 questions (using capital letters A, B, C, D).
You need at least 5 correct answers to pass. Enter your answers below:
Question 1: B
Question 2: B
Question 3: C
Question 4: D
Question 5: B
Question 6: B
Question 7: C
Question 8: D
Question 9: B
Question 10: B
** Correct answers: 7
** Incorrect answers: 3
** You passed the exam.
You missed the following questions:
1 5 9

Question 1: A
Question 2: B
Question 3: C
Question 4: D
Question 5: A
Question 6: B
Question 7: C
Question 8: D
Question 9: C
Question 10: D
** Correct answers: 8
** Incorrect answers: 2
** You passed the exam.
You missed the following questions:
9 10

Add a comment
Know the answer?
Add Answer to:
Hello, can someone please fix my code? It runs perfect but I'm having trouble with the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm also having trouble on dividing the code into methods. This is my work done so...

    I'm also having trouble on dividing the code into methods. This is my work done so far and net beans give me a lot of errors. Help! package caesarcipher; import textio.TextIO; /** * * @author */ public class CaesarCipher { enum code {encodeText, decodeText }; /** * @param args the command line arguments */ public static void main(String[] args) {    String code; char x;    int totalcount = 0;    while(true){ TextIO.put("Type in an E to encode a message...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • Hi All, Can someone please help me correct the selection sort method in my program. the...

    Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...

  • Can someone fix the program below so it does what the picture says it won't work...

    Can someone fix the program below so it does what the picture says it won't work for me. import java.util.Scanner; public class Userpass { static String arr[]; static int i = 0; public static void main(String[] args) { String username, password; int tries = 0, result; do { System.out.print("Enter the username: "); username = readUserInput(); System.out.print("Enter the password: "); password = readUserInput(); result = verifyCredentials(username, password); tries++; if (result == -1) System.out.println("The username is incorrect!\n"); else if (result == -2)...

  • The files provided contain syntax and/or logic errors. In each case, determine and fix the problem,...

    The files provided contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. import java.util.*; public class DebugEight1 { public static void main(String args[]) { Scanner input = new Scanner(System.in); char userCode; String entry, message; boolean found = false; char[] okayCodes = {'A''C''T''H'}; StringBuffer prompt = new StringBuffer("Enter shipping code for this delivery\nValid codes are: "); for(int x = 0; x <...

  • *JAVA* Can somebody take a look at my current challenge? I need to throw a different...

    *JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> {    private int top, size;    private E arrS[];    private static final int MAX_STACK_SIZE = 10;    public ArrayStack() {        this.arrS = (E[]) new Object[MAX_STACK_SIZE];        this.top = size;        this.size = 0;...

  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT