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

Hello! I need help with my Java assignment. This is what we were told to do: Pro

ID: 669022 • Letter: H

Question

Hello! I need help with my Java assignment. This is what we were told to do:

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

-------This is what I got so far-----

/**
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

public String questionText;
public String choice;
public boolean correct;

/**
Constructs a choice question with a given text and no choices.
@param questionText the text of this question
*/
public ChoiceQuestion(String 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)
{
this.addLine(choice);

if(correct == true)
{
setAnswer(choice);
}


}
}

Thank you!

Explanation / Answer

/Program:

//Note: Modified code is shown in bold

// QuestionDemo.java

import java.util.Scanner;

public class QuestionDemo

{

   public static void main(String[] args)

   {

      Question[] quiz = new Question[3];

      quiz[0] = new Question("Who was the inventor of Java?");

      quiz[0].setAnswer("James Gosling");    

   

      ChoiceQuestion question = new ChoiceQuestion(

         "In which country was the inventor of Java born?");

      question.addChoice("Australia", false);

      question.addChoice("Canada", true);

      question.addChoice("Denmark", false);

      question.addChoice("United States", false);

      quiz[1] = question;

      ChoiceQuestion newQuestion = new ChoiceQuestion(

         "Which person got two Oscar Awards for Music?");

      newQuestion.createMultilineQuestion("Which country he belongs to?");

      newQuestion.addChoice("A.R.Rahman,India", true);

      newQuestion.addChoice("Haris,Paris", false);

      newQuestion.addChoice("Rahman,London", false);

      newQuestion.addChoice("Yuvan,Australia", false);

      quiz[2] = newQuestion;

     

      Scanner in = new Scanner(System.in);

      for (Question q : quiz)

      {

         q.display();

         System.out.println("Your answer: ");

         String response = in.nextLine();

         System.out.println(q.checkAnswer(response));     

     }

   }

}

// 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

   public String questionText;

   public String choice;

   public boolean correct;

   /**

      Constructs a choice question with a given text and no choices.

      @param questionText the text of this question

   */

   public ChoiceQuestion(String questionText)

   {

      super(questionText);

   }

  

   /**

      Constructs a multiple line choice question by appending the Line of text

      with the existing question in questionText parameter

     * @param Line the line of text to append

   */

   public void createMultilineQuestion(String Line)

   {

       super.addLine(Line); // calling super class addLine method to add text

   }

   /**

      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)

   {

      this.addLine(choice);

      if(correct == true)

        {

        setAnswer(choice);

        }

   }

}

// 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);

   }

}

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:

United States

false

Which person got two Oscar Awards for Music?

Which country he belongs to?

A.R.Rahman , India

Haris, Paris

Rahman, London

Yuvan, Australia

Your answer:

A.R.Rahman, India

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