Results 1 to 2 of 2
  1. #1
    Exceedinglife is offline Member
    Join Date
    Apr 2012
    Posts
    17
    Rep Power
    0

    Default eRROR with my code not sure why

    yellow im writing a program to see if a person pasts a test or not and i am having problems with my code here it is


    Java Code:
    /**
     * DriverExam class
     */
    
    public class DriverExam
    {
       // Array containing the correct answers
       private char[] correct = { 'B', 'D', 'A', 'A',
                                  'C', 'A', 'B', 'A',
                                  'C', 'D', 'B', 'C',
                                  'D', 'A', 'D', 'C',
                                  'C', 'B', 'D', 'A' };
    
       private char[] student;       // The student's answers
       private int[] missed;         // The missed question numbers
       private int numCorrect = 0;   // The number correct
       private int numIncorrect = 0; // The number incorrect
       
       /**
        * Constructor
        * Accepts an array of the student's
        * answers as the argument. The contents
        * of the array are copied to the student
        * field.
        */
       
       public DriverExam(char[] s)
       {
          student = s;
          gradeExam();
       }
       
       /**
        * gradeExam method
        * This method determines the number of
        * correct and incorrect answers. It calls
        * the makeMissedArray method.
        */
       
       private void gradeExam()
       {
         /** Missing code starts here */
    	  char[] correct = { 'B', 'D', 'A', 'A',
              'C', 'A', 'B', 'A',
              'C', 'D', 'B', 'C',
              'D', 'A', 'D', 'C',
              'C', 'B', 'D', 'A' };
    	  char[] student = new char[20];
    	  for(int i=0; i<correct.length; i++)
    	  {
    		  if(student[i] == correct[i])
    		  	numCorrect++;
    		   	  
    	  }
         /** Missing code ends here */
         makeMissedArray();
       }  
       
       /**
        * makeMissedArray method
        * This method makes the missed array and
        * stores the numbers of all the questions
        * that the student missed in it.
        */
       
       private void makeMissedArray()
       {
         /* Missing code starts here */
    	   for(int i=0; i<correct.length; i++)
    	   {
    	   if(student[i] != correct[i])
    	   {
    		   missed[numIncorrect] = student[i];
    		   numIncorrect++;
    	   }
    	   }   
         /* Missing code ends here */
       }
       
       /**
        * passed method
        */
       
       public boolean passed()
       {
          boolean status;
          
          if (numCorrect >= 15)
             status = true;
          else
             status = false;
             
          return status;
       }
       
       /**
        * totalCorrect method
        */
    
       public int totalCorrect()
       {
          return numCorrect;
       }
    
       /**
        * totalIncorrect method
        */
    
       public int totalIncorrect()
       {
          return numIncorrect;
       }
    
       /**
        * questionsMissed method
        * Returns an array containing the numbers
        * of the missed questions. If no questions
        * were missed, returns null.
        */
    
       public int[] questionsMissed()
       {
           return missed;
       }
    }

    Java Code:
    import java.util.Scanner;
    
    /**
     * DriverTest program.
     */
    
    public class DriverTest
    {
       public static void main(String[] args)
       {
    		// Constant for the number of answers
          final int NUM_ANSWERS = 20;
    		
    		// Array to hold the student's answers
          char[] answers = new char[NUM_ANSWERS];
    		
    		// This will reference an array holding the
    		// question numbers of the missed questions.
          int[] missedQuestions = null;
          
          // Create a Scanner object for keyboard input.
          Scanner keyboard = new Scanner(System.in);
    		
          // Get the user's answers to the questions.
          System.out.println("Enter your answers to the " +
                             "exam questions. (Make sure " +
                             "Caps Lock is ON!)");
    
          for (int i = 0; i < answers.length; i++)
          {
             System.out.print("Question " + (i + 1) + ": ");
    	 String input = keyboard.nextLine();
             answers[i] = input.charAt(0);
    
             // Validate the answer.
             while (!valid(answers[i]))
             {
                System.out.println("ERROR: Valid answers are " +
                                   "A, B, C, or D.");
                System.out.print("Question " + (i + 1) + ": ");
        			input = keyboard.nextLine();        
    				answers[i] = input.charAt(0);
             }
          }
          
          // Create a DriverExam object.
          DriverExam exam = new DriverExam(answers);
          
          // Get an array of the missed question numbers.
          missedQuestions = exam.questionsMissed();
          
          // Display a report.
          System.out.println("Correct answers: " + 
    		                   exam.totalCorrect());
          System.out.println("Incorrect answers: " + 
    		                   exam.totalIncorrect());
          
    		// Display whether the student passed or failed.
          if (exam.passed())
             System.out.println("You passed the exam.");
          else
             System.out.println("You failed the exam.");
          
    		// Display a list of missed questions, if any.
          if (missedQuestions != null)
          {
             System.out.println("You missed the following questions:");
             for (int i = 0; i < missedQuestions.length; i++)
                System.out.print(missedQuestions[i] + " ");
          }
       }
       
       /**
        * valid method
        * Returns true if the argument is A, B, C, or D.
        */
       
       public static boolean valid(char c)
       {
          boolean status;
          
          if (c == 'A' || c == 'B' || c == 'C' || c == 'D')
             status = true;
          else
             status = false;
          
          return status;
       }
    }
    Last edited by DarrylBurke; 04-25-2012 at 07:21 AM. Reason: FIxed the comments (workaround for forum bug)

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: eRROR with my code not sure why

    What is the error produced by your program?

Similar Threads

  1. Error in my code
    By MikeJ39 in forum New To Java
    Replies: 2
    Last Post: 10-19-2011, 09:58 PM
  2. error code help please..
    By andnlou2678 in forum New To Java
    Replies: 7
    Last Post: 10-07-2011, 12:11 AM
  3. error in code
    By Akap in forum New To Java
    Replies: 4
    Last Post: 01-31-2011, 06:37 AM
  4. Error Code???
    By andmartha in forum New To Java
    Replies: 11
    Last Post: 10-04-2008, 02:16 AM
  5. error in code
    By dirtycash in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 11:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •