QuestionDemo2.java.html

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