Free University of Bolzano/Bozen
Faculty of Computer Science - Bachelor in Applied Computer Science
Introduction to Programming - A.A. 2006/2007

Exercise 9

Files and Exception Handling


Exercise 9A

Extend the class Document of Exercise 6A with a static method read(). The method should take a parameter br of type BufferedReader, read data from br, and create and return a new instance of the class Document. More precisely, the method read() should do the following:

  1. It reads a single line of text from br into a string variable. The line contains the author's name, the document's title, and the number of copies, separated by ';'. The following is an example of a line formatted in such a way:

    Bruce Tate;Beyond Java;53

  2. It extracts the information from the string using the split() method of the class String.
  3. It constructs a new Document object using the information extracted from the line of text, and returns it.

Solution


Exercise 9B

Implement a client of the class Document that reads a set of Document objects from a given input file, computes the average number of copies, and writes to and output file all those documents whose number of copies is greater than the average.

More precisely, the client application should do the following:

  1. It reads from the command line the name of an input file and the name of an output file. The input file should contain in the first line an integer numDocs, and in the next numDocs lines the information about documents, one document per line, formatted according to part A of the exercise.
  2. It reads from the first line of the input file the number numDocs of documents, and creates an array of Document objects of size numDocs.
  3. Using the method read(), it reads from the remaining lines of the input file the Document objects and stores them into the array.
  4. Based on the documents stored in the array, it computes the average number of copies.
  5. It writes to the output file all documents whose number of copies is greater than the average. The first line of the file should again contain the number of documents stored in the file, and all remaining lines of the file should be formatted in the same way as for the input file.
The file data.txt is an example input file for the exercise.

Solution


Exercise 9C

Adapt the client application to catch the following exceptions:

The application should terminate if any of the above mentioned exceptions occurs.

Solution


Exercise 9D

Define a class DocumentFormatException to represent document formatting exceptions. Extend the method read() of the class Document so that it throws this exception in the following two cases:

Also, modify the client application to catch DocumentFormatException. If the exception occurs the client application should do the following:

  1. It displays the notification message including the index of the badly formatted line.
  2. It sets the corresponding element of the array of Documents to null. Afterwards, while calculating the average number of copies and writing to the output file, it skips all elements of the array whose value is equal to null.
The file data_errors.txt is an example input file for the exercise, containing various types of errors.

Solution