Question: Using classes Question.java, MultipleChocie.java, and FreeResponse.java you are going to write a class called Quiz.java. The quiz class has one instance variable: an ArrayList of

Using classes Question.java, MultipleChocie.java, and FreeResponse.java you are going to write a class called Quiz.java. The quiz class has one instance variable: an ArrayList of Question objects. Your class will have one constructor and one method called takeQuiz.

Constructor: The constructor will take no parameters, and will interact with the user, inputting the questions. This will be a complex constructor that does much more than just assign variables.

Enter the text of the question:

What color is the sky?

Is your question an MC? Y or N

Y

How many choices are there?

4

Enter choice text:

red

Is it correct? Y or N

N

Enter choice text:

green

Is it correct? Y or N

N

Enter choice text:

blue

Is it correct? Y or N

Y

Enter choice text:

yellow

Is it correct? Y or N

N

Do you want to input another question? Y or N

Y

Enter the text of the question:

What is 2+2?

Is your question an MC? Y or N

N

Enter the correct answer to your question:

4

Do you want to input another question? Y or N

N

Your quiz is built.

One thing that youll need to know: in order to read input that is more than one word you will need to do in.next() + in.nextLine() in order to properly read the entire line.

TakeQuiz: this method allows the user to take their quiz and gives them a percentage score. If the question is a Multiple Choice question, the user will only enter one letter as the answer, so you must just use in.next() to get the answer. If the question is a FreeResponse the user could enter multiple words so you must use in.next() + in.nextLine(); to read in the answer.

1. What color is the sky?

A. red

B. green

C. blue

D. yellow

Answer: C

Correct!

2. What is 2+2?

Answer: 2

Wrong!

You scored: 50.0 %

Main: Main should instantiate the class, and call takeQuiz outputting the returned value.

/* AP CS * Quiz Program */ import java.util.*; public class Quiz { private ArrayList questions; public Quiz() { } public double takeQuiz() { //return percentage score } public static void main(String[] args) { Quiz q = new Quiz(); System.out.println("You scored: " + q.takeQuiz()); } }

/* Free Response * AP CS A */ import java.util.*; public class FreeResponse extends Question { private String correctAns; public FreeResponse(String text, String correct) { super(text); correctAns = correct.toLowerCase(); } public boolean checkAnswer(String answ) { return answ.toLowerCase().equals(correctAns); } }

/* MultipleChoice * AP CS A */ import java.util.*; public class MultipleChoice extends Question{ private String correctAns; private ArrayList choices; MultipleChoice(String text){ super(text); correctAns = null; choices = new ArrayList(); } //add choice to the ArrayList choices and set answer public void addChoice(String choice, boolean correct){ String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String letter = alpha.substring(choices.size(), choices.size() + 1); if (correct) correctAns = letter; choices.add(letter + ". " + choice); } public boolean checkAnswer(String choice) { return choice.toUpperCase().equals(correctAns); } public void display(){ super.display(); for (String ans: choices) System.out.println(ans); } } 
/* Question.java * AP CS A */ public class Question { private String text; public static int numQuestions = 0; //constructor with parameters public Question(String text){ this.text = text; Question.numQuestions++; } //display the question public void display(){ System.out.print(Question.numQuestions + ". "); System.out.println(text); } public static void resetQuestionNum(){ numQuestions = 0; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!