seabass
11-14-2010, 05:08 AM
For class NumericQuestion Add setAnswer, a method that takes a double as input, converts it to a string and
calls setAnswer in the superclass Question to store the answer.
o Add checkAnswer, a method that takes an answer in string form, converts it to a
double, gets the correct answer in string form from Question, converts it to a
double, and returns true if they are within 0.01 of each other and false otherwise.
For class quiz Change main so that all three questions are asked. presentQuestion that prints out the correct answer, only when the
answer is wrong
public class NumericQuestion {
private String text;
private Double answer;
/**
* Constructs a question with empty question and answer.
*/
public NumericQuestion() {
text = "";
answer = 3.1416;
}
/**
* 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(Double 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);
}
}
import java.util.Scanner;
public class Quiz {
public static void main(String[] args)
{
Question first = new Question();
first.setText("What inherits data and behavior from a superclass?");
first.setAnswer("subclass");
ChoiceQuestion second = new ChoiceQuestion();
second.setText("Which modifies the object on which it operates in some way?");
second.addChoice("Accessor", false);
second.addChoice("Mutator", true);
second.addChoice("Method", false);
second.addChoice("Variable", false);
NumericQuestion third = new NumericQuestion();
third.setText("What is the numeric value for PI?");
third.setAnswer(3.1416);
presentQuestion(first);
presentQuestion(second);
//presentQuestion(third);
}
/**
Presents a question to the user and checks the response.
@param q the question
*/
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));
}
}
calls setAnswer in the superclass Question to store the answer.
o Add checkAnswer, a method that takes an answer in string form, converts it to a
double, gets the correct answer in string form from Question, converts it to a
double, and returns true if they are within 0.01 of each other and false otherwise.
For class quiz Change main so that all three questions are asked. presentQuestion that prints out the correct answer, only when the
answer is wrong
public class NumericQuestion {
private String text;
private Double answer;
/**
* Constructs a question with empty question and answer.
*/
public NumericQuestion() {
text = "";
answer = 3.1416;
}
/**
* 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(Double 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);
}
}
import java.util.Scanner;
public class Quiz {
public static void main(String[] args)
{
Question first = new Question();
first.setText("What inherits data and behavior from a superclass?");
first.setAnswer("subclass");
ChoiceQuestion second = new ChoiceQuestion();
second.setText("Which modifies the object on which it operates in some way?");
second.addChoice("Accessor", false);
second.addChoice("Mutator", true);
second.addChoice("Method", false);
second.addChoice("Variable", false);
NumericQuestion third = new NumericQuestion();
third.setText("What is the numeric value for PI?");
third.setAnswer(3.1416);
presentQuestion(first);
presentQuestion(second);
//presentQuestion(third);
}
/**
Presents a question to the user and checks the response.
@param q the question
*/
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));
}
}