Results 1 to 6 of 6
  1. #1
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default Pls help me - how to draw out questions wrongly answered from a pool of array

    Hi, I'm desparately trying to know how to do the code whereby I can dispaly the wrong questions answered from category chosen. There are 4 categories with 5 questions each and I need to dispaly all the wrong questions. So far my code only show the question 1 all the time.:confused:

    Java Code:
    import javax.swing.*;
    
    public class Quiz {//Quiz Class
    
        public final static int NOOFQUESTION = 5;
        public final static int POOLSIZE = 20;
        public final static int CAT1 = 0;
        public final static int CAT2 = 1;
        public final static int CAT3 = 2;
        public final static int CAT4 = 3;
        private Question[] pool;
        private Question[] mcqs;
        private int currentCategory;
        private int currentQuestionIndex;
        private String pic;
        private String result;
        private String wrongquestion;
    
        //constructor
        public Quiz() {
            pool = new Question[POOLSIZE];
            mcqs = new Question[NOOFQUESTION];
    
            pool[0] = new Question(CAT1, "cat1_q1_question", "q1_choice1", "q1_choice2", "q1_choice3", "q1_choice4", 2);
            pool[1] = new Question(CAT1, "cat1_q2_question", "q2_choice1", "q2_choice2", "q2_choice3", "q2_choice4", 2);
            pool[2] = new Question(CAT1, "cat1_q3_question", "q3_choice1", "q3_choice2", "q3_choice3", "q3_choice4", 2);
            pool[3] = new Question(CAT1, "cat1_q4_question", "q4_choice1", "q4_choice2", "q4_choice3", "q4_choice4", 2);
            pool[4] = new Question(CAT1, "cat1_q5_question", "q5_choice1", "q5_choice2", "q5_choice3", "q5_choice4", 2);
            pool[5] = new Question(CAT2, "cat2_q1_question", "choice1", "choice2", "choice3", "choice4", 2);
            pool[6] = new Question(CAT2, "cat2_q2_question", "choice1", "choice2", "choice3", "choice4", 3);
            pool[7] = new Question(CAT2, "cat2_q3_question", "choice1", "choice2", "choice3", "choice4", 2);
            pool[8] = new Question(CAT2, "cat2_q4_question", "choice1", "choice2", "choice3", "choice4", 3);
            pool[9] = new Question(CAT2, "cat2_q5_question", "choice1", "choice2", "choice3", "choice4", 2);
            pool[10] = new Question(CAT3, "cat3_q1_question", "choice1", "choice2", "choice3", "choice4", 2);
            pool[11] = new Question(CAT3, "cat3_q2_question", "choice1", "choice2", "choice3", "choice4", 3);
            pool[12] = new Question(CAT3, "cat3_q3_question", "choice1", "choice2", "choice3", "choice4", 2);
            pool[13] = new Question(CAT3, "cat3_q4_question", "choice1", "choice2", "choice3", "choice4", 3);
            pool[14] = new Question(CAT3, "cat3_q5_question", "choice1", "choice2", "choice3", "choice4", 2);
    
            pool[15] = new Question(CAT4, "cat3_q1_question", "choice1", "choice2", "choice3", "choice4", 2);
            pool[16] = new Question(CAT4, "cat3_q2_question", "choice1", "choice2", "choice3", "choice4", 3);
            pool[17] = new Question(CAT4, "cat3_q3_question", "choice1", "choice2", "choice3", "choice4", 2);
            pool[18] = new Question(CAT4, "cat3_q4_question", "choice1", "choice2", "choice3", "choice4", 3);
            pool[19] = new Question(CAT4, "cat3_q5_question", "choice1", "choice2", "choice3", "choice4", 2);
        }
    
        public void setupMCQs(int cat) {
            currentCategory = cat;
            int j = 0;
            for (int i = 0; i < pool.length; i++) {
                if (pool[i].getCategory() == currentCategory) {
                    mcqs[j] = pool[i];
                    j++;
                }//end if
            }//end for
        }
    
        public String getCurrentCategory() {
            if (currentCategory == CAT1) {
                return "Category 1";
            }
            if (currentCategory == CAT2) {
                return "Category 2";
            }
            if (currentCategory == CAT3) {
                return "Category 3";
            } else {
                return "Category 4";
            }
        }
    
        public void nextCategory() {
            currentCategory++;
        }
    
        public void nextQuestion() {
            currentQuestionIndex++;
        }
    
        public boolean isLastQuestion() {
            if (currentQuestionIndex == mcqs.length) {
                return true;
            } else {
                return false;
            }
        }
    
        public int getCurrentQuestionIndex() {
            return currentQuestionIndex;
        }
    
        public Question getCurrentQuestion() {
            return mcqs[currentQuestionIndex];
        }
    
        public void setSelectedAnswer(int ans) {
            mcqs[currentQuestionIndex].setSelected(ans);
        }
    
        public void setPic(String p) {
            pic = p;
        }
    
        public String getPic() {
            return pic;
        }
    
        public String getResult() {
            int score = 0;
            int j = 0;
            for (int i = 0; i < mcqs.length; i++) {
                if (mcqs[i].getCorrect() == mcqs[i].getSelected()) {
                    score++;
                }
                if (mcqs[i].getCorrect() != mcqs[i].getSelected());
                mcqs[i] = mcqs[j];
                mcqs[j].getQuestionText();
            }
            result = "You've answered  " + "\t" + score + " questions correctly" +
                    "\n You've chosen the wrong answer(s) for the following question(s)" + "\n" + mcqs[j].getQuestionText();
    
            return result;
        }//end of Quiz Class
    }
    the result method is the part that I need to know how cos this will be used in the main application program.

    I really hope someone can give me some pointers. Am very lost. Tks.
    Last edited by Fubarable; 08-11-2009 at 07:55 PM. Reason: code tags added for readability

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if (mcqs[i].getCorrect() != mcqs[i].getSelected());
    why ";" present?

  3. #3
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default

    Thanks so much for taking the time to help me like this.

    I have changed the code as follows :-

    Java Code:
     public String getResult() {
            int score = 0;
            int j = 0;
            for (int i = 0; i < mcqs.length; i++) {
                if (mcqs[i].getCorrect() == mcqs[i].getSelected()) {
                    score++;
                if (mcqs[i].getCorrect() != mcqs[i].getSelected()){
                mcqs[i] = mcqs[j];
                mcqs[j].getQuestionText();
                }
                result = "You've answered  " + "\t" + score + " questions correctly" +
                    "\n You've chosen the wrong answer(s) for the following question(s)" + "\n" 
                    + mcqs[j].getQuestionText();
                }}
            return result;
        }}
    However, when I run it, the result is blank. I wanted it to appear the question text for that particular question which is in my array.

    Do you mind show me how to change my code in the way that will do that?

    Thanks in advance.
    Last edited by Fubarable; 08-13-2009 at 07:27 PM. Reason: Code tags added for readability -- please don't forget to use them!

  4. #4
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default

    Hi, I thought I better post up the front part of the quiz as well so that you may advise me how to write the code which will draw out the wrong questions in text.

    import javax.swing.*;

    public class Question {//Question Class
    public static final int NOT_SELECTED = -1;
    public static final int NUMOFCHOICES = 4;

    private int category;
    private String questionText;
    private String[] choices = new String[NUMOFCHOICES];
    private int correct;
    private int selected;
    private int wrong;

    //constructor
    Question(int category, String questionText, String c1, String c2, String c3, String c4,
    int correct) {

    this.category = category;
    this.questionText = questionText;
    choices[0] = c1;
    choices[1] = c2;
    choices[2] = c3;
    choices[3] = c4;
    this.correct = correct;
    this.selected = NOT_SELECTED;
    }

    public int getCategory() {
    return category;
    }

    public String getQuestionText() {
    return questionText;
    }

    public String[] getChoices() {
    return choices;
    }

    public String getChoicesStr() {
    String s = "";
    for(int i = 0 ; i < choices.length ; i++) {
    s += "(" + (i + 1) + ") " + choices[i] + "\n";
    }
    return s;
    }


    public int getCorrect() {
    return correct;
    }

    public void setSelected(int selected) {
    this.selected = selected;
    }

    public int getSelected() {
    return selected;
    }
    }
    //end of Question class

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello,

    One problem I see is the location of where your creating your result String. Proper indentation will point out to you much more easily that it's not sitting in the right location with reference to your if blocks and for loops:
    Java Code:
      public String getResult() {
        int score = 0;
        int j = 0;
        String result = "";
        for (int i = 0; i < mcqs.length; i++) {
          if (mcqs[i].getCorrect() == mcqs[i].getSelected()) {
            score++;
            if (mcqs[i].getCorrect() != mcqs[i].getSelected()) {
              mcqs[i] = mcqs[j];
              mcqs[j].getQuestionText();
            }
            result = "You've answered  "
                + "\t"
                + score
                + " questions correctly"
                + "\n You've chosen the wrong answer(s) for the following question(s)"
                + "\n" + mcqs[j].getQuestionText();
          }
        }
        return result;
      }
    You want to change this so that the result string is created only after you've iterated through all questions -- after the for loop.

    Hope this helps. Also, I placed code tags in your post number 3 to aid readability and will leave it to you to place them in post number four (please see my signature). Please do so, since it makes reading your code and thus understanding your question much easier to do.

  6. #6
    Kareng is offline Member
    Join Date
    Aug 2009
    Posts
    35
    Rep Power
    0

    Default

    I have tried your method. However, it doesn't give the desired output.

    May be I didn't say it correctly.

    The program is supposed to let the user choose the category, thereafter, select the questions, there are 5 questions.

    N so, it will tells you what is your score and tells you in that category, which questions you got wrong.

Similar Threads

  1. how can I draw a board in 2d array?
    By java_fun2007 in forum New To Java
    Replies: 8
    Last Post: 12-28-2011, 06:58 PM
  2. Simple array questions
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 02-15-2009, 06:57 AM
  3. Java Question, i need to be answered
    By Sunshine in forum New To Java
    Replies: 7
    Last Post: 04-28-2008, 02:00 PM
  4. If you have a question that was answered to your satisfaction,
    By CaptainMorgan in forum Java Software
    Replies: 0
    Last Post: 03-29-2008, 01:55 PM
  5. questions about using array to store profile
    By hien_NU in forum New To Java
    Replies: 6
    Last Post: 01-08-2008, 06:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •