Quote:
Originally Posted by pefok
Hello everyone,
One of my course works is to implement a java based quiz system. the program consists of two modules one for the teacher to create, set questions , answers and the number of questions to be published, and the other one is to allow the student to run the quiz. the data for the quiz is stored in text files and the correct answers are encrypted. everytime the students take a quiz the result has to be stored and a report containing the number of questions were answered correctly and the number of questions were answered incorrectly to be included for the teacher to view later.
Would really appreciate some suggestions regarding the following questions.
1. I am not entirly sure what data structure to use in order to link each question with the right answer?
2. Whether to store the correct answers in a seperate file or in the same file as the questions and their associated answers?
3. How to Structure the database file?
Thanks for all your replys in advance.
1.) you can use a Vector.
- Vector answers = new Vector();
-
Vector questions= new Vector();
-
questions.add("question1");
-
answers.add("answer1");
-
questions.add("question2");
-
answers.add("answer2");
-
questions.add("question3");
-
answers.add("answer3");
-
...
-
System.out.println("The first answer is: " + answers .elementAt(0));
-
System.out.println("The second question is: " + questions.elementAt(1));
You could also try with Properties:
- Properties questionsAndAnswers = new Properties();
-
questionsAndAnswers.setProperty("question1", "answer1");
-
questionsAndAnswers.setProperty("question2", "answer2");
-
questionsAndAnswers.setProperty("question3", "answer3");
-
...
-
System.out.println("The first answer is: " + questionsAndAnswers.getProperty("question1"));
or you can use string-arrays:
- String [][] questionsAndAnswers = {{"question1", "answer1"}, {"question2", "answer2"}, {"question3", "answer3"}};
-
System.out.println("The first answer is: " + questionsAndAnswers[0][1]);
2.) store them in a separate file. One file for the original question and answer. And one file for each student for all the answers a student has given.
If you mix all data in one file, you will have problems if you want to modify/delete some data later on.
3.) Use XML. Then here is already a java-method that you can use to read and save all the data for you. (read about SAX and DOM). XML is industrial standard. A lot of programs can read it or create it and so you can use the data for everything. You can display the file directly into your browser, convert it to a table for a database, edit it comfortably etc.
For example:
...
- <student>
-
<name>newbie</name>
-
<answerList>
-
<answer>answer1</answer>
-
<answer>answer2</answer>
-
<answer>answer3</answer>
-
</answerList>
-
</student>
4.) Now I have a question:
It's not quite clear why you post this question in this forum if this task was given by a teacher for your programming course. If others solve it for you, then you don't learn. I hope the answers gave to you are getting you started. But try yourself as much as you can. Next time, post some code from your own tries where you got stuck. Have you tried already or just copied the task onto the forum without trying or thinking/reading yourself?