Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

description Design a class hierarchy used to represent test papers that would be

ID: 3824855 • Letter: D

Question

description Design a class hierarchy used to represent test papers that would be given in school. It should include at least the following: Question Each question has: points Number of points earned for answering the question correctly. difficulty Range of difficulty ranging from the constants MIN_DIFFICULTY to MAX_DIFFICULTY. answerSpace Amount of space that should be left on the paper so that the test taker can fill in the answer. This can be represented as the number of lines on the page. questionText The text of the question, e.g., "How much wood would a woodchuck chuck if a woodchuck could chuck wood?" 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). ObjectiveQuestion 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: points Number of points earned for answering the question correctly. difficulty Range of difficulty ranging from the constants MIN_DIFFICULTY to MAX_DIFFICULTY. answerSpace Amount of space that should be left on the paper so that the test taker can fill in the answer. This can be represented as the number of lines on the page. questionText The text of the question, e.g., "How much wood would a woodchuck chuck if a woodchuck could chuck wood?" correctAnswer The correct answer to the question. 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. FillInTheBlankQuestion 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: points Number of points earned for answering the question correctly. difficulty Range of difficulty ranging from the constants MIN_DIFFICULTY to MAX_DIFFICULTY. answerSpace Amount of space that should be left on the paper so that the test taker can fill in the answer. This can be represented as the number of lines on the page. questionText The text of the question, e.g., "How much wood would a woodchuck chuck if a woodchuck could chuck wood?" correctAnswer The correct answer to the question. 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. MultipleChoiceQuestion A MultipleChoiceQuestion is a kind of question in which there are multiple possible solutions but only one correct solution. Each MultipleChoiceQuestion has: points Number of points earned for answering the question correctly. difficulty Range of difficulty ranging from the constants MIN_DIFFICULTY to MAX_DIFFICULTY. answerSpace Amount of space that should be left on the paper so that the test taker can fill in the answer. This can be represented as the number of lines on the page. MultipleChoiceQuestions can be answered in only one line. questionText The text of the question, e.g., "How much wood would a woodchuck chuck if a woodchuck could chuck wood?" possibleAnswers A list of possible answers, only one of which is correct. correctAnswer The correct answer among the possibleAnswers. 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? Peter Griffin Scooby Doo Spongebob Squarepants Eric Cartman and in an answer key, could look something like: Who lives in a pineapple under the sea? Peter Griffin Scooby Doo **** Spongebob Squarepants **** Eric Cartman Test Each test has: questions a list of questions of any or all of the types previously described. totalPoints the sum of the points of each question 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. Driver 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. Extra Credit Send to a File (+10 points) Add the ability to send a test and its answer key to files whose names are determined at run time. Test Bank (+20 points) Implement a TestBank class, which has: a collection of questions the ability to generate a Test with a given number of questions chosen randomly from the collection of questions. The ability to read a collection of questions from and write them to files. Reading questions from files is the most difficult part of the TestBank and it will be the source of most of the extra credit points. What to submit Submit a zip file that includes all of your .java files, and if you've completed the extra credit part of the assignment, provide your example input files.

Explanation / Answer

TestBank.java


import java.io.*;
import java.util.*;
public class TestBank {

    public static void main(String[] args) throws FileNotFoundException {
        PrintWriter outfile = new PrintWriter (args[0]);//takes first word from command line argument as name of new file
        ObjectiveQuestion q1 = new ObjectiveQuestion("Who Who, Who Who", 5, 5, 3, "Who are you?");//sample new question
        Question[] test2 = makeQuiz(1,1,1,1);//creates array test2 that contains one of each type of question
        Test test1 = new Test(test2);//creates object with ^ array of chosen questions
      
        outfile.println(test1.toQuiz());    //creates a test on the file chosen at runtime
        outfile.println(" ******************************************* ");
        outfile.println(test1.toKey());    //creates a key
        outfile.println("Total possible points: " + test1.totalPoints());//prints out total point value of test1
        outfile.println(q1.toKey());
        //outfile.close();
     
        System.out.println(test1.toQuiz());    //prints sample to screen
        System.out.println(" ******************************************* ");
        System.out.println(test1.toKey());  
        System.out.println("Total possible points: " + test1.totalPoints());//prints out total point value of test1
        System.out.println(q1.toKey());//prints indivudal question to demonstrate indivudal methods
      
        //reads WechtHW9Test (a test bank) to create a question.
        Scanner in = new Scanner(new File("WechtHW9Test.text"));//creates scanner from test bank called WechtHW9Test
        Scanner kbd = new Scanner(System.in);
        int points =0;
        int difficulty = 0;
        int answerSpace = 0;
        String questionText ="";
        String answer = "";
        System.out.println("Please type how many questions you would like to pull from the test bank. Max of 11 as of now.");
        int numQuestions =kbd.nextInt();
        Question[] test3Array = new Question[numQuestions];//new array of input size that will be filled with question bank questions

   for(int i =0; i<numQuestions; i++) {//runs through test bank grabing questions for the new test array
            points = in.nextInt();
            difficulty = in.nextInt();
            answerSpace = in.nextInt();
            in.nextLine();
            questionText = in.nextLine();
            answer = in.nextLine();
            test3Array[i] = new ObjectiveQuestion (answer, points,difficulty,answerSpace,questionText);
   }
        Test test3 = new Test(test3Array);//creates object with ^ array of chosen questions
        System.out.println(test3.toQuiz());
        outfile.println(test3.toKey());
        System.out.println(test3.totalPoints());//prints new test/point value to screen and key to file file .
        outfile.close();
    }
  
    //method to return an array of the given number of each type of question
    //each question is the default, however they can be overwritten to include other questions
    public static Question[] makeQuiz(int numOfquestion, int numOfobjectivequestion, int numOffillintheblankquestion, int numOfmultiplechoicequestion ){
        int totalQuestions = numOfquestion + numOfobjectivequestion + numOffillintheblankquestion + numOfmultiplechoicequestion;//total num of questions
        Question [] test1 = new Question [totalQuestions];//array of questions
        for(int i =0; i<=totalQuestions; i++){//runs for how many questions to be turned
            if(numOfquestion > 0){
                test1[i] = new Question();//initilizes array[0] to a default question
                numOfquestion--;//takes one question off num needed to make
            }
            else if(numOfobjectivequestion > 0){
                test1[i] = new ObjectiveQuestion();//default objective question
                numOfobjectivequestion--;
            }
            else if(numOffillintheblankquestion > 0){
                test1[i] = new FillInTheBlankQuestion();
                numOffillintheblankquestion--;
            }
            else if(numOfmultiplechoicequestion > 0){
                test1[i] = new MultipleChoiceQuestion();
                numOfmultiplechoicequestion--;
            }
        }
        return test1;
    }
}

Question.java


class Question {
    int points;
    int difficulty;
    int answerSpace;
    String questionText;

    //general constructor
    public Question(int points, int difficulty, int answerSpace, String questionText) {
        this.points = points;
        this.difficulty = difficulty;
        this.answerSpace = answerSpace;
        this.questionText = questionText;
    }
  
    //default constructor
    public Question(){
        points = 5;
        difficulty = 1;
        answerSpace = 5;
        questionText = "What is the answer to life the universe and everything?";
    }

    //prints all aspects of the string
    public String toString() {
        return "points = " + points + ", difficulty=" + difficulty + ", answerSpace=" + answerSpace + ", questionText=" + questionText;
    }
  
    //prints the question in quiz format
    public String toQuiz(){
       String a = questionText;//sets one string as the question
       for(int i = 0; i <= answerSpace; i++)
           a += " ";//adds one new line per answerSpace to leave room to write
       return a;
    }
  
    //prints the key for this question, which tells teachers to grade
    public String toKey(){
        String a = this.toQuiz();
        a += " ****Correct response needs grading**** ";
        return a;
    }
}

class ObjectiveQuestion extends Question {
    String correctAnswer;

    //standard constructor
    public ObjectiveQuestion(String correctAnswer, int points, int difficulty, int answerSpace, String questionText) {
        super(points, difficulty, answerSpace, questionText);
        this.correctAnswer = correctAnswer;
    }
  
    //default constructor
    public ObjectiveQuestion(){
        this.correctAnswer = "42";
    }

    //prints the question with the correct response below
    public String toKey() {
        String a = this.toQuiz();
        a += " Correct response: " + correctAnswer + " ";
        return a;
    }
}

class FillInTheBlankQuestion extends ObjectiveQuestion {

    //new default question
    public FillInTheBlankQuestion(){
        this.questionText = "I feel        .";
        this.correctAnswer = " pretty";
    }

    //prints out key with word in the blank if blank is larger than the correct response
    public String toKey(){
        String spaces = "";
        String questionCopy= "";
      
        for(int i = 0; i< correctAnswer.length(); i++)//adds the length of the answer to a blank string
            spaces += " ";
      
        for (int i = 0; i <questionText.length(); i++)//creates copy of string
            questionCopy += questionText.charAt(i);
      
        return questionCopy.replace(spaces, correctAnswer) + " "; //replaces spaces with the response and returns
    }
}

class MultipleChoiceQuestion extends ObjectiveQuestion {
    String [] possibleAnswers;
  
    //default constructor
    public MultipleChoiceQuestion(){
        this.possibleAnswers = new String[] {"42", "Me","Blue","I give up"};
    }
  
    //new constructor
    public MultipleChoiceQuestion(String[] possibleAnswers, String correctAnswer, int points, int difficulty, int answerSpace, String questionText) {
        super(correctAnswer, points, difficulty, answerSpace, questionText);
        this.possibleAnswers = possibleAnswers;
    }
  
    //new print quiz
    public String toQuiz(){
       String a = questionText + " ";//sets one string as the question
       for(int i = 0; i <= possibleAnswers.length-1; i++)
           a += " " + "     " + (i+1) + ": " + possibleAnswers[i];//adds one new line and possible choice
     
       return a;
    }
}

class Test {
    Question [] fullQuiz;//passes an array of questions into format them to a quiz or key or points value
  
    //constructor
    public Test (Question [] fullQuiz){
        this.fullQuiz = fullQuiz;
    }
  
    //returns string of a quiz for the array passed in
    public String toQuiz(){
        String a = "";
        for(int i =0; i <fullQuiz.length; i++)
            a += (i+1) + ": " + fullQuiz[i].toQuiz();//runs its toQuiz function
        return a;
    }
  
    //returns string of key
    public String toKey(){
        String key = "";
        for(int i =0; i <fullQuiz.length; i++)
            key += (i+1) + ": " + fullQuiz[i].toKey();//runs its toQuiz function          
        return key;
    }
    //returns total point value
    public int totalPoints(){
        int x = 0;
        for(int i =0; i <fullQuiz.length; i++)
            x+= fullQuiz[i].points;
        return x;
    }
}