Question
Okie dokie so this assignment has the following instructions (in total probably around 30 minutes of coding, im just lost on a few steps): Instructions
Okie dokie so this assignment has the following instructions (in total probably around 30 minutes of coding, im just lost on a few steps):
Instructions
Start with provided files.Question.java
Implements question class with basic functionality to set question text, set answer and check if the value passed as answer represents correct answer or not.
There is no need to update code in this file
QuestionTester.java
test class for methods in Question.java
You need to add more code to this file but there is no need to remove any existing code
Implement two classesChoiceQuestion class with below functionality
Inherit Question class. This will make Question class methods available to ChoiceQuestion class. 5 points
Represents multiple choice question. Use ArrayList to store multiple answer choices 5 points
Implement method addChoice that allows adding answers text along with a Boolean parameter indicating whether the answer is true or false. If the flag passed is true, set the answer text passed as true. 20 points
Implement display method to print all answer choices 20 points
TrueFalseQuestion class with below functionality
Inherit ChoiceQuestion class. This allows reuse of addChoice method 5 points
Implement setQuestion method so that
it uses already implemented setText method 5 points
internally calls addChoice two times (one for true answer and second for false answer) 10 points
Submit your code for Question / Choice Question from the in-class work and add the true/false type question class that uses Choice Question class as superclass.
Add one more test method testTrueFalseQuestionTwo to test the question with false as right answer. And here are the supplied .java files: Question.java
/**
* @author {add name}
* Description: 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 paramQuestionText) {
text = paramQuestionText;
}
/**
* 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);
}
}
Question Tester.java
import java.util.Scanner;
public class QuestionTester {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
//testQuestion();
//testChoiceQuestion();
testTrueFalseQuestionOne();
}
private static void testQuestion() {
Question q = new Question();
q.setText("Which keyword is used when method doesn't
return anything?");
q.setAnswer("void");
presentQuestion(q);
}
private static void testChoiceQuestion()
{
//ChoiceQuestion cq = new ChoiceQuestion();
//cq.setText("Which one of the below keyword is used for
implementng subclass?");
//cq.addChoice("instanceof", false);
//cq.addChoice("extends", true);
//cq.addChoice("super", false);
//presentQuestion(cq);
}
private static void testTrueFalseQuestionOne()
{
//TrueFalseQuestion tq = new TrueFalseQuestion();
//tq.setQuestion("String is NOT a primitive data type",
true);
//presentQuestion(tq);
}
private static void presentQuestion(Question q){
q.display();
System.out.print("Your answer: ");
String response = in.nextLine();
System.out.println(q.checkAnswer(response));
}
}
ChoiceQuestion.java
import java.util.ArrayList;
//use extends keyword to inherit Question class
public class ChoiceQuestion {
//private ArrayList choices;
public ChoiceQuestion() {
}
public void addChoice(String choice, boolean correct){
}
public void display(){
}
}
TrueFalseQuestion.java
//inherit ChoiceQuestion class
public class TrueFalseQuestion extends ChoiceQuestion {
public void setQuestion(String text, boolean b) {
//
//
//
// addChoice("False", !b);
}
}
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