Add a class NumericQuestion to the question hierarchy of Section 9.1. If the res
ID: 3572612 • Letter: A
Question
Add a class NumericQuestion to the question hierarchy of Section 9.1. If the response and the expected answer differ by no more than 0.01, then accept the response as correct.
I add a class like this but the second question's result is wrong. To satisfy upper conditon, how can I fix it?
import java.util.Scanner;
public class Question
{
private String text;
private String answer;
public Question()
{
text="";
answer="";
}
public static void presentQuestion(Question q)
{
q.display();
System.out.print("Your answer: ");
Scanner in =new Scanner(System.in);
String response =in.nextLine();
System.out.println(q.checkAnswer(response));
}
public void setText(String questionText)
{
text=questionText;
}
public void setAnswer(String correctResponse)
{
answer=correctResponse;
}
public boolean checkAnswer(String response)
{
return response.replaceAll("\s+","").toLowerCase().equals(answer.replaceAll("\s+","").toLowerCase());
}
public void display()
{
System.out.println(text);
}
}
Question java D NumericQuestion,java 23 D Question Tester java 2 public class NumericQuestion extends Question 3 private double answer; 6e public void setAnswer (double correct Response) answer Correct Response 10e public boolean checkAnswer (String response) 12 double response Double Double. parseDouble (response) return Math abs (responseDouble answer)Explanation / Answer
The reason is the second question you mentioned:
second.setAnswer("3.141593") // this is string which actually calls the string argument function of question class.
Change:
Question second=new Question();
second.setText("pi?");
second.setAnswer("3.141592");
To:
NumericQuestion second=new NumericQuestion();
second.setText("pi?");
second.setAnswer(3.141592);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.