i have written a java code but i need the results to be disp

i have written a java code but i need the results to be displayed in a method called displayResults, how do i do it?

import java.util.Scanner;

public class DriverExamExercise
{
static final Scanner console = new Scanner(System.in);
static char[] officialAnswers={\'B\',\'D\',\'A\',\'A\',\'C\',\'A\',\'B\',\'A\',\'C\',\'D\',\'B\',\'C\',\'D\',\'A\',\'D\',\'C\',\'C\',\'B\',\'D\',\'A\'};
static char[] getStudentAnswer=new char[20];
public static void main(String[] args)
{
int counter=0;

String answer;

int[] wrong;

do{
System.out.print(\" enter the answer to question #\"+(counter+1)+\": \");
answer=console.nextLine().toUpperCase();
if(answer.charAt(0)!=\'A\'&&answer.charAt(0)!=\'B\'&&answer.charAt(0)!=\'C\'&&answer.charAt(0)!=\'D\')
{
System.out.println(\"Invalid input, please try again\");
}
else
{
getStudentAnswer[counter]=answer.charAt(0);
++counter;
}
}while(counter<getStudentAnswer.length);
if(passed())
{
System.out.println(\" Success! you have passed :) \");
}
else
{
System.out.println(\"Sorry, you failed :(\");
}
System.out.println(\"You got \"+totalCorrect()+\" right\");
System.out.println(\"You got \"+totalIncorrect()+\" wrong\");
wrong=questionsMissed();
for(int count=0;count<wrong.length;++count)
{
System.out.println(\"You got #\"+wrong[count]+\" wrong\");
}

}
public static boolean passed()
{
int correct=totalCorrect();
if(correct>=15)
{
return true;
}
else
{
return false;
}
}
public static int totalCorrect()
{
int correct=0;
for(int counter=0;counter<officialAnswers.length;++counter)
{
if(officialAnswers[counter]==getStudentAnswer[counter])
{
++correct;
}
}
return correct;
}
public static int totalIncorrect()
{
int incorrect=0;
for(int counter=0;counter<officialAnswers.length;++counter)
{
if(officialAnswers[counter]!=getStudentAnswer[counter])
{
++incorrect;
}
}
return incorrect;
}
public static int[] questionsMissed()
{
int[] missed=new int[totalIncorrect()];
int count=0;
for(int counter=0;counter<officialAnswers.length;++counter)
{
if(officialAnswers[counter]!=getStudentAnswer[counter])
{
missed[count]=counter+1;
++ count;
}
}
return missed;
}
}

Solution

Hi, I have added required method.

Please let me know in case of any issue.

import java.util.Scanner;

public class DriverExamExercise

{

   static final Scanner console = new Scanner(System.in);

   static char[] officialAnswers={\'B\',\'D\',\'A\',\'A\',\'C\',\'A\',\'B\',\'A\',\'C\',\'D\',\'B\',\'C\',\'D\',\'A\',\'D\',\'C\',\'C\',\'B\',\'D\',\'A\'};

   static char[] getStudentAnswer=new char[20];

   public static void main(String[] args)

   {

       int counter=0;

       String answer;

       do{

           System.out.print(\" enter the answer to question #\"+(counter+1)+\": \");

           answer=console.nextLine().toUpperCase();

           if(answer.charAt(0)!=\'A\'&&answer.charAt(0)!=\'B\'&&answer.charAt(0)!=\'C\'&&answer.charAt(0)!=\'D\')

           {

               System.out.println(\"Invalid input, please try again\");

           }

           else

           {

               getStudentAnswer[counter]=answer.charAt(0);

               ++counter;

           }

       }while(counter<getStudentAnswer.length);

       displayResults();

   }

   public static boolean passed()

   {

       int correct=totalCorrect();

       if(correct>=15)

       {

           return true;

       }

       else

       {

           return false;

       }

   }

   public static int totalCorrect()

   {

       int correct=0;

       for(int counter=0;counter<officialAnswers.length;++counter)

       {

           if(officialAnswers[counter]==getStudentAnswer[counter])

           {

               ++correct;

           }

       }

       return correct;

   }

   public static int totalIncorrect()

   {

       int incorrect=0;

       for(int counter=0;counter<officialAnswers.length;++counter)

       {

           if(officialAnswers[counter]!=getStudentAnswer[counter])

           {

               ++incorrect;

           }

       }

       return incorrect;

   }

   public static int[] questionsMissed()

   {

       int[] missed=new int[totalIncorrect()];

       int count=0;

       for(int counter=0;counter<officialAnswers.length;++counter)

       {

           if(officialAnswers[counter]!=getStudentAnswer[counter])

           {

               missed[count]=counter+1;

               ++ count;

           }

       }

       return missed;

   }

   public static void displayResults(){

       if(passed())

       {

           System.out.println(\" Success! you have passed :) \");

       }

       else

       {

           System.out.println(\"Sorry, you failed :(\");

       }

       System.out.println(\"You got \"+totalCorrect()+\" right\");

       System.out.println(\"You got \"+totalIncorrect()+\" wrong\");

       int[] wrong =questionsMissed();

       for(int count=0;count<wrong.length;++count)

       {

           System.out.println(\"You got #\"+wrong[count]+\" wrong\");

       }

   }

}

/*

Sample run:

enter the answer to question #1: 3

Invalid input, please try again

enter the answer to question #1: A

enter the answer to question #2: B

enter the answer to question #3: C

enter the answer to question #4: D

enter the answer to question #5: E

Invalid input, please try again

enter the answer to question #5: F

Invalid input, please try again

enter the answer to question #5: G

Invalid input, please try again

enter the answer to question #5: A

enter the answer to question #6: B

enter the answer to question #7: B

enter the answer to question #8: C

enter the answer to question #9: A

enter the answer to question #10: D

enter the answer to question #11: D

enter the answer to question #12: D

enter the answer to question #13: A

enter the answer to question #14: V

Invalid input, please try again

enter the answer to question #14: C

enter the answer to question #15: B

enter the answer to question #16: B

enter the answer to question #17: C

enter the answer to question #18: A

enter the answer to question #19: A

enter the answer to question #20: D

Sorry, you failed :(

You got 3 right

You got 17 wrong

You got #1 wrong

You got #2 wrong

You got #3 wrong

You got #4 wrong

You got #5 wrong

You got #6 wrong

You got #8 wrong

You got #9 wrong

You got #11 wrong

You got #12 wrong

You got #13 wrong

You got #14 wrong

You got #15 wrong

You got #16 wrong

You got #18 wrong

You got #19 wrong

You got #20 wrong

*/

i have written a java code but i need the results to be displayed in a method called displayResults, how do i do it? import java.util.Scanner; public class Driv
i have written a java code but i need the results to be displayed in a method called displayResults, how do i do it? import java.util.Scanner; public class Driv
i have written a java code but i need the results to be displayed in a method called displayResults, how do i do it? import java.util.Scanner; public class Driv
i have written a java code but i need the results to be displayed in a method called displayResults, how do i do it? import java.util.Scanner; public class Driv
i have written a java code but i need the results to be displayed in a method called displayResults, how do i do it? import java.util.Scanner; public class Driv
i have written a java code but i need the results to be displayed in a method called displayResults, how do i do it? import java.util.Scanner; public class Driv
i have written a java code but i need the results to be displayed in a method called displayResults, how do i do it? import java.util.Scanner; public class Driv

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site