Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Java Quiz Help


  • Please log in to reply

#1
zell_ffhut

zell_ffhut

    Member

  • Member
  • PipPip
  • 34 posts
Hi Guys

I'm just learning Java, and i've been plugging away with a exercise in a book, but im a bit stuck!

I need to make a program that asks a user a question, takes there answer, and then checks it to make sure its correct. Here's what i have so far.

This is the main Class

/*
 * QuestionMain.java
 *
 * Created on 09 February 2006, 17:18
 */

/**
 *
 * @author 04127263
 */

import java.io.*;

public class QuestionMain 
{
	static private BufferedReader in =
	new BufferedReader(new InputStreamReader(System.in));
	
	public static void main(String[] args) 
	{
		String question;
	  
		Questions q1 = new Questions(3);
		q1.setAnswer(0, "The method does not return a value.");
		q1.setAnswer(1, "The method is empty.");
		q1.setAnswer(2, "The method doesn't do anything.");
		// TODO code application logic here
		question = "What does 'void' mean when it precedes the name of a method?";
		System.out.println(questions(1));
		System.out.println(questions(2));
		System.out.println(questions(3));
		System.out.println("Please Choose 0, 1 or 2");
	}
	
}


This is a Class


/*
 * Questions.java
 *
 * Created on 09 February 2006, 15:22
 */

/**
 *
 * Christopher Murray 04127263
 */
public class Questions 
{
	private String questionText;
	private String [] answers;
	String answerText;
	private int correctAnswer;
	
	//Creates an Instance of the class Questions
	public Questions(int noAnswers)
	{   
		answers=new String[noAnswers];
	}	   
   
	//Gets the Question from the Main Class
	public void setQuestion(String qText)
	{
		questionText = qText;
	} 
	
	//This sets the answer numbers for the answer
	public void setAnswer(int answerNo, String answerText)
	{
		answers[answerNo]=answerText;
	}
	
	//Gets the number for the correct answer
	public void setCorrectAnswer(int answerNo)
	{
		correctAnswer = answerNo;
	}
	
	//Tests to check for correct answer
	public boolean qAnswerCorrect(int answer)
	{
	   if (answer == correctAnswer)
		 return true;
	   else
		 return false;
	}
	
}



The main problem i having currently is that the Main Class calls the error -

Can not find symbol -
symbol : method questions(int)
location: class QuestionMain
System.out.println(questions(1));

Any help would be fantastic.
Thanks
  • 0

Advertisements


#2
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
You're going to need to make some way that you can retreive values from your Questions class, like a get method.
public String get(int index) {
	return answers[index];
}
Then, you can change the following:
System.out.println(questions(1));
System.out.println(questions(2));
System.out.println(questions(3));
to
System.out.println(questions.get(0));
System.out.println(questions.get(1));
System.out.println(questions.get(2));

Edited by destin, 15 February 2006 - 05:23 PM.

  • 0

#3
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Thanks for your input!

I've added this method to the questions.java class

	//Returns the Value of questionText
	public String getQuestion(int answerNo)
	{
		return answers[answerNo];   
	}

and this code to the main class


		System.out.println(question.getQuestion(1));
		System.out.println(question.getQuestion(2));
		System.out.println(question.getQuestion(3));



still getting errors :tazz:

symbol : method getQuestion(int)
location: class java.lang.String
System.out.println(question.getQuestion(1));

Any ideas?
  • 0

#4
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
Ah, sorry. If you look back in your first post, you'll notice that question is a String object, not a Questions object. You want to use q1.getQuestion(int).

Also, make sure that you use 0, 1, and 2 instead of 1, 2, and 3 or an ArrayIndexOutOfBoundsException will be thrown.
  • 0

#5
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
Thank you

Getting somwhere now. The program runs with no errors. But it dosen't do a whole lot! :tazz:
  • 0

#6
zell_ffhut

zell_ffhut

    Member

  • Topic Starter
  • Member
  • PipPip
  • 34 posts
I think im nearly done now, just think i need to compare the users input, with the correct answer. Can anyone help me on how to do this please? :tazz:

/*
 * QuestionMain.java
 *
 * Created on 09 February 2006, 17:18
 */

/**
 *
 * @author 04127263
 */

import java.io.*;

public class QuestionMain 
{
	static private BufferedReader in =
	new BufferedReader(new InputStreamReader(System.in));
	
	public static void main(String[] args) 
	{
		String question;
		int number;
	  
		Questions q1 = new Questions(3);
		q1.setAnswer(0, "The method does not return a value.");
		q1.setAnswer(1, "The method is empty.");
		q1.setAnswer(2, "The method doesn't do anything.");
		
		q1.setCorrectAnswer(0);
		
		// TODO code application logic here
		question = "What does 'void' mean when it precedes the name of a method?";
		System.out.println(q1.getQuestion(0));
		System.out.println(q1.getQuestion(1));
		System.out.println(q1.getQuestion(2));
		System.out.println("Please Choose 0, 1 or 2");
		
		number = Text.ReadInt(in);
		
	   
	}
	
}

/*
 * Questions.java
 *
 * Created on 09 February 2006, 15:22
 */

/**
 *
 * Christopher Murray 04127263
 */
public class Questions 
{
	private String questionText;
	private String [] answers;
	private int correctAnswer;
	
	//Creates an Instance of the class Questions
	public Questions(int noAnswers)
	{   
		answers=new String[noAnswers];
	}	   
   
	//Gets the Question from the Main Class
	public void setQuestion(String qText)
	{
		questionText = qText;
	} 
	
	//This sets the answer numbers for the answer
	public void setAnswer(int answerNo, String answerText)
	{
		answers[answerNo]=answerText;
	}
	
	//Gets the number for the correct answer
	public void setCorrectAnswer(int answerNo)
	{
		correctAnswer = answerNo;
	}
	
	//Tests to check for correct answer
	public boolean qAnswerCorrect(int answer)
	{
	   if (answer == correctAnswer)
		 return true;
	   else
		 return false;
	}
	
	//Returns the Value of questionText
	public String getQuestion(int answerNo)
	{
		return answers[answerNo];   
	}
	
	
	//Returns the Value of answerText
	public String getAnswers(String answerText)
	{
		int count;
		
		for(count = 0; count < answers.length; count++)
		{
			answerText = answerText + count + " " + answers[count];
		}
		answerText = answerText + "\n";
		return answerText;
	}
	 
}



  • 0

#7
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
This it the easy part. Look at your Questions class. You have a qAnswerCorrect(int) method that will check if an answer is correct. So you can do something like:
if (q1.qAnswerCorrect(number)) {
	System.out.println("Correct!");
} else {
	System.out.println("Sorry! Incorrect!");
}

Hope this helps.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP