Exam Question Questions for the exam Answers chosen Create e

Exam Question Questions for the exam Answers chosen Create exam given Question\'s Display a question Choose answer for a question Return exam score (each question worth 1 point) public class Question private String question private List answers; choices for answer private int correct; index of correct answer public Question (String ques List ansrs) public boolean check (int answer) true if answer is co @Override public string tostring The above is an example of how much you need to write for \"supporting\" classes in H2

Solution

Question.java

import java.util.Iterator;
import java.util.List;

public class Question {

   private String question;
   private List<String> answers;
   private int correct;
  
   public Question(String que, List<String> ansrs) {
      
       this.question = que;
       this.answers = ansrs;
   }
  
   public boolean check(int answer){
       if(answer == correct){
           return true;
       }
       return false;
   }
  
   @Override
   public String toString(){
      
       String output = question+\"\ \";
       Iterator ite = answers.iterator();
       output += \"Options: \ \";
       while(ite.hasNext()){
           output += ite.next()+\"\ \";
       }
       return output;      
   }
  
   public void setCorrect(int correct){
       this.correct = correct;
   }
}

TestQuestion.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestQuestion {

   public TestQuestion() {

   }

   public static void main(String[] args) throws NumberFormatException, IOException {
      
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       System.out.print(\"How many questions you want to set:\");
      
       int totQuestions = Integer.parseInt(br.readLine());
       ArrayList<Question> questions = new ArrayList<Question>(totQuestions);
       Question ques;
       List ansrs;
       for(int i=0;i<totQuestions;i++){
           System.out.print(\"Please enter the question:\");
           String question = br.readLine();
           System.out.println(\"Please enter the options seperated by space(\' \'):\");
           ansrs = new ArrayList();
           String[] answers = br.readLine().split(\" \");
           for(int j=0;j<answers.length;j++){
               ansrs.add(answers[j]);
           }
           ques = new Question(question, ansrs);
           System.out.print(\"Please enter the index of correct answer:\");
           ques.setCorrect(Integer.parseInt(br.readLine()));
           questions.add(ques);
       }
       System.out.println(\"*******All questions are set. Please enter Yes to begin the test:******\");
       String response = br.readLine();

int marks = 0;
       if(response.equalsIgnoreCase(\"yes\")||response.equalsIgnoreCase(\"y\")){
           Iterator ite = questions.iterator();
           Question que;
           while(ite.hasNext()){
               que = (Question) ite.next();
               System.out.println(que.toString());
               System.out.print(\"Please enter the answer:\");
               if(que.check(Integer.parseInt(br.readLine()))){
                   marks++;
                   System.out.println(\"Correct!!\ \");
               }
               else{
                   System.out.println(\"Wrong answer!!\ \");
               }
           }
       }
       else{
           System.out.println(\"Thank You!\");
       }
   }

}

Output:

How many questions you want to set:3
Please enter the question:how players in cricket?
Please enter the options seperated by space(\' \'):
10 11 12 13
Please enter the index of correct answer:2
Please enter the question:how many player in vollyball?
Please enter the options seperated by space(\' \'):
7 10 9 11
Please enter the index of correct answer:3
Please enter the question:Who is President of America?
Please enter the options seperated by space(\' \'):
Obama Trump Bush None
Please enter the index of correct answer:2
*******All questions are set. Please enter Yes to begin the test:******
y
how players in cricket?
Options:
10
11
12
13

Please enter the answer:2
Correct!!
how many player in vollyball?
Options:
7
10
9
11

Please enter the answer:4
Wrong answer!!
Who is President of America?
Options:
Obama
Trump
Bush
None

Please enter the answer:2
Correct!!


Your Total Marks: 2

 Exam Question Questions for the exam Answers chosen Create exam given Question\'s Display a question Choose answer for a question Return exam score (each quest
 Exam Question Questions for the exam Answers chosen Create exam given Question\'s Display a question Choose answer for a question Return exam score (each quest
 Exam Question Questions for the exam Answers chosen Create exam given Question\'s Display a question Choose answer for a question Return exam score (each quest

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site