//********************************************************************
// Question.java
//
// Represents a question (and its answer).
//********************************************************************
public class Question implements Complexity
{
private String question, answer;
private int complexityLevel;
//-----------------------------------------------------------------
// Constructor: Sets up the question with a default complexity.
//-----------------------------------------------------------------
public Question (String query, String result)
{
question = query;
answer = result;
complexityLevel = 1;
}
//-----------------------------------------------------------------
// Sets the complexity level for this question.
//-----------------------------------------------------------------
public void setComplexity (int level)
{
complexityLevel = level;
}
//-----------------------------------------------------------------
// Returns the complexity level for this question.
//-----------------------------------------------------------------
public int getComplexity()
{
return complexityLevel;
}
//-----------------------------------------------------------------
// Returns the question.
//-----------------------------------------------------------------
public String getQuestion()
{
return question;
}
//-----------------------------------------------------------------
// Returns the answer to this question.
//-----------------------------------------------------------------
public String getAnswer()
{
return answer;
}
//-----------------------------------------------------------------
// Returns true if the candidate answer matches the answer.
//-----------------------------------------------------------------
public boolean answerCorrect (String candidateAnswer)
{
return answer.equals(candidateAnswer);
}
//-----------------------------------------------------------------
// Returns this question (and its answer) as a string.
//-----------------------------------------------------------------
public String toString()
{
return question + "\n" + answer;
}
}
im basically working with this class. i have created another class called quiz where my main objective is to be able to add questions and answer to the quiz and eventually run the quiz ( ask user question and accept answer to each one, keep track of results and prints them) ignore the complexity part.
i cannot figure this out and have been struggling with it for a while. i am basically trying to store questions into an array. the addQuestion method is supposed to ask the user what is the question and answer it wants to add to the quiz (array). so far i have this
import java.util.Scanner;
public class Quiz
{
Question[] q = new Question[25];
public void addQuestion()
{
Scanner scan = new Scanner(System.in);
System.out.println ("Type the question you want to add followed by comma and the answer");
for(int i = 0; i < q.length; i++)
{
q[i] = scan.nextString();
}
}
public void giveQuiz()
{
}
}
it errors at part q[i] = scan.nextString(); and i could be going about this completely wrong. if someone could help me that would be awesome.