views:

94

answers:

2

i am developing an App,its like a test series,dere r two version FULL & LITE,for FULL total number of question in d xml database is around 300,& for LITE version its 20,so wen d test starts for lite version,each time a user starts a test,10 questions r being generated randomly one after d oder wid no repeatation,& for full version,its like 50 questions per test taken.Nw i want to add a new feature,which is like exhausting all d questions in d database,before a question gets repeated,which means say in d xml database for full version dere r 300 questions,so for the first 6 test taken by d user(coz each test comprise of 50 questions) dere should nt b any repeatation of question,same for the lite version,after 20 question hav been exhauasted,which means a user appeared for two test,den only dere can b repeatation of old question. i am providing u d required pages....

AppMain Page

package com.firstBooks.series7.db;

import java.util.Random;
import com.firstBooks.series7.AppMain;
import com.firstBooks.series7.db.parser.XMLParser;

public class DBMain {

 public static String answer = "";
 public static String selectedAnswer = "";
 public static Question curQuestion;

 public static int currQuesNumber = 1;
 public static int correctAnswerCount = 0;
 public static int totalNumofQuestions = AppMain._totalNumofQuestions ;

 static int quesNum[] = new int[XMLParser.questionList.size()];
 static int quesCount = -1;

 static{ 
  initialize(); 
 }

 private static void initialize(){

  Random rgen = new Random();  // Random number generator

  //--- Initialize the array 
  for (int i=0; i<quesNum.length; i++) {
      quesNum[i] = i;
  }

  //--- Shuffle by exchanging each element randomly
  for (int i=0; i< quesNum.length; i++) {
      int randomPosition = rgen.nextInt(quesNum.length);
      int temp = quesNum[i];
      quesNum[i] = quesNum[randomPosition];
      quesNum[randomPosition] = temp;
  }
 }

 /*Changed the code to get a unique random number
  * @author: Venu  
 */
 public static int getQuestionNumber() {
  quesCount++;
  if(quesCount < quesNum.length){
   return quesNum[quesCount];
   }
  else{ 
     initialize();
     quesCount = -1;
     return getQuestionNumber();
  }
 }
}

XMLParser Page

package com.firstBooks.series7.db.parser;

import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.firstBooks.series7.AppMain;
import com.firstBooks.series7.db.Question;

public class XMLParser {

 private Document document;
 public static Vector questionList;

 public XMLParser() {     
  questionList = new Vector();
 }

 public void parseXMl() throws SAXException, IOException,
   ParserConfigurationException {

  // Build a document based on the XML file.
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  InputStream inputStream = getClass().getResourceAsStream(AppMain._xmlFileName);

  document = builder.parse(inputStream);
 }

 public void parseDocument() {
  Element element = document.getDocumentElement();

  NodeList nl = element.getElementsByTagName("question");

  if (nl != null && nl.getLength() > 0) {
   for (int i = 0; i < nl.getLength(); i++) {
    Element ele = (Element) nl.item(i);
    Question question = getQuestions(ele);
    questionList.addElement(question);
   }
  }
 }

 private Question getQuestions(Element element) {

  String title = getTextValue(element, "title");
  String choice1 = getTextValue(element, "choice1");
  String choice2 = getTextValue(element, "choice2");
  String choice3 = getTextValue(element, "choice3");
  String choice4 = getTextValue(element, "choice4");
  String answer = getTextValue(element, "answer");
  String rationale = getTextValue(element, "rationale");

  Question Questions = new Question(title, choice1,
    choice2, choice3, choice4, answer, rationale);

  return Questions;
 }

 private String getTextValue(Element ele, String tagName) {
  String textVal = null;
  NodeList nl = ele.getElementsByTagName(tagName);
  if (nl != null && nl.getLength() > 0) {
   Element el = (Element) nl.item(0);
   textVal = el.getFirstChild().getNodeValue();
  }

  return textVal;
 }
}

Questions Page

package com.firstBooks.series7.db;

public class Question {

 String title;
 String choice1;
 String choice2;
 String choice3;
 String choice4;
 String answer;
 String rationale;

 public Question(String title, String choice1, String choice2,
   String choice3, String choice4, String answer, String rationale) {

  this.title = title;
  this.choice1 = choice1;
  this.choice2 = choice2;
  this.choice3 = choice3;
  this.choice4 = choice4;
  this.answer = answer;
  this.rationale = rationale;
 }

 public String getTitle() {
  return title;
 }

 public String getChoice1() {
  return choice1;
 }

 public String getChoice2() {
  return choice2;
 }

 public String getChoice3() {
  return choice3;
 }

 public String getChoice4() {
  return choice4;
 }

 public String getAnswer() {
  return answer;
 }

 public String getRationale() {
  return rationale;
 }
}

DBMain Page

package com.firstBooks.series7.db;

import java.util.Random;
import com.firstBooks.series7.AppMain;
import com.firstBooks.series7.db.parser.XMLParser;

public class DBMain {

 public static String answer = "";
 public static String selectedAnswer = "";
 public static Question curQuestion;

 public static int currQuesNumber = 1;
 public static int correctAnswerCount = 0;
 public static int totalNumofQuestions = AppMain._totalNumofQuestions ;

 static int quesNum[] = new int[XMLParser.questionList.size()];
 static int quesCount = -1;

 static{ 
  initialize(); 
 }

 private static void initialize(){

  Random rgen = new Random();  // Random number generator

  //--- Initialize the array 
  for (int i=0; i<quesNum.length; i++) {
      quesNum[i] = i;
  }

  //--- Shuffle by exchanging each element randomly
  for (int i=0; i< quesNum.length; i++) {
      int randomPosition = rgen.nextInt(quesNum.length);
      int temp = quesNum[i];
      quesNum[i] = quesNum[randomPosition];
      quesNum[randomPosition] = temp;
  }
 }

 /*Changed the code to get a unique random number
  * @author: Venu  
 */
 public static int getQuestionNumber() {
  quesCount++;
  if(quesCount < quesNum.length){
   return quesNum[quesCount];
   }
  else{ 
     initialize();
     quesCount = -1;
     return getQuestionNumber();
  }
 }
}

If any one can help me out,will b really grateful

A: 

Copy your questions in a temporary ArrayList.
1. select a random element from 0 to listsize
2. delete the question from the list.
You can go on the same steps 1 and 2 for every new random question

class RandomQuestionGenerator
        implements java.util.Enumeration<Question> {
    java.util.ArrayList<Question> randal;
    java.util.Random rnd = new java.util.Random();
    public RandomQuestionGenerator(Question[] qs) {
        randal = new java.util.ArrayList<Question>();
        for (int i = 0; i < qs.length; i++) {
            randal.add(qs[i]);
        }
    }
    public boolean hasMoreElements(){
        return (randal.size() > 0);
    }
    public Question nextElement() {
        Question r = null;
        if (hasMoreElements()) {
            int i = rnd.nextInt(randal.size());
            r = randal.get(i);
            randal.remove(i);
        }
        return r;
    }
}  

.

Oops
A: 

cant copy dem in an array list,questions r in an xml database,so we need to generate frm dat xml database only,xml_full & xml_lite,dese r d xml database,kindly help me wid the code i hav given abaove,i want to store d questions in a memory,& generate dem randomly & uniquely,by d way my application is for blackberry,& my ide is jdp plugin for eclipse3.4 ganymade

Arunabha Dutta Choudhury