QuestionDemo3.java.html

  1  import java.util.Scanner;
  2  
  3  /**
  4     This program shows a simple quiz with two question types.
  5  */
  6  public class QuestionDemo3
  7  {
  8     public static void main(String[] args)
  9     {
 10        Question first = new Question();
 11        first.setText("Who was the inventor of Java?");
 12        first.setAnswer("James Gosling");      
 13  
 14        ChoiceQuestion second = new ChoiceQuestion();
 15        second.setText("In which country was the inventor of Java born?");
 16        second.addChoice("Australia", false);
 17        second.addChoice("Canada", true);
 18        second.addChoice("Denmark", false);
 19        second.addChoice("United States", false);
 20  
 21        presentQuestion(first);
 22        presentQuestion(second);
 23     }
 24  
 25     /**
 26        Presents a question to the user and checks the response.
 27        @param q the question
 28     */
 29     public static void presentQuestion(Question q)
 30     {
 31        q.display();
 32        System.out.print("Your answer: ");
 33        Scanner in = new Scanner(System.in);
 34        String response = in.nextLine();
 35        System.out.println(q.checkAnswer(response));
 36     }
 37  }
 38