Question
I am working out the Big Java: Late Objects (1st Edition) book and I am trying to add the problems from chapter 9 1PE and
I am working out the Big Java: Late Objects (1st Edition) book and I am trying to add the problems from chapter 9 1PE and 2PE to my existing program. I need help as I do not understand how to do the problem here is my code for the program so far.
Problem 1 is I am suppose to implement the numericQuestion subclass
Problem 2 is to implement the fillinQuestion subclass
import java.util.Scanner;
/** This program shows a simple quiz with two choice questions. */ public class QuestionDemo2 { public static void main(String[] args) { ChoiceQuestion first = new ChoiceQuestion(); first.setText("What was the original name of the Java language?"); first.addChoice("*7", false); first.addChoice("Duke", false); first.addChoice("Oak", true); first.addChoice("Gosling", false);
ChoiceQuestion second = new ChoiceQuestion(); second.setText("In which country was the inventor of Java born?"); second.addChoice("Australia", false); second.addChoice("Canada", true); second.addChoice("Denmark", false); second.addChoice("United States", false); Question third = new Question(); third.setText("What is the sum of the following numbers: 4 + 4?");
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: "); @SuppressWarnings("resource") Scanner in = new Scanner(System.in); String response = in.nextLine(); System.out.println(q.checkAnswer(response)); } }
/** A question with a text and an answer. */ public class Question { protected String text; protected 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.equals(answer); }
/** Displays this question. */ public void display() { System.out.println(text); } }
public class NumericQuestion extends Question { public NumericQuestion() { } public void checkAnswer(double answer) { double diff = answer-(double)3.1; if(diff<0) { diff=(double)3.1-answer; } if(answer==(double)3.1||diff<=(double)0.01) { setAnswer(answer); System.out.println("Your answer is correct!"); } else { System.out.println("Incorrect answer!"); } //public void display() //{ //super.display(); //} } private void setAnswer(double answer) {} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started