Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Arrays and files

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Arrays and files

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,974
    Thanks
    52
    Thanked 2,270 Times in 2,242 Posts

    Default Re: Arrays and files

    how do I correctly read the candidate numbers
    If the numbers are first on a new line and end with a space, read the line and split it, the number will be the first element in the array.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: August 3rd, 2012, 10:40 AM
  2. Java Picture Processor with PGM files and Multi Arrays
    By snow_mac in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2011, 08:55 PM
  3. Having an issue reading files to create two arrays.
    By ccrosby in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 26th, 2011, 06:47 AM
  4. Replies: 1
    Last Post: March 22nd, 2011, 06:59 PM