PDA

View Full Version : Java - cannot find symbol error


nickyfraggle
12-06-2006, 11:58 AM
Hello,

I am new to Java and this is driving me nuts!

I created an abstract data class called McQuestion. It creates instances to store the values for a multiple choice question - question, 5 answers, correct answer, no of attempts and no of correct attempts.

This compiles and seems to work ok...(i created a tester class and it does what it's supposed to do.)

I now want to create a class QuestionManager which creates a Vector of all the questions (just the actual question not any answers) using the McQuestion class. I used a vector instead of an array so there is no limit to the number of questions I can store.

I'm getting an error saying it can't find the McQuestion class...

If I add a main method..I get an illegal start of type...confused!

Any help would be appreciated.

Nicky

CODE:
McQUESTION (abstract class)

/*
* MCQuestion.java
* Comp 213 Java Assignment 1
* The class McQuestion implements an abstract data type of multiple-choice questions.
* Each question has 5 possible answers, and a record must be kept of how many times
* the question has been attempted and the number of times that it has been answered correctly.
* Created on: 14 October 2006
*/

/**
*
* @author Nicola Farrell Student ID: 200341327
*
*/

import java.util.*;

public class McQuestion {

/**
* Declaring the fields for the McQuestion class
*/

private String questionText;
private Vector<String> answers;
private int numberOfAttempts;
private int numberOfCorrectAttempts;
private int correctAnswer;

/**
* The constructor for the McQuestion class.
*
* @param vector Creates instances for the question text and the correct answer. A vector is used,
* therefore it also creates elements for the 5 possible answers.
* @ param questionText Initiates questionText where the question is stored.
* @param correctAnswer Initiates correctAnswer which holds the number of times that the
* question has been answered correctly.
* @param answers A vector to store the answer values.
*/

public McQuestion(String qt, String pa1, String pa2, String pa3, String pa4, String pa5, int ca) {

questionText = qt;

answers = new Vector<String>();
answers.addElement(pa1);
answers.addElement(pa2);
answers.addElement(pa3);
answers.addElement(pa4);
answers.addElement(pa5);

correctAnswer = ca;

}

/**
* This method returns the text for the question stored in the questionText variable.
*
* @param questionText The text of the question to answer.
* @return getQuestion This string returns the question text for the user to answer.
*
*/

public String getQuestion() {
return questionText;
}

/**
* This method returns the five possible answers to the question. These answers have been stored
* in a vector. The loop runs through the values stored within the vector and returns each question
* as a string, provided that the answer number is between 1 and 5. The answer number is
* decremented as the places in the array start at 0 i.e question 1 is held at the 0 index.
*
* @param answerNumber The number of the answer to be inputted by the user.
* @return getAnswer Returns the strings held in the vector. If the question number
* is outside the boundries or doesn't exist, a null value is returned.
*
*/

public String getAnswer(int answerNumber) {
if ( answerNumber >= 1 && answerNumber <= 5 ) {
answerNumber--;
return (String)answers.get(answerNumber);
}
else {
return null;
}
}

/**
* An integer which is the number of times an attempt has been made to answer the question.
* @param NoOfAttempts the number of attempts that have been made to answer the question.
* @return return int Returns the integer storing the value for the number of times the
* question has been attempted.
*
*/

public int getNoOfAttempts() {
return numberOfAttempts;
}

/**
* An integer which is the number of times the question has been answered correctly.
* @param NoOfCorrectAttempts the number of times the question has been answered correctly.
* @return return int Returns the integer storing the value for the number of times the
* question has been answered correctly.
*
*/

public int getNoOfCorrectAttempts() {
return numberOfCorrectAttempts;
}

/**
* Method that takes the answer (as an int) and determines whether it is correct. It incremements the
* numberOfAttempts counter each time the question is answered, and if it is answered correctly, it also
* increments the numberOfCorrectAttempts value. It has some checks, and will output a false value if
* the answer number inputted is outside of the 1-5 range, or if the question is answered incorrectly.
* @param attemptAnswer the answer given by the user as an integer 1,2,3,4 or 5.
* @param correctAnswer the correct answer to the question
* @param NoOfAttempts the number of attempts that have been made to answer the question. It is
* incremented every time an attempt is made.
* @param NoOfCorrectAttempts the number of times the question has been answered correctly. It is
* incremented each time the answer is correct.
* @return return <code>true</code> returns a Boolean value of true if the answer is correct;
* returns a Boolean value of <code>false </code> if the answer is incorrect, or if it is not a
* number between 1 and 5.
*
*/
public boolean attempt(int attemptAnswer) {
numberOfAttempts++;
if ( attemptAnswer >= 1 && attemptAnswer <= 5 ) {
if ( attemptAnswer == correctAnswer ) {
numberOfCorrectAttempts++;
return true;
}
else {
return false;
}
}
else {
return false;
}
}

}
QUESTION MANAGER
import java.util.*;

public class QuestionManager{

/**
* Declaring the fields for the QuestionManager class - a vector to hold the String values of the questions.
*/

private Vector<String> questions;


/**
* The constructor for the QuestionManager class.
*
*/
//public static void main (String[] args) {

public QuestionManager(String qt) {

/** creates a new instance of the McQuestion class called newq1*/
McQuestion newq1 = new McQuestion();

/** gets question with new instance*/
newq1.getQuestion();

/**assigns questionText to variable qt*/
String questionText = qt;

/** creates a new vector called questions to populate using a while loop*/
questions = new Vector<String>();

while( getQuestion.next() )

questions.addElement(qt);

}
}

nikkiH
12-06-2006, 03:47 PM
Are you SURE the error said it couldn't find the class?
When you get an error, it really helps us if you post the full thing.

I see a constructor that takes arguments and then an attempt to use a default empty constructor...

McQuestion newq1 = new McQuestion();

That class has no constructor that matches no arguments.

public McQuestion(String qt, String pa1, String pa2, String pa3, String pa4, String pa5, int ca)