Design a class hierarchy used to represent test papers that would be given in sc
ID: 3825067 • Letter: D
Question
Design a class hierarchy used to represent test papers that would be given in school. It should include at least the following:
Each question has:
For each question, we should be able to create a String representation of the question as it should be presented to the student (for example, during a test or quiz).
An ObjectiveQuestion is a type of question which has a definitive answer. For example, the question 2+3 has the answer 5. (A non-objective question would be one where there's no single correct answer, e.g., something like, "The Civil War was neither civil nor a war. Discuss.".)
Each ObjectiveQuestion has:
For each ObjectiveQuestion, we should be able to create a String representation of the question as it should be presented to the student during a Test. We should also be able to create a String representation of the question which includes the correct answer, such as one that a grader might use when grading a paper.
A fill in the blank question is a kind of question where the correct answer fills in a missing word in the provided text. For example. " _____ was the 16th US President."
Each FillInTheBlankQuestion has:
For each FillInTheBlankQuestion, we should be able to create a String representation of the question as it should be presented to the student during a Test. This should include the blank space. We should also be able to create a String representation of the question which includes the correct answer, such as one that a grader might use when grading a paper.
In a test representation, this might look something like:
______ was the 16th US President.
and in an answer key representation, it might look something like:
___Abraham Lincoln___ was the 16th US President.
A MultipleChoiceQuestion is a kind of question in which there are multiple possible solutions but only one correct solution.
Each MultipleChoiceQuestion has:
For each MultipleChoiceQuestion, we should be able to create a String representation of the question as it should be presented to the student during a Test. We should also be able to create a String representation of the question which includes the correct answer, such as one that a grader might use when grading a paper.
In a test representation, this might look something like:
Who lives in a pineapple under the sea?
and in an answer key, could look something like:
Who lives in a pineapple under the sea?
Each test has:
We should be able to generate a String representation of a Test as well as its answer key. We should also be able to send either of these to a file whose name is determined at runtime.
Write a program that uses your classes in order to generate both a test and an answer key, writing each to the screen and to files chosen at runtime.
Explanation / Answer
package chegg;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
public class TestDriver{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
Test t=new Test();
Question q1=new ObjectiveQuestion(10,5,10,"How many seconds in an hour?", "3600");
Question q2=new FillInTheBlankQuestion(10,3,10,"is the color of rose?", "red");
t.addQuestion(q1);
t.addQuestion(q2);
System.out.println(t);
AnswerKey ak=new AnswerKey(t);
for(Question q: ak.test.questions){
System.out.println(q.QuestionText);
String ans=sc.next();
ak.addAnswer(q, ans);
}
System.out.println(ak);
}
}
class Test{
ArrayList<Question> questions;
int totalPoints;
Test(){
questions=new ArrayList<Question>();
}
Test(ArrayList<Question> questions){
this.questions=questions;
for(int i=0;i<questions.size();i++){
totalPoints+=questions.get(i).point;
}
}
public void addQuestion(Question q){
questions.add(q);
}
@Override
public String toString(){
return "No of question :"+questions.size()+" Total Points :"+totalPoints;
}
}
class AnswerKey{
Test test;
HashMap<Question,String> answers;
AnswerKey(Test t){
test=t;
answers=new HashMap<Question,String>();
}
public void addAnswer(Question q, String ans){
if(q instanceof ObjectiveQuestion){
answers.put(q, ans);
}else if (q instanceof FillInTheBlankQuestion){
answers.put(q, "_"+ans+"_ "+q.QuestionText);
}else if(q instanceof MultipleChoiceQuesion){
answers.put(q, "****"+ans+"****");
}
}
public String toString(){
String anss="";
Iterator it = answers.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
String qa=pair.getKey() + " = " + pair.getValue()+" ";
anss+=qa;
}
return anss;
}
}
class Question{
int point;
int difficulty;
int answerSpace;
String QuestionText;
Question(int point,int diff, int ansSpace, String text){
this.point=point;
difficulty=diff;
answerSpace=ansSpace;
QuestionText=text;
}
@Override
public String toString(){
return QuestionText+" ,point="+point+" ,difficulty="+difficulty+" ,space="+answerSpace;
}
}
class ObjectiveQuestion extends Question{
String correctAnswer;
ObjectiveQuestion(int point,int diff, int ansSpace, String text, String corr){
super(point,diff,ansSpace,text);
correctAnswer=corr;
}
}
class FillInTheBlankQuestion extends Question{
String correctAnswer;
FillInTheBlankQuestion(int point,int diff, int ansSpace, String text, String corr){
super(point,diff,ansSpace,text);
correctAnswer=corr;
}
public String toString(){
return "__ "+QuestionText+" ,point="+point+" ,difficulty="+difficulty+" ,space="+answerSpace;
}
}
class MultipleChoiceQuesion extends Question{
ArrayList<String> possibleAnswers;
String correctAnswer;
MultipleChoiceQuesion(int point,int diff, int ansSpace, String text, String corr){
super(point,diff,ansSpace,text);
correctAnswer=corr;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.