Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Simple JAVA program, Please fill in the bolded italicized part. import java.util.ArrayList; import java.util.StringTokenizer ; import java.util.Scanner; /** This class tests the AnyCorrectChoiceQuestion subclass. Your

Simple JAVA program, Please fill in the bolded italicized part.

import java.util.ArrayList;

import java.util.StringTokenizer ;

import java.util.Scanner;

/**

This class tests the AnyCorrectChoiceQuestion subclass.

Your work is in the todo region in AnyCorrectChoiceQuestion.java.

Nothing to change here.

*/

public class AnyCorrectChoiceTester

{

public static void main(String[] args)

{

Question[] quiz = new Question[1];

AnyCorrectChoiceQuestion question = new AnyCorrectChoiceQuestion(

"Which of the following is a country of Africa? (More than one answer may be correct).");

question.addChoice("Bosnia", false);

question.addChoice("Algeria", true);

question.addChoice("Crete", false);

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

question.addChoice("United Arab Emirates", false);

question.addChoice("Tunisia", true);

question.addChoice("Libya", true);

question.addChoice("Morocco", true);

question.addChoice("Zambia", true);

question.addChoice("Saudi Arabia", false);

question.addChoice("Malawi", true);

quiz[0] = question;

String response ;

Question q ;

q = quiz[0] ;

q.display();

response = "1" ;

System.out.println(response + "?");

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

System.out.println(false + " WAS EXPECTED.") ;

response = "2" ;

System.out.println(response + "?");

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

System.out.println(true + " WAS EXPECTED.") ;

response = "11" ;

System.out.println(response + "?");

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

System.out.println(true + " WAS EXPECTED.") ;

}

}

/**

Question.java

A question with a text and an answer.

This is an example in Big Java.

*/

class Question

{

//instance variables

private String text;

private String answer;

/**

Constructs a question with a default question and answer

*/

public Question()

{

text = "1 + 1 = " ;

answer = "2" ;

}

/**

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;

}

/**

Gets the answer for this question.

@return the correct answer

*/

public String getAnswer()

{

return answer ;

}

/**

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

}

/**

Sets the question text

@param questionText the text of this question

*/

public void setQuestion(String questionText)

{

text = questionText;

}

/**

Displays this question.

*/

public void display()

{

System.out.println(text);

}

}

/**

AnyCorrectChoiceQuestion.java

A question with multiple choices and multiple correct answers. The question

looks like:

Which of the following is a country of Africa? (More than one answer may be correct)."

1. Bosnia

2. Algeria

3. Crete

4. United States

5. United Arab Emirates

6. Tunisia

The respondent should provide any one of the correct choices

as a number string, like "2".

The answer string which is in the superclass Question

should contain all of the correct choices, separated by spaces,

like "2 6" ;

For example, here is how the class is used:

AnyCorrectChoiceQuestion question = new AnyCorrectChoiceQuestion(

"Which of the following is a country of Africa? (More than one answer may be correct).");

question.addChoice("Bosnia", false);

question.addChoice("Algeria", true);

question.addChoice("Crete", false);

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

question.addChoice("United Arab Emirates", false);

question.addChoice("Tunisia", true);

question.addChoice("Libya", true);

question.addChoice("Morocco", true);

question.addChoice("Zambia", true);

question.addChoice("Saudi Arabia", false);

question.addChoice("Malawi", true);

In this case, the question variable in Queston will have the value:

"Which of the following is a country of Africa? (More than one answer may be correct)."

and the list choices in AnyCorrectChoiceQuestion will have all

the possible choices:

["Bosnia", "Algeria", "Crete", "United States",

"United Arab Emirates", "Tunisia", "Libya", "Morocco",

"Zambia", "Saudi Arabia", "Malawi"]

and the answer string in Question will have the indices (plus 1)

of the correct choices:

"2 6 7 8 9 10"

*/

class AnyCorrectChoiceQuestion extends Question

{

private ArrayList choices;

/**

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

@param questionText the text of this question

*/

public AnyCorrectChoiceQuestion(String questionText)

{

super(questionText);

choices = new ArrayList();

}

/**

Adds an answer choice to this question.

It adds to the list of choices. For example if choices is currently

["Bosnia", "Algeria", "Crete", "United States", "United Arab Emirates"]

and the call is addChoice("Tunisia", true), then choices becomes

["Bosnia", "Algeria", "Crete", "United States", "United Arab Emirates",

"Tunisia"],

and the answer string which previously was "2", becomes

"2 6", since we are recording that the sixth choice, Tunisia, is

correct.

@param choice the choice to add

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

*/

public void addChoice(String choice, boolean correct)

{

//-----------Start below here. To do: approximate lines of code = 5

// 1. add choice to choices

//2. if this choice is correct

//3. getAnswer to find the current answer string

//4. append to answer the number of this choice and

//maybe a blank before and after

//according to its position in the list, counting from 1

// 5. setAnswer

//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

}

/**

Display the question text and the answer choices.

*/

public void display()

{

// Display the question text

super.display();

// Display the answer choices

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

int choiceNumber = i + 1;

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

}

}

/**

Override the checkAnswer from Question class.

Checks a given response for correctness, where the answer

string has possibly multiple correct answers. For example

if the answer string is "2 4 5"

and the response is "4"

then the method should return true.

But if the answer string is "2 11"

and the response is "1"

then the method should return false.

@param response the response to check

@return true if the response was correct, false otherwise

*/

public boolean checkAnswer(String response)

{

//-----------Start below here. To do: approximate lines of code = 2

// 1. Fill in this method

//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.

}//closing checkAnswer

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions