Problem ( Please provide all the steps and comments in the code ) (a) Modify the
ID: 3921527 • Letter: P
Question
Problem ( Please provide all the steps and comments in the code )
(a) Modify the checkAnswer method of the Question class discussed in class so that it
does not take into account different spaces or upper/lowercase characters. For example, the
response "JAMES gosling" should match an answer of "James Gosling".
(b) Add a class NumericQuestion to the Question hierarchy discussed in class. If the
response and the expected answer differ by no more than 0.01, then accept the response as
correct.
(c) Add a class FillInQuestion to the Question hierarchy discussed in class. Such a
question is constructed with a string that contains the answer, surrounded by _ _, for
example, “The father of the Java programming language was born in _Canada_.”. The
question should be displayed as
The father of the Java programming language was born in ________.
Explanation / Answer
(a) The program for the above is:
public class CheckAnswer {
public class Question {
private String text;
private String answer;
public Question() {
text = ""; // Here we are entering the text//
answer = "";
}
public void setText(String questionText) {
text = questionText;
}
public void setAnswer(String correctResponse) {
answer = correctResponse;
}
public boolean checkAnswer(String response) {
return response.toLowerCase().equals(answer.toLowerCase());
}
public void display() {
System.out.println(text);
}
}
}
(b)
public class NumericQuestion extends Question {
private double answer;
public void setAnswer(double correctResponse) {
answer = correctResponse;
}
public boolean checkAnswer(String response)
{
double responseDouble = Double.parseDouble(response);
return Math.abs(responseDouble - answer) <= 0.01;
}
}
(c)
public class FillInQuestion extends Question {
public void setText(String questionText) {
Scanner parser = new Scanner(questionText);
parser.useDelimiter("_");
String question = parser.next();
String answer = parser.next();
question += " " + parser.next();
parser.close();
super.setText(question);
super.setAnswer(answer);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.