Hi,
This is the questions:
A multiple-choice examination consists of twenty questions. Each question has five
choices, labelled A, B, C, D and E. The first line of data contains the correct
answers to the twenty questions in the first 20 consecutive character positions, for
example:
BECDCBAADEBACBAEDDBE
Each subsequent line contains the answers for a candidate. Data on a line consists
of a candidate number (an integer), followed by one or more spaces, followed by
the twenty answers given by the candidate in the next twenty consecutive character
positions. An X is used if a candidate did not answer a particular question. You
may assume all data are valid and stored in a file exam.dat. A sample line is:
4325 BECDCBAXDEBACCAEDXBE
There are at most 100 candidates. A line containing a “candidate number” 0 only
indicates the end of the data.
Points for a question are awarded as follows:– correct answer: 4 points; wrong
answer: -1 point; no answer: 0 points
Write a program to process the data and print a report consisting of candidate
number and the total points obtained by the candidate, in ascending order by
candidate number. At the end, print the average number of points gained by the
candidates.
This my solution:
package worksheet6Quest21; import java.util.*; import java.io.*; public class MultipleChoiceExam { //declares constants //final static int MaxCandidates = 100; final static int MaxAnswers = 20;// there are only 20 correct answers to the 20 questions final static int correctAns = 4;// points for answers final static int wrongAns = -1; final static int noAns = 0; public static void main (String[] args) throws IOException { Scanner in = new Scanner (new FileReader ("exam.dat")); char [] answerArray = new char[MaxAnswers+1];//array that stores the correct answers int numOfCharacters; char candidateAns; int candidateNum; //iniitializing the array for(int i = 0; i < answerArray.length; i++) answerArray[i] = ' '; candidateNum = in.nextInt();// reads candidate number checkAnswers(candidateAns); in.close(); }//end main //Sub Methods //Store the list of characters for the answer and returns the number of characters stored public static int storeWords(Scanner in, char [] answerArray) throws IOException{ int i = 1; while(in.hasNext() && i <= MaxAnswers){ answerArray[i] = in.next().charAt(0); } return i+1; } //Correct answers- returns the number of correct answers public static int checkAnswer(char[] answerArray, char[] candidateAns, char c){ //Test candidate answer against correct answer int correctCount =0; int wrongCount = 0; int noAnsCount = 0; int totalPoints = 0; int highestPoints = 0; for(int k = 0; k < answerArray.length; k++){ if (Character.toLowerCase(c) == Character.toLowerCase(answerArray[k])){ //Tests if candidate answer is correct if (candidateAns[k]== Character.toLowerCase(c)); { correctCount ++; //increment number of correct answers } if (Character.toLowerCase(c) != Character.toLowerCase(answerArray[k])) { //Tests if candidate answer is wrong or no answer was given if (candidateAns[k] != Character.toLowerCase(c)); { wrongCount ++; //increment number of wrong answers } if (candidateAns[k] != Character.toLowerCase(c) && candidateAns[k] ==' '); { noAnsCount ++; // increment number of no answers } } } totalPoints= ((correctCount * correctAns) + (wrongCount * wrongAns) + (noAnsCount * noAns)); if (totalPoints > highestPoints)//finds highest points { highestPoints = totalPoints; highestPointscandidate = candidateNum; } }//end for } }
My problems:
1.linking the main to the checkAns method
2.how do I correctly read the candidate numbers
Haven't attempted the ascending order part because I don't know how to store the highest points and candidate numbers in a array.
please help.