0

My program reads question details from a QuestionBank.sql file. Everything is right but instead of getting 12 questions the output contains 10 questions.

The Output is:

GK Simple
GK Simple
GK Medium
GK Complex
Science Complex
History Medium
History Medium
History Simple
History Simple
Geography Medium

**DataManagerImpl.java**

@Override
    public Set<Question> generateQuestionPaper(List<Question> list,
            List<Criteria> template) {
        // TODO Auto-generated method stub
        Set<Question> questionSet = new HashSet<Question>();
        int count;
        int index = 0;
        for(Criteria c: template){
            count = 0;
            while(c.getNoOfQuestion() > count){
                index = (int)(Math.random()*list.size());
                //System.out.println(index);
                Question q = list.get(index);
                if(c.getCategory().equals(q.getCategory()) && c.getComplexity().equals(q.getComplexity()) ){
                    if(!questionSet.contains(q)){
                        count++;
                        questionSet.add(q);
                        System.out.println(q.getCategory()+" "+q.getComplexity());
                    }

                }                   
            }
        }
        return questionSet;
    }

Criteria.java

public class Criteria {

private Category category;
private Complexity complexity;
private int noOfQuestion;

public Criteria() {
}

public Criteria(Category category, Complexity complexity,int noOfQuestion) {
    super();
    this.noOfQuestion = noOfQuestion;
    this.category = category;
    this.complexity = complexity;
}

public int getNoOfQuestion() {
    return noOfQuestion;
}

public void setNoOfQuestion(int noOfQuestion) {
    this.noOfQuestion = noOfQuestion;
}

public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

public Complexity getComplexity() {
    return complexity;
}

public void setComplexity(Complexity complexity) {
    this.complexity = complexity;
}

}

List template contains: (Passed as parameter to generateQuestionpaper() enter image description here

Please help me!!

chresse
  • 5,486
  • 3
  • 30
  • 47
  • It looks like the `Question` hashcode/equals implementation is vital, please provide that code. – weston May 31 '16 at 12:44
  • i have not overrided hashcode and equals method because i can't understand how these two method can solve my problem? – Abhishek Jha Jun 01 '16 at 05:07

1 Answers1

0

Problem is with the definition of Math.random() method.

Try after modifying your code as below -

Random random = new Random();
for(Criteria c: template){
        count = 0;
        while(c.getNoOfQuestion() > count){
            index = random.nextInt(list.size());

As, list index is also zero based this should work fine.

Community
  • 1
  • 1
Tirath
  • 2,294
  • 18
  • 27