ASSIGNMENT: Add a class FillInQuestion1 to the question hierarchy of section 9.
ID: 3549350 • Letter: A
Question
ASSIGNMENT:
Add a class FillInQuestion1 to the question hierarchy of section 9. Such a question is constructed witha string that contains the answer, surrounded by _ _, for example, "The inventor of java was _James Gosling_". The question should be displayed as The inventor of java was ____
CODE:
here is the Question super class (provided by book): http://notepad.cc/womeavi79
here is the FIllInQuestion1 sub class (extends Question class) that I made: http://notepad.cc/ribiaxu25
here is the tester class that I made to test the FillInQuestion1 class: http://notepad.cc/glecotu11
When I tried to run it, the console window came up with this message in red:
Exception in thread "main" java.lang.NullPointerException
at FillInQuestion1.<init>(FillInQuestion1.java:8)
at FillInQuestionTester.main(FillInQuestionTester.java:4)
Explanation / Answer
// OUTPUT
The last month of the year is _______
Answer: December
true
The first president of the United States was _______
Answer: keka
false
3 x 5 is _______
Answer: 15
true
The capital of Texas is _______
Answer: Austin
true
//
import java.util.Scanner;
/**
A question with a text and an answer.
*/
public class Question
{
private String text;
private String answer;
/**
Constructs a question with empty question and answer.
*/
public Question()
{
text = "";
answer = "";
}
/**
Sets the question text.
@param questionText the text of this question
*/
public void setText(String questionText)
{
text = questionText;
}
/**
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.equalsIgnoreCase(answer);
}
/**
Displays this question.
*/
public void display()
{
System.out.println(text);
}
}
public class FillInQuestion1 extends Question {
private String answerToQuestion;
private String enteredText;
private String theQuestion;
public FillInQuestion1(String questionWithAnswer){
enteredText = questionWithAnswer;
int questionBegin = questionWithAnswer.indexOf("_");
int questionEnd = questionWithAnswer.lastIndexOf("_");
String questionBegin + 1);// + "___" + questionWithAnswer.substring(questionEnd);
String>
answerToQuestion = onlyAnswer;
theQuestion = onlyQuestion;
setAnswer(onlyAnswer);
setText(onlyQuestion);
}
public void display(){
System.out.println(theQuestion + "______");
}
public boolean checkAnswer(String response){
return response.equals(answerToQuestion);
}
}
public class FillInQuestionTester {
public static void main(String[] args){
FillInQuestion1 FillInQuestion1("The last month of the year is _December_");
FillInQuestion1 two = new FillInQuestion1("The first president of the United States was _George Washington_");
FillInQuestion1 three = new FillInQuestion1("3 x 5 is _15_");
FillInQuestion1 four = new FillInQuestion1("The capital of Texas is _Austin_");
presentQuestion(one);
presentQuestion(two);
presentQuestion(three);
presentQuestion(four);
}
public static void presentQuestion(FillInQuestion1 q){
q.display();
System.out.print("Answer: ");
Scanner in = new Scanner(System.in);
String response = in.nextLine();
System.out.println(q.checkAnswer(response));
}
}