Back to 111 Main Page

Mobius strip

Assignment 11

Task

Extend an existing Quizzer program to provide a basic graphical user interface and to add extra features.

Details:

Quizzer

The Quizzer program reads in a plain text file of questions and associated answers. It poses each question in turn to the user, and prompts them for their answer. It then tells the user whether they selected the correct answer. If they were incorrect, the Quizzer tells them what the right answer was. The program continues until it has asked all the questions from the file. Quizzer is currently text-based.

Here are the necessary files:

GraphicalQuizzer

Your job is to write a GraphicalQuizzer. It should do everything Quizzer does, only it will use JOptionPanes (rather than Scanner and System.out.println) in order to provide a basic graphical interface.

In addition, it will:

  • Let the user quit in the middle of a quiz
  • Initially prompt the user for which question file they want to read from
  • Keep track of how many questions the user answers, and how many of those are correct answers
  • Keep an ongoing record of scores for completed quizzes
  • Let the user play again, looping through as many quizzes as the user wishes to play
  • Always show a "Thank you for playing!" message before quitting.

Suggestions

This is a slightly complicated program, and you will need to apply everything you've learned so far in this course--including conditionals, loops, and arrays. You'll also need to catch exceptions and do some file output.

First of all, study the Quizzer program. This is to practice reading another person's code. In particular, note what instance variables a Quizzer has and how the run and main method work. Also check out how toString creates a single string containing multiple lines. You may want to try creating your own questions text file and running your own quiz.


Next, you can start writing GraphicalQuizzer. Since it does everything a Quizzer does, GraphicalQuizzer should extend Quizzer. Then you want to override the run and main methods.

To start with, your run and main should do basically the same thing as Quizzer's. But you want to replace all System.out.printlns and the Scanner with approprate JOptionPanes (from the javax.swing package). You should use the showInputDialog to read in strings, and showMessageDialog for output when you're not also asking for input (such as when reporting error messages).

Because the question window disappears after the user enters an answer, you should include the question (and answer list) again when you display whether the user's answer was correct or not. If incorrect, tell them what the correct answer was.

Also, if the showInputDialog returns null, it means the user hit cancel or closed the input window. In this case, the run method should end.

Using two new instance variables, keep track of how many questions the user answers, and how many questions the user answers correctly. (You'll need this information later.)


Now you are done with the run method. Next you should expand main (or, better yet, create a new static method) to do the following.

Start by asking the user to enter the name of a questions file to load. (If they cancel this dialog, the program should end.) Use this entered filename to create a new GraphicalQuizzer instance and invoke run on it. (You will need to put a try/catch block around the call to run in order to catch any exceptions to do with file problems and report the error.)

After running the quiz, tell the user how many questions they answered (out of the total number of questions) and how many they answered correctly (out of those they answered).

Next, if they answered all the questions in the quiz, open the scores file. (Use a static constant to hold this filename.) Then ask the user for their name. If they give you one (that is, their name != null), then append a line to the scores file. This line should include: the name of the questions file used, the number of questions answered correctly out of the total number of questions, and then the name entered by the user. These three elements should be separated by tabs ("\t") and don't forget the newline ("\n"). Here an example line as printed in the file:

coding.txt	2/3	Fred

Don't forget to close the file to be sure everything gets written.

Now build a loop around all of this so that after each quiz you can ask the user if they would like to play again. Use a YES/NO confirmation dialog for this.

You program should always show a "Thanks for playing!" message before it quits.

What to Submit

Attach your GraphicalQuizzer.java file to an email. (Don't change any of the other files given above; I will use my own quizzes with your program.)

FAQ

Where's the sample code from lab that demonstrates the use of JOptionPanes, parsing Strings to integers, and writing/appending to a file?
Here: AgeCalculator.java.
When I try to compile the code given above, I get errors about the compiler source level on the strange for loop and the <angle brackets> used with ArrayList.
You need to change your compiler source level. Go to Window -> Preferences, Java -> Compiler, and then change the Compiler Compliance level to at least 5.0.
Is there an easy way to create a YES/NO dialog box for the play again question?
Sure, something like this:
  boolean playAgain;
  ...
  int again = JOptionPane.showConfirmDialog(null, "Play again?", "Confirm",
                                            JOptionPane.YES_NO_OPTION);
  if (again == JOptionPane.YES_OPTION) {
    keepPlaying = true;
  }else {
    keepPlaying = false;
  }
This is not required, though; you can just use an input dialog if you prefer.

Grading

Out of 12 points (with 2 points of extra credit):

1 - Submission
Follows required submission policies.
1 - Coding standards
3 - run method
As Quizzer, but graphical (nothing printed to the console). Runs through each question in file, reporting correct or incorrect user response in a readable way (includes question in report). Also reports error if user enters words or invalid numbers (question does not need to be shown in this message) and repeats the question.
1 - run can be aborted
If the user closes a question input dialog, run ends.
2 - Asks which file to load/run before each quiz
Loads the user-specified file; reports errors in file. If dialog is canceled, program ends.
2 - Reports user performance
States how many questions out of total answered, and how many correct of those answered.
2 - Scores file
Appends the quiz name, number of questions correct, and the user's name to a standard score file. (Only does this if user answered all questions in a quiz, and doesn't cancel the request for their name.)
+1 - Asks user if they want to play again
If so, successfully loops again, starting with a request for a filename to load
+1 - Program always reports "Thanks for playing" at end


~ztomasze Index : TA Details: ICS111: Assignment 11
http://www2.hawaii.edu/~ztomasze
Last Edited: 23 Nov 2007
©2007 by Z. Tomaszewski.