QuestionQuestion

Idea:
We have all had to take quizzes, whether for fun or as a requirement (ex: this course). In this assignment you will implement a simple multiple choice quiz, using good object-oriented programming style. The user will be shown some questions that he / she will answer. After completing all of the questions, the user will find out how many he / she got right and wrong. He / she will also then be shown the correct answers and the average scores for all users, followed by a sorted list of the questions. Note: Information on the individual players will not be stored – see extra credit options.

Execution Details:
When your program is started it will read a number of questions from a text file. The name of the file must be entered on the command line when the program is executed. Each question will consist of the following parts:
1)    The question itself, in sentence form on a single line
2)    An integer, N, indicating how many answers are given (also on a single line)
3)    The actual N answers, 1 per line
4)    An integer, K, indicating which of the answers is correct (also on a single line)
5)    An integer, T, indicating how many times this question was tried (also on a single line)
6)    An integer, C, indicated how many times this question was answered correctly (also on a single line)
There could be any number of questions in the file, and your program will not know how any in advance. Thus, you must be able to check for the end of the file in a reasonable way. You must read these questions into an ArrayList of type Question. All of your questions CANNOT have the same number of possible answers [i.e. the value for 2) above must be different in at least two of your questions]. When you first create your file, items 5) and 6) should be 0 for every question. However, as people take the quiz these values will be updated and saved back into the file. You must write and use class Question in a good object oriented way. More details on Question will be given in the Implementation Details and Hints. I will put a SAMPLE file of questions online, but for this assignment you are REQUIRED to make up your own question file with at least 5 questions in it.

Once your questions have been read in you will display them to the user one at a time and allow the user to answer them by typing in the numbers of the correct answers. However, you should NOT show the user the answers and his/her results until the entire quiz has been completed. After each question has been answered, simply accept the answer and proceed to the next question (storing the user's guess within each Question object). Answers outside the valid range should be rejected (and user must re-enter).

After all questions have been tried you must show the user his/her results in the following way:
*      For each item in the Question ArrayList, do the following
–    Display the question to the user
–    Display the correct answer to the user
–    Display the user's guess back to the user
–    Indicate whether the user got the question right or wrong
*      After all of the individual results have been displayed, show the user his / her overall score (percent of questions correct)
*      Next, show the following cumulative statistics:
–    Times right, times wrong and percentage right for each question
–    Easiest question and its percentage right
–    Hardest question and its percentage right

Before ending the program, update the question file to reflect the results from the current user. Make sure the format is exactly the same as it was, so the same file can be used multiple times in your program.
Implementation Details and Hints:
*      You must write a class called Question to represent a single quiz question. This class must minimally include a String for the question itself, an array of Strings for the possible answers, an int to indicate the correct answer and two ints to store the number of times tried and number of times correct. Note that the array of Strings to store the possible answers must be sized in the constructor, since it can vary from question to question.   You may find that you need additional instance variables as well. You will also need various methods in your class – think about what access is needed and how it must be implemented.
*      You must store your Questions in an ArrayList and access the questions via the ArrayList. This ArrayList should NOT be within the Question class since the Question class represents a single Question. Rather it should be in your main (or perhaps another class if you prefer). See handout ArrayL.java for some hints on using an ArrayList.
*      The file access for this program is again fairly straightforward. The file is formatted in such a way as to make the input and extraction of the data fairly simple. However, note that some of the data read in is String data and some is int data – be careful to handle these types appropriately.
*      To find the easiest and hardest questions you will need to compare Questions based on their percentage right. Thus you will need an accessor in the Question class to return the percentage right for that question.
*      Don't forget to comment your code and to submit your Assignment Information Sheet with your other files.
*      Make sure you submit all of your files (including data files) so the TA can run your program.
*      To see an example of how your program execution might look, see this sample execution.
*      As with the first two assignments, you can earn extra credit if you exceed the assignment specifications in approved ways. Some possibilities include:
– Allow for different types of questions in your quiz (ex: fill in the blank and T/F). These will require your Question class to be more complicated and may (esp. with the fill in the blanks) require more thought in the grading. They will also require the interface to be different (since the user is not simply entering the index of an answer for each question).
– Allow the user more than one guess for a question and give a hint after an incorrect guess. If you do this, you should also keep more detailed statistics (ex: percentage correct on first guess, percentage correct on second guess).
– After the quiz is finished, sort the questions based on percentage of users who got them correct, showing them from highest percentage down to lowest percentage.
– Store information on the users as well as the questions and have users log in before taking the quiz.
If you have another idea, ask me about it before doing it.

Sample output:
assig3 > java Assig3 sample.txt
Welcome to the Quiz Program! Good Luck!

Question 0:
How many licks does it take to get to the tootsie roll center of a tootsie pop?
Answers:
0: one
1: two
2: three
3: four

Your answer? (enter a number): -2
Your answer? (enter a number): 5
Your answer? (enter a number): 2

Question 1:
What is your name?
Answers:
0: Arthur, King of the Britons
1: Sir Lancelot the Brave
2: Sir Robin the Not-Quite-So-Brave-As-Sir Lancelot

Your answer? (enter a number): 2

Question 2:
Who's on first?
Answers:
0: What
1: Why
2: Because
3: Who
4: I don't know

Your answer? (enter a number): 3

Question 3:
Which of the following is a terror of the fire swamp?
Answers:
0: Lightning sand
1: Flame spurt
2: R.O.U.S.
3: All of the above

Your answer? (enter a number): 1

Question 4:
Who is the all-time greatest pilot?
Answers:
0: Manfred von Richthofen
1: Chuck Yeager
2: Hiraku Sulu
3: Luke Skywalker
4: Kara Thrace
5: Charles Lindbergh

Your answer? (enter a number): 4

Thanks for your answers!
Here are your results:

Question: How many licks does it take to get to the tootsie roll center of a tootsie pop?
Answer: three
Player Guess: three
Result: CORRECT! Great Work!

Question: What is your name?
Answer: Arthur, King of the Britons
Player Guess: Sir Robin the Not-Quite-So-Brave-As-Sir Lancelot
Result: INCORRECT! Remember the answer for next time!

Question: Who's on first?
Answer: Who
Player Guess: Who
Result: CORRECT! Great Work!

Question: Which of the following is a terror of the fire swamp?
Answer: All of the above
Player Guess: Flame spurt
Result: INCORRECT! Remember the answer for next time!

Question: Who is the all-time greatest pilot?
Answer: Kara Thrace
Player Guess: Kara Thrace
Result: CORRECT! Great Work!

Your overall performance was:
Right: 3
Wrong: 2
Pct: 0.6

Here are some cumulative statistics:
Question: How many licks does it take to get to the tootsie roll center of a tootsie pop?
Times Tried: 14
Times Correct: 5
Percent Correct: 35.7%
Question: What is your name?
Times Tried: 14
Times Correct: 6
Percent Correct: 42.9%
Question: Who's on first?
Times Tried: 14
Times Correct: 7
Percent Correct: 50.0%
Question: Which of the following is a terror of the fire swamp?
Times Tried: 14
Times Correct: 4
Percent Correct: 28.6%
Question: Who is the all-time greatest pilot?
Times Tried: 14
Times Correct: 9
Percent Correct: 64.3%

Easiest Question:
Question: Who is the all-time greatest pilot?
Times Tried: 14
Times Correct: 9
Percent Correct: 64.3%
Hardest Question:
Question: Which of the following is a terror of the fire swamp?
Times Tried: 14
Times Correct: 4
Percent Correct: 28.6%

assig3 > cat sample.txt
How many licks does it take to get to the tootsie roll center of a tootsie pop?
4
one
two
three
four
2
14
5
What is your name?
3
Arthur, King of the Britons
Sir Lancelot the Brave
Sir Robin the Not-Quite-So-Brave-As-Sir Lancelot
0
14
6
Who's on first?
5
What
Why
Because
Who
I don't know
3
14
7
Which of the following is a terror of the fire swamp?
4
Lightning sand
Flame spurt
R.O.U.S.
All of the above
3
14
4
Who is the all-time greatest pilot?
6
Manfred von Richthofen
Chuck Yeager
Hiraku Sulu
Luke Skywalker
Kara Thrace
Charles Lindbergh
4
14
9

Sample file:
How many licks does it take to get to the tootsie roll center of a tootsie pop?
4
one
two
three
four
2
14
5
What is your name?
3
Arthur, King of the Britons
Sir Lancelot the Brave
Sir Robin the Not-Quite-So-Brave-As-Sir Lancelot
0
14
6
Who's on first?
5
What
Why
Because
Who
I don't know
3
14
7
Which of the following is a terror of the fire swamp?
4
Lightning sand
Flame spurt
R.O.U.S.
All of the above
3
14
4
Who is the all-time greatest pilot?
6
Manfred von Richthofen
Chuck Yeager
Hiraku Sulu
Luke Skywalker
Kara Thrace
Charles Lindbergh
4
14
9

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Assignment3 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
       if (args.length != 1) {            
            System.out.println("assig3 > java Assig3 sample.txt");
            System.exit(0);
       }
       ArrayList<Question> questions = new ArrayList<>();
       if ( readFile(args[0], questions) == true) {
            processAnswer(questions);
            printResult(questions);
            easiestAndHardest(questions);
            writeFile(questions, args[0]);
       }
    }
   
    private static boolean easiestAndHardest(ArrayList<Question> questions){
       if (questions == null ||
                questions.isEmpty()) {
            return false;
       }
       Question maxPct = questions.get(0);
       Question minPct = questions.get(0);
       for (Question question : questions) {
            if (question.pct() < maxPct.pct()) {
                maxPct = question;
            } else if (question.pct() > minPct.pct()) {
                minPct = question;
            }
       }
      
       System.out.println("Easiest Question: ");
       System.out.println(minPct.statistics());
       System.out.println("Hardest Question: ");
       System.out.println(maxPct.statistics());
      
       return true;
    }...
$33.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats