Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Provide a different implementation of ChoiceQuestion. Instead of storing the cho

ID: 3667017 • Letter: P

Question

Provide a different implementation of ChoiceQuestion. Instead of storing the choices in an array list, the addChoice method should add the choice to the question text. For this purpose, an addLine method has been added to the Question class.

Use the following files:

Question.java

QuestionDemo.java

Complete the following file:

ChoiceQuestion.java

/**
A question with multiple choices.
*/
public class ChoiceQuestion extends Question
{
// Add any needed instance variables, but don't store the choices
// The choices should be added to the text of the superclass

/**
Constructs a choice question with a given text and no choices.
@param questionText the text of this question
*/
public ChoiceQuestion(String questionText)
{
. . .
}

/**
Adds an answer choice to this question.
@param choice the choice to add
@param correct true if this is the correct choice, false otherwise
*/
public void addChoice(String choice, boolean correct)
{
. . .
}
}

Explanation / Answer

//Question.java
/**
   A question with a text and an answer.
*/
public class Question
{
   private String text;
   private String answer;

   /**
      Constructs a question with a given text and an empty answer.
      @param questionText the text of this question
   */
   public Question(String questionText)
   {
      text = questionText;
      answer = "";
   }

   /**
      Sets the answer for this question.
      @param correctResponse the answer
   */
   public void setAnswer(String correctResponse)
   {
      answer = correctResponse;
   }

   /**
      Checks a given response for correctness.
      @param response the response to check
      @return true if the response was correct, false otherwise
   */
   public boolean checkAnswer(String response)
   {
      return response.equals(answer);
   }

   /**
      Add a line of text to the question text.
   */
   public void addLine(String line)
   {
      text += " " + line;
   }

   /**
      Displays this question.
   */
   public void display()
   {
      System.out.println(text);
   }
}

------------------------------------------------------------------------------------------------------------

/**
   Modified ChoicQuestion class with multiple choices.
*/
public class ChoiceQuestion extends Question
{
  
   // Add any needed instance variables, but don't store the choices
       // The choices should be added to the text of the superclass
  
   //Add a static variable for the question number and set to 1
       private static int number=0;
   /**
   Constructs a choice question with a given text and no choices.
   @param questionText the text of this question
   */
   public ChoiceQuestion(String questionText)
   {
       //Calls the super class Question constructor
       //with questionText
       super(questionText);
      
   }

   /**
   Adds an answer choice to this question.
   @param choice the choice to add
   @param correct true if this is the correct choice, false otherwise
   */
   public void addChoice(String choice, boolean correct)
   {
       number++;
       //Call the method addLine with choice that is added to
       //the Question .
       //Add number to the addLine with choice
       addLine(number+": "+choice);      
       //Checking if the correct is true then
       //add choice to the setAnswer method
       //with choice method
       if(correct==true)
           //set the answer for true
           setAnswer(String.valueOf(number));
   }
}

------------------------------------------------------------------------------------------------------------

/**The java program that prompts user to
* enter answer for the quiz question then
* print if answer is correct otherwise print
* false. Then prompt a question that contains
* four options to choose. After user enters his
* option then the program prints true if correct
* false otherwise*/
//QuestionDemo.java
import java.util.Scanner;

public class QuestionDemo
{
   public static void main(String[] args)
   {
       //Create an array of type Question of size,2
      Question[] quiz = new Question[2];

      //Set question
      quiz[0] = new Question("Who was the inventor of Java?");
      //set answer
      quiz[0].setAnswer("James Gosling");    

      //Create an instance of ChoiceQuestion
      ChoiceQuestion question =
              new ChoiceQuestion("In which country was the inventor of Java born?");
      //Call addChoice method to add options for the questions
      question.addChoice("Australia", false);
      question.addChoice("Canada", true);
      question.addChoice("Denmark", false);
      question.addChoice("United States", false);
      quiz[1] = question;

      Scanner in = new Scanner(System.in);
      //Print question and prompt for answer from user
      for (Question q : quiz)
      {
         q.display();
         System.out.println("Your answer: ");
         //prompt for user answer.
         String response = in.nextLine();
         //Check the answer and print boolean value
         System.out.println(q.checkAnswer(response));    
      }
   }
}

------------------------------------------------------------------------------------------------------------

Sample Output:

Who was the inventor of Java?
Your answer:
James Gosling
true
In which country was the inventor of Java born?
1: Australia
2: Canada
3: Denmark
4: United States
Your answer:
2
true

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote