      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 no choices.

     12     */

     13     public ChoiceQuestion()

     14     {

     15        choices = new ArrayList<String>();

     16     }

     17

     18     /**

     19        Adds an answer choice to this question.

     20        @param choice the choice to add

     21        @param correct true if this is the correct choice, false otherwise

     22     */

     23     public void addChoice(String choice, boolean correct)

     24     {

     25        choices.add(choice);

     26        if (correct)

     27        {

     28           // Convert choices.size() to string

     29           String choiceString = "" + choices.size();

     30           setAnswer(choiceString);

     31        }

     32     }

     33

     34     public void display()

     35     {

     36        // Display the question text

     37        super.display();

     38        // Display the answer choices

     39        for (int i = 0; i < choices.size(); i++)

     40        {

     41           int choiceNumber = i + 1;

     42           System.out.println(choiceNumber + ": " + choices.get(i));

     43        }

     44     }

     45  }

     46