This discussion is archived
1 Reply Latest reply: Dec 6, 2010 7:55 AM by 796440 RSS

Having difficulty running this codes on Netbeans.

819445 Newbie
Currently Being Moderated
public class Quiz extends Form implements CommandListener {

     Dictionary dictionary;
     private Vector questions;
     private Data data;

     Command exit;
     Command select;

     TextField textBoxQuestion;
     ChoiceGroup listAnswers;
     TextField textScore;

     int correct = 0;
     int score = 0;
     int qNo = 0;

     public Quiz(String arg0, Dictionary dictionary) {
          super(arg0);
          this.dictionary = dictionary;

          data = dictionary.data;
          questions = data.getQuestions();

          select = new Command("Select", Command.OK, 1);
          exit = new Command("End", Command.EXIT, 1);

          this.addCommand(select);
          this.addCommand(exit);
          this.setCommandListener(this);

          textBoxQuestion = new TextField("", "", 20, 0);
          listAnswers = new ChoiceGroup("Answers:", ChoiceGroup.EXCLUSIVE);
          textScore = new TextField("Score:", "" + score, 5, 0);

          this.append(textBoxQuestion);
          this.append(listAnswers);
          this.append(textScore);

          askQuestion();
     }

     public Quiz(String arg0) {
          super(arg0);
     }

     public void commandAction(Command arg0, Displayable arg1) {
          if (arg0 == select) {
               if (listAnswers.getSelectedIndex() == correct)
                    score++;
               if (qNo > data.MAX - 1) {
                    quizOver("Quiz finished.");
                    return;
               } else
                    askQuestion();
          } else if (arg0 == exit) {
               quizOver("Player pressed quit.");
          }
     }

     private void askQuestion() {
          Random r = new Random();
          int randomIntQ = r.nextInt();
          if (randomIntQ < 0)
               randomIntQ = -randomIntQ;
          randomIntQ = randomIntQ % data.MAX;
          Question q = (Question) questions.elementAt(randomIntQ);
          textBoxQuestion.setString(q.question);

          String a3 = q.answer;
          String a1 = "";
          String a2 = "";
          do {
               int randomInt = r.nextInt();
               if (randomInt < 0)
                    randomInt = -randomInt;
               randomInt = randomInt % data.MAX;
               a1 = ((Question) questions.elementAt(randomInt)).answer;
          } while (a1.equals(a3));
          
          do {
          int randomInt = r.nextInt();
          if (randomInt < 0)
               randomInt = -randomInt;
          randomInt = randomInt % data.MAX;
          a2 = ((Question) questions.elementAt(randomInt)).answer;
          } while (a2.equals(a3) || a2.equals(a1));
          
          listAnswers.deleteAll();

          int randomOrder = r.nextInt();
          if (randomOrder < 0)
               randomOrder = -randomOrder;
          randomOrder = randomOrder % 6;
          if (randomOrder == 0) {
               listAnswers.append(a2, null);
               listAnswers.append(a1, null);
               listAnswers.append(a3, null);
               correct = 2;
          } else if (randomOrder == 1) {
               listAnswers.append(a1, null);
               listAnswers.append(a2, null);
               listAnswers.append(a3, null);
               correct = 2;
          } else if (randomOrder == 2) {
               listAnswers.append(a1, null);
               listAnswers.append(a3, null);
               listAnswers.append(a2, null);
               correct = 1;
          } else if (randomOrder == 3) {
               listAnswers.append(a2, null);
               listAnswers.append(a3, null);
               listAnswers.append(a1, null);
               correct = 1;
          } else if (randomOrder == 4) {
               listAnswers.append(a3, null);
               listAnswers.append(a1, null);
               listAnswers.append(a2, null);
               correct = 0;
          } else if (randomOrder == 5) {
               listAnswers.append(a3, null);
               listAnswers.append(a2, null);
               listAnswers.append(a1, null);
               correct = 0;
          } else {
               System.out.println("Error.");
          }

          textScore.setString("" + score);

          qNo++;
     }

     Command scoreQuit = new Command("OK", Command.OK, 1);

     private void quizOver(String string) {
          Form qo = new Form(string);
          TextField scoreField = new TextField("Score:", "" + score, 5, 0);
          if(score > dictionary.max_score)
               dictionary.max_score = score;
          qo.append(scoreField);
          qo.addCommand(scoreQuit);
          qo.setCommandListener(new CommandListener() {
               public void commandAction(Command arg0, Displayable arg1) {
                    quit();
               }
          });
          Display.getDisplay(dictionary).setCurrent(qo);
     }

     private final void quit() {
          dictionary.mainmenu();
     }
}
  • 1. Re: Having difficulty running this codes on Netbeans.
    796440 Guru
    Currently Being Moderated
    1. When posting code, put { code } before and after it (without the spaces inside the brackets) so it will be readable. Use the Preview tab to see how your post will look before submitting.

    2. You have to ask an actual question. We have no idea what kind of "difficulty" you're having. Is your mouse broken so you can't click Run? Is there a vicious dog between you and your computer? Is there a compiler error? If so, what's the complete, exact error message, and which line is causing it? Is there a runtime error? If so, what's the complete, exact error message, and which line is causing it? Does it run but just not do what you want? If so, what do you want it to do and what is it doing instead? When you try to run it, does it kill you and then resurrect you at the previous level?