CS2514 Lab 3
Exercise
Write a program that reads in a multiple-choice quiz from a text file; the user takes the quiz
and receives a score.
Here are a sample text file and also your main
method:
You can assume that the text file has no formatting errors and that there are exactly 5 questions, each with exactly 4 options, with exactly one option designated as the solution to the question.
How do you read in text files? Assume that variable filename
contains the file name (e.g. "mcq.txt"
),
then you shoud create a BufferedReader
as follows:
BufferedReader br = new BufferedReader(new FileReader(new File(filename)));(The class definitions are all in
java.io
.)
Then, you read a line of the file (a String
) using, e.g.:
String line = br.readLine();(If you try to read too many lines, it returns
null
.)
However, the code that you write to handle files all needs to be in a try...catch
construction:
try { // all your file hanlding stuff here } catch (IOException e) { System.out.println("IO Problem"); System.exit(1); }If there is a problem, we simply display
IO Problem
and terminate the program using
System.exit(1)
Submission
Deadline: 4pm, Friday 12th February 2016.
Put all your class definitions into a directory called lab3
.
lab3
, not Lab3
,
lab-3
, Lab-3
or some other variant. Henceforth, variants will not be graded.
To submit:
- Open the Dolphin File Manager. Right-click on your
lab3
directory. Choose Compress from the menu and As Zip File from the sub-menu. You should see a new icon appear, namedlab3.zip
. - Open a console; use the cd command to move to the directory that contains
lab3.zip
and type:submit-2514 lab3.zip
Challenge exercises
Remember that challenge exercises are always optional. They do not form part of your year's work and they are not worth any marks. They are designed for those students who finish the main exercise quickly and easily, and wish to explore further. You may need to use things not covered so far in lectures.
This lab sheet is fairly easy so why not choose one or more of the following ideas (or ideas of your own)...
- Generalise your program so that it can handle any number of questions (not just 5) and different numbers of options (2 or more, not just 4) for each question. (Arrays may no longer be ideal so you may want to teach yourself Java lists.)
- Allow your program to randomly permute the questions and their options before asking them.
- Generalise your program so that it can handle questions which have multiple answers, where to be correct the user has to select all correct answers.
- Allow the file to contain many questions, but select only a subset for any particular user.
- Allow the file to contain many questions, but categorized into different levels of difficulty. A user will see a sequence of questions, one from each level of difficulty, in increasing order of difficulty. The quiz stops as soon as the user gets a question wrong.
- Similar to the previous idea but there are 15 levels of difficulty. The user has three lives which she can use to go 50:50, ask the audience or ask for a hint. Perhaps she wins a million points if she answers all 15 correctly. (Any resemblance to a copyrighted quiz game format is entirely coincidental.)
- Major, major, major challenge: teach yourself GUIs in Java (ahead of next year) and reimplement your program to use a GUI!