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 that calls the addText meth

ID: 669008 • Letter: P

Question

Provide a different implementation of ChoiceQuestion that calls the addText method that has been added to the Question class. Your ChoiceQuestion class should not store an array list of choices.

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 class that contains methods for text and aswer private members aaccessors and mutators .

//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  
   /**
   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)
   {
       //Call the method addLine with choice that is added to
       //the Question .
       //Add number to the addLine with choice
       addLine(choice);
      
       //Checking if the correct is true then
       //add choice to the setAnswer method
       //with choice method
       if(correct==true)
           //set choice for setAnswer method
           setAnswer(choice);
      
   }//end of the addChoice method
}


-------------------------------------------------------------------
//Tester program for Question and ChoiceQuestion
//class
//QuestionDemo.java
import java.util.Scanner;

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

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

    
      //set choice question for question for question2
      ChoiceQuestion question = new ChoiceQuestion(
         "In which country was the inventor of Java born?");
      //add choices of the questions
      question.addChoice("Australia", false);
      question.addChoice("Canada", true);
      question.addChoice("Denmark", false);
      question.addChoice("United States", false);
      quiz[1] = question;

      //Create an object of Scanner class
      Scanner in = new Scanner(System.in);
    
      for (Question q : quiz)
      {
          //print the question
         q.display();
       
         System.out.println("Your answer: ");
         //prompt for user answer as string
         String response = in.nextLine();
         //call checkAnswer with user response
         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?
Australia
Canada
Denmark
United States
Your answer:
Canada
true

Hope this helps you

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