Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective Demonstrate the following: Usage of Eclipse ArrayList Object based programming Java building blocks for Inheritance (use of extends and super keywords) Exception Handling Testing

Objective

Demonstrate the following:

Usage of Eclipse

ArrayList

Object based programming

Java building blocks for Inheritance (use of extends and super keywords)

Exception Handling

Testing

Javadoc comments

Instructions

Start with provided files.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

java

Extends Exception class and calls its constructor with the provided message.java

test class for methods in Question.java 10 points

Throw Custom exception in checkInput() 10 points

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.

Total Possible Points: 100

Program runs as submitted - 10 points

ChoiceQuestion class is implemented 50 points

TrueFalseQuestion class is implemented 10 points

Implement checkInput() method 10 points

New test method with FALSE as correct answer is implemented 10 points

Code is well documented using Javadocs - 10 points

Provided files

Question.java

/**

* @author {add name}

* Description: A Question with a text and an answer.

*/

import java.util.ArrayList;

abstract public class Question {

private String text;

private String answer;

/**

* Constructs a question with empty question and answer.

*/

public Question() {

text = "";

answer = "";

}

public Question(String q, String a) {

text = q;

answer = a;

}

/**

* 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);

}

/**

* Abstract method to be implemented for Choice Questions.

*/

abstract ArrayList getChoices();

}

SimpleQuestion.java

import java.util.ArrayList;

public class SimpleQuestion extends Question{

public SimpleQuestion() {

}

public SimpleQuestion(String q, String a) {

super(q, a);

}

@Override

ArrayList getChoices() {

// TODO Auto-generated method stub

return null;

}

}

ChoiceQuestion.java

import java.util.ArrayList;

public class ChoiceQuestion extends Question{

ArrayList choices;

public ChoiceQuestion() {

super();

choices = new ArrayList();

}

public void addChoice(String choice, boolean correct){

// add choices and the correct answer

}

public void display(){

//displays question text and answer choices

}

ArrayList getChoices() {

// return ArrayList choices

}

}

TrueFalseQuestion.java

// inherits ChoiceQuestion class

public class TrueFalseQuestion extends ChoiceQuestion {

public void setQuestion(String text, boolean b) {

// set Question text

// add True False answer choices

// addChoice("False", !b);

}

}

CustomException.java

//use extends keword to inherit Exception class and calls constructor

public class CustomException extends Exception{

public CustomException(String message) {

super(message);

}

}

QuestionTester.java

import java.util.Scanner;

import java.util.ArrayList;

public class QuestionTester {

private static Scanner in = new Scanner(System.in);

private static ArrayList questions;

public static void main(String[] args) {

questions = new ArrayList();

testQuestion();

testChoiceQuestion();

testTrueFalseQuestionOne();

presentQuestions(questions);

}

private static void testQuestion() {

SimpleQuestion q = new SimpleQuestion();

q.setText("Which keyword is used when method doesn't

return anything?");

q.setAnswer("void");

questions.add(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);

// questions.add(cq);

}

private static void testTrueFalseQuestionOne()

{

// TrueFalseQuestion tq = new TrueFalseQuestion();

// tq.setQuestion("String is NOT a primitive data type",

true);

// questions.add(tq);

//add more True-False questions

}

private static void presentQuestions(ArrayList q){

boolean done = false;

int i=0;

while(!done){

try {

while (i

q.get(i).display();

System.out.print("Your

answer: ");

String response =

in.nextLine();

checkInput(q.get(i),

response);

System.out.println(q.get(i).checkAnswer(response)+" ");

i++;

}

done = true;

}

catch(CustomException ce) {

System.out.println(ce.getMessage());

}

}

System.out.println(" Thank you for completing the

questionaire");

}

private static void checkInput(Question ques, String ans) throws

CustomException{

/**If question type is SimpleQuestion, check if answer is

empty. If it is empty,

throw new CustomException with the message

"Please submit an answer"

otherwise, check if answer is within the

given range of choices(1, 2, 3, ...),If not,

throw new CustomException with the message

"Input given doesn't match choice list"

**/

if((ques.getClass().getName()).equals("SimpleQuestion"))

{

if (ans.isEmpty()){

// throw new CustomException(" Please

submit an answer.");

}

}else {

ArrayList choices =

ques.getChoices();

// implement here as descrbed above

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

User Defined Tensor Data Analysis

Authors: Bin Dong ,Kesheng Wu ,Suren Byna

1st Edition

3030707490, 978-3030707491

More Books

Students also viewed these Databases questions