1 import java.util.ArrayList;
2
3 /**
4 A question with multiple choices.
5 */
6 public class ChoiceQuestion extends Question
7 {
8 private ArrayList<String> choices;
9
10 /**
11 Constructs a choice question with a given text and no choices.
12 @param questionText the text of this question
13 */
14 public ChoiceQuestion(String questionText)
15 {
16 super(questionText);
17 choices = new ArrayList<String>();
18 }
19
20 /**
21 Adds an answer choice to this question.
22 @param choice the choice to add
23 @param correct true if this is the correct choice, false otherwise
24 */
25 public void addChoice(String choice, boolean correct)
26 {
27 choices.add(choice);
28 if (correct)
29 {
30 // Convert choices.size() to string
31 String choiceString = "" + choices.size();
32 setAnswer(choiceString);
33 }
34 }
35
36 public void display()
37 {
38 // Display the question text
39 super.display();
40 // Display the answer choices
41 for (int i = 0; i < choices.size(); i++)
42 {
43 int choiceNumber = i + 1;
44 System.out.println(choiceNumber + ": " + choices.get(i));
45 }
46 }
47 }
48