Question:
Java array question?
Filipinocalypse
2008-02-18 15:34:38 UTC
How can i call an array from one class to another?

Situation:

I have this class (which is a quiz class) where there are two arrays. The methods add a question to an array and an answer to the other array (yes, they are parallel arrays).

The assignment is to create another class with a main method where it gives a quiz to the user. Now the problem is, how can i transfer the question and answer from one class to another.
Four answers:
micealg
2008-02-18 15:53:36 UTC
You could declare the two arrays as static, this means that you can call them with the class name and array name.
Mike C
2008-02-18 17:13:03 UTC
Well,



I'm not EXACTLY sure what you are trying to do, but here are a few suggestions that may help.



You have class Quiz that has two arrays, so I assume the attributes will look something like this.



public class Quiz

{

String[] questions; //I'm assuming the arrays hold strings

String[] answers;

}



If you wanted to return just one answer or question at a time you could use a 'getAnswer' or 'getQuestion' method that takes an index as a parameter and returns the element at that index.



This is sort of how it would be used in your main method:



// Create your quiz. Let's assume it has all the q's and a's

// programmed in already.

Quiz myQuiz = new Quiz();

String myAnswer;



// Get the answer for question 5 (or 6, if you haven't taken

// into account the zero-based array)

myAnswer = myQuiz.getAnswer(5);



If you wanted to return the entire array you could have a method called getAnswerList() (or getQuestionList() ) that would look like this:



public String[] getAnswerList()

{

return answers;

}



Then your other class could get access to the array by calling it's method, like this:



String[] myAnswers = myQuiz.getAnswerList();



Another option (which you MAY already be doing) is to use a two dimensional array, so you could have one array that holds both the question AND the answer in the same array.



Let me know if this helps,



-Mike
?
2008-02-18 16:34:45 UTC
Not sure exactly about the details but:



One class has an array of Questions and an array of Answers.



So I am thinking the main class will ask question from the Question array in the Quiz class and get the answers in a different local "main" array. Then you send this new array to the quiz class and compare it to the answer array there?



example:

Main

String myAnswers = new String[5];

Quiz myQ = new Quiz();



loop

myQ.getQuestions[ i ] //first question is asked

myAnswers= get answer maybe (yes/no).

End loop



myQ.sendAnswerOver(myAnswers);

Sends to quiz array to compare answers in answer array

Not sure what values arrays hold.
joolzz79
2008-02-18 15:54:20 UTC
Create a class... If this involves computers do it on Java with a site that runs on a site with Java, Like: Whyville Mini Clip, or any other site that runs on Java if you already know a site with Java then the quiz should be a piece of cake! Good Luck on the Quiz!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...