|
8 Nov 2006, 12:35
|
#1
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Arrays
Let's say I have this situation. I am going to be producing a similar system to NHS Direct where you select which area that concerns you, enter another option and then you get given yes/no type questions.
Depending on which decisions are answered yes and which are answered no, a final message is displayed.
Now I cannot just store these questions in array can I? do I have to individually store each question and mark the array? also, do you HAVE to define how big the array is? can I not just define new string[n] ?
for example:
Code:
String questions[] = new string[8];
questions[1] = "Do you have a headache?";
questions[2] = "Do you have a sore throat?";
etc.
My only problem with all of this is this - trying to use if statements when deciding which questions to pull from this array based on the answers to questions that are going to be stored in another array. It's not that easy in my eyes because the indexes for the questions will not match up with the indexes for the answers and the answers will only be "yes" or "no" - so a boolean in other words. The answers array will be a boolean. I know you suggested using an arraylist but that confused me.
does anyone have any algorithms I can work from to figure this out? thanks.
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
8 Nov 2006, 14:11
|
#2
|
wasted
Join Date: Dec 2000
Location: Under the floorboards
Posts: 1,240
|
Re: Arrays
You probably can store the questions in an array. Which language are you using? The code you pasted could be Java, C++ or maybe even C#, except for the fact that I doubt it would work properly in any of those languages.
__________________
“They were totally confused,” said the birdman, whose flying suit gives him a passing resemblance to Buzz Lightyear in Toy Story. “The authorities said that I was an unregistered aircraft and to fly, you need a licence. I told them, ‘No. To fly, you need wings’.”
|
|
|
8 Nov 2006, 16:29
|
#3
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: Arrays
use an ArrayList to store the questons in. that gives you the accessibility of an array without the worry of how big to make it , since it expands itself automatically
java.util.ArrayList is the class you want, might wanna read up on it ( http://java.sun.com/j2se/1.5.0/docs/...ArrayList.html )
the main functions you'd use in it are get(i) , add(element), add(i,element) , set(i, element) , and size()
get(i) retrieves the element stored at position i , add(element) adds a new element to the arraylist at the end, or you can use add(i,element) to add an element at a specified position , set(i,element) sets the element in the array at position i to the element passed in parameters , and size() returns the number of elements in the arraylist
the (object orientated ) way i would do it though is to create a new class called question, and in it would be two strings. one for question and one for answer, along with the accessor and mutator methods for it.
those question classes would be stored in an arraylist giving you as many as you want, but without having to worry about indexes since each question class stores both the question and the answer
Last edited by Phil^; 8 Nov 2006 at 16:36.
|
|
|
8 Nov 2006, 16:33
|
#4
|
Avenger of Calamari
Join Date: Aug 2003
Location: Alberta, Canada
Posts: 939
|
Re: Arrays
new string[n] will not work... but you can define an array with no limit to the number of 'array spots' if you will.
example method of one of the programs that we've done in class for C#.
[WebMethod(Description="This method returns a string containing the tip of the day")]
public string Tip()
{
string[] tipArray = { "Go away", "TGIF", "I am busy", "Class is over" };
Random keyGen = new Random();
return tipArray[keyGen.Next(4)];
}
This may also solve your problem regarding doing each question manually as below.
To figure out which question goes to what, you may end up having to hardcode each result. This possibly means that you would not have to use a second arraylist with your answers, but it means you would have to do "If answer1 = 'Y' then { insert question #2 here } else {insert question #2.5 here}
etc
Edit: *shakes fist at phil with the quicker typing fingers*
|
|
|
8 Nov 2006, 16:40
|
#5
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: Arrays
thats a way you could do it too, create a string array for each question that contains the qu and the answer etc , and store that in another array. a two dimensional array if you will.
|
|
|
8 Nov 2006, 19:37
|
#6
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
Re: Arrays
Whatever you end up using, make sure you it encapsualte inside a QuestionDatabase class or whatever so that you can change it later.
|
|
|
8 Nov 2006, 21:53
|
#7
|
Love's Sweet Exile
Join Date: May 2001
Location: Living on a Stair (Now Sword-less)
Posts: 2,371
|
Re: Arrays
I'd be tempted to treat it more like a graph then just an array of answers, something like:
Code:
Survey survey = new Survey();
SurveyQuestion q1 = new SurveyQuestion('Do you feel sick?');
SurveyQuestion q2 = new SurveyQuestion('Have you been physically sick?');
SurveyQuestion q3 = new SurveyQuestion('Do you have a headache?');
q1.setSuccessor('yes',q2);
q1.setSuccessor('no',q3);
q2.setSucessor('no',q3);
// etc.
q2.setSuccessor('yes',new EndPoint('You have food poisoning'));
survey.setup(q1,q2,q3);
while(!survey.hasEnded()) {
survey.presentNextQuestion();
}
survey.showResult();
in java-esque pseudo-code.
Replacing the hard-coded survey setup with something database- or file-driven would be fairly trivial.
__________________
--SYMM--
Ba Ba Ti Ki Di Do
|
|
|
9 Nov 2006, 00:09
|
#8
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Arrays
cheers guys - yeah i am using java
will attempt something tonight and get back to you
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
9 Nov 2006, 09:49
|
#9
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Arrays
Quote:
Originally Posted by Phil^
the (object orientated ) way i would do it though is to create a new class called question, and in it would be two strings. one for question and one for answer, along with the accessor and mutator methods for it.
those question classes would be stored in an arraylist giving you as many as you want, but without having to worry about indexes since each question class stores both the question and the answer
|
how does this work? storing an entire class in arraylist?
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
9 Nov 2006, 14:55
|
#10
|
Insomniac
Join Date: May 2003
Posts: 3,583
|
Re: Arrays
an arraylist stores objects, every class created in java extends object.
its done through polymorphism - a hugely powerful subject essentially.
think of it like this
you create a class called car, each car has a list of people in it - whos names are strings.
an arraylist would be like a car park of sorts
|
|
|
9 Nov 2006, 19:28
|
#11
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Arrays
danke
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
17 Nov 2006, 00:31
|
#12
|
☆ ♥
Join Date: Jan 2003
Posts: 3,489
|
Re: Arrays
This is the way I'm currently dealing with this situation. Forget about the actual text I've for the questions, it's just to help me while I'm debugging.
Code:
/* ***************************************************
Method for asking questions and storing the answers
*************************************************** */
public static void quizquestions()
{
// Declare variables
//
String[] question = new String[6]; // store questions
String stringans; // obtain answer to a question & temp. store it
String[] answer = new String[6]; // permenantly store answers to questions
question[0] = ("question 0?");
question[1] = ("question 1?");
question[2] = ("question 2?");
question[3] = ("question 3?");
question[4] = ("question 4?");
question[5] = ("question 5?");
// ask questions
do {
for (int i=1; i<2; i++)
{
answer[0] = JOptionPane.showInputDialog(question[0]);
if (answer[0].equalsIgnoreCase("yes"))
{
answer[2] = JOptionPane.showInputDialog(question[2]);
questiontwo(); // do question two
}
else if (answer[0].equalsIgnoreCase("no"))
{
answer[5] = JOptionPane.showInputDialog(question[5]);
questionfive(); // do question five
}
else
{
JOptionPane.showMessageDialog(null, "You did not enter yes or no.\n\n Please go back and try again."); j--;
}
}
}
}
} while (!answer[0].equalsIgnoreCase("yes") || !answer[0].equalsIgnoreCase("no"));
} // END quizquestions
/* ***************************************************
Method for question two
*************************************************** */
public static void questiontwo()
{
do {
for (int i=1; i<2; i++)
{
if (answer[2].equalsIgnoreCase("yes"))
{
answer[3] = JOptionPane.showInputDialog(question[3]);
questiontwo();
}
else if (answer[2].equalsIgnoreCase("no"))
{
answer[4] = JOptionPane.showInputDialog(question[4]);
}
else
{
JOptionPane.showMessageDialog(null, "You did not enter yes or no.\n\n Please go back and try again."); i--;
}
}
} while (!answer[2].equalsIgnoreCase("yes") || !answer[2].equalsIgnoreCase("no"));
}
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
|
|
|
17 Nov 2006, 02:12
|
#13
|
Warden
Join Date: Jul 2005
Location: The Far Side
Posts: 137
|
Re: Arrays
You might want to use links so that after a question, depending on the answer, it links to the next question.
I don't know Java but in C I would use something like:
Code:
struct QandA {
char *question;
struct QandA *affirm;
struct QandA *deny;
}
then build a path from question to question, so q1 would point to q5 on affirm and q7 on deny.
You might want to make it more specific going from the history but that shouldn't be too difficult. My tip would be to draw out all your questions and responses in a sort of flow chart and see what structure suits it best.
Sorry it's a bit vague
Last edited by SpaceMonkey; 17 Nov 2006 at 02:18.
|
|
|
|
All times are GMT +1. The time now is 03:26.
| |