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. Important: It must be called lab3, not Lab3, lab-3, Lab-3 or some other variant. Henceforth, variants will not be graded.

To submit:

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)...