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: Problem creating a new object

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem creating a new object

    Help! Cannot figure out what is wrong with my code.
    <ExamDemo.java:25: cannot find symbol
    symbol  : constructor DriverExam(java.lang.String[])
    location: class DriverExam
    DriverExam exam = new DriverExam(answers);
                      ^
    1 error>

    < import java.util.Scanner;
    // Input Validation:
       public class ExamDemo
       {
          public static void main(String[] args)
          {
             System.out.println("    Driver's Liscense Exam     ");
             Scanner kb = new Scanner(System.in);
     
             System.out.println(" 20 multiple choise questions Mark A,B,C,D ");
          // Inputting string
             String[] answers = new String[20];
             String answer;
             for (int i = 0; i < 20; i++)
             {
                do
                {
                   System.out.print((i + 1) + ": ");
                   answer = kb.nextLine();
                }
                while (!isValidAnswer(answer));
                answers[i] = answer;
             }
          // Process
             DriverExam exam = new DriverExam(answers);
          // Result
             System.out.println("        Results       ");
          // outputting Total correct
             System.out.println("Total Correct: " + exam.totalCorrect());
          // outputting Total incorrect
             System.out.println("Total Incorrect: " + exam.totalIncorrect());
             String passed = exam.passed() ? "YES" : "NO";
          //result pass or fail
             System.out.println("Passed: " + passed);
             if (exam.totalIncorrect() > 0)
             {
                System.out.println("The incorrect answers are:");
                int missedIndex;
                for (int i = 0; i < exam.totalIncorrect(); i++)
                {
                   missedIndex = exam.questionsMissed()[i] + 1;
                   System.out.print(" " + missedIndex);
                }
             }
          }
          public static boolean isValidAnswer (String answer)
          {
             return "A".equalsIgnoreCase(answer) || "B".equalsIgnoreCase(answer) || "C".equalsIgnoreCase(answer) || "D".equalsIgnoreCase(answer) ;
          }
       }>
    <   import java.util.Scanner;
     
       class DriverExam
       {
       //an array of student's answers.
          private char[] correctAnswers = {
             'B','D','A','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};
       // Store the user answers.
          private char[] studentAnswers;
          int[] missed = new int[correctAnswers.length];
       //Process the user answers
          public DriverExam (char[] Answers)
          {
             studentAnswers = new char[Answers.length];
             for (int i = 0; i < Answers.length; i++)
             {
                studentAnswers[i] = Answers[i];
             }
          }
       //returns boolean value if correct answers > 15
          public boolean passed()
          {
             if(totalCorrect() >= 15)
                return true;
             else
                return false;
          }
       //totalCorrect answers
          public int totalCorrect()
          {
             int correctCount = 0;
             for (int i = 0; i < correctAnswers.length; i++)
             {
                if (studentAnswers[i] == (correctAnswers[i]))
                {
                   correctCount++;
                }
             }
             return correctCount;
          }
       // totalIncorrect answered.
          public int totalIncorrect()
          {
             int incorrectCount = 0;
             for (int i = 0; i < correctAnswers.length; i++)
             {
                if (studentAnswers[i] != (correctAnswers[i]))
                {
                   missed[incorrectCount] = i;
                   incorrectCount++;
                }
             }
             return incorrectCount;
          }
       //Missed questions.
          public int[] questionsMissed()
          {
             return missed;
          }
       }
    //end driver exam class
    >

    Also, keep getting java.lang.ArrayIndexOutOfBoundsException: 20 in my interactions tab when trying to pass e.passed()
    Last edited by smithmar; April 18th, 2012 at 02:19 AM.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Problem creating a new object

    Hello smithmar!
    I haven't tested your code but i think the error occurs because the DriverExam constructor you have needs a char array as an argument and in your ExamDemo class when you try to create a new instance of DriverExam you pass it a String array (answers).

Similar Threads

  1. Creating object everytime object is called
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2012, 05:18 PM
  2. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 09:40 AM
  3. Using a string when creating an object.
    By ZeroLRS in forum Object Oriented Programming
    Replies: 10
    Last Post: July 13th, 2011, 10:22 PM
  4. Creating an object...
    By RodePope4546 in forum Object Oriented Programming
    Replies: 6
    Last Post: June 27th, 2011, 07:44 AM
  5. Creating Servlet object
    By tcstcs in forum Java Servlet
    Replies: 3
    Last Post: May 9th, 2011, 03:13 AM