NOTE: The completed code must pass in the following compiler. Please make absolu
ID: 3573829 • Letter: N
Question
NOTE: The completed code must pass in the following compiler. Please make absolutely sure it does before posting: http://codecheck.it/codecheck/files?repo=bj4fp&problem=ch10/c10_exp_10_9
PLEASE also make sure of the following:
-Post this as text, not as an image.
-Avoid making alterations to the original code unless necessary.
-Post the passing output- from the this compiler specifically- as evidence this code was accepted and passed.
~
Provide toString and equals methods for the Question and ChoiceQuestion classes of How To 10.1.
Use the following class as your tester class:
Complete the following files:
ChoiceQuestion.java
import java.util.ArrayList;
/**
A question with multiple choices.
*/
public class ChoiceQuestion extends Question
{
private ArrayList<String> choices;
/**
Constructs a choice question with a given text and no choices.
@param questionText the text of this question
*/
public ChoiceQuestion(String questionText)
{
super(questionText);
choices = new ArrayList<String>();
}
/**
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)
{
choices.add(choice);
if (correct)
{
// Convert choices.size() to string
String choiceString = "" + choices.size();
setAnswer(choiceString);
}
}
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));
}
}
}
~~~~~~~~~
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);
}
/**
Displays this question.
*/
public void display()
{
System.out.println(text);
}
}
Explanation / Answer
ChoiceQuestion.java
public class ChoiceQuestion extends Question
{
private ArrayList<String> choices;
/**
Constructs a choice question with a given text and no choices.
@param questionText the text of this question
*/
public ChoiceQuestion(String questionText)
{
super(questionText);
choices = new ArrayList<String>();
}
/**
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)
{
choices.add(choice);
if (correct)
{
// Convert choices.size() to string
String choiceString = "" + choices.size();
setAnswer(choiceString);
}
}
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));
}
}
public String toString(){
return super.toString()+choices;
}
}
Question.java
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);
}
/**
Displays this question.
*/
public void display()
{
System.out.println(text);
}
public String toString(){
return getClass().getName()+"[text="+text+",answer="+answer;
}
}
Output:
Question[text=Who was the inventor of Java?,answer=James Gosling
Expected: Question[text=Who was the inventor of Java?,answer=James Gosling]
false
Expected: false
ChoiceQuestion[text=In which country was the inventor of Java born?,answer=2[Australia, Canada, Denmark, United States]
Expected: ChoiceQuestion[text=In which country was the inventor of Java born?,answer=2][choices=[Australia, Canada, Denmark, United States]]
true
Expected: true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.