Exercise 9A
Extend the classDocument
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:
;
'. The following is an example of a
line formatted in such a way:
Bruce Tate;Beyond Java;53
split()
method of the class String
.
Document
object using the information
extracted from the line of text, and returns it.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:
Document
objects of size
numDocs.read()
, it reads from the remaining lines
of the input file the Document
objects and stores them into
the array.Exercise 9C
Adapt the client application to catch the following exceptions:
FileNotFoundException
. The message "The file does not exist"
should be displayed.IOException
. The message "The file cannot be read" should be
displayed.NumberFormatException
. Occurs when an input file is not
formatted correctly, i.e. the first line does not contain an integer
representing the number of documents, or an integer representing the number
of copies of a document is not present in the required position in a line
containing the info for a document. The message "Incorrect formatting: the
number of documents or the number of copies is missing" should be
displayedExercise 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:
split
method. The correct size of
the array is 3.NumberFormatException
while converting a
string into an integer.Also, modify the client application to catch
DocumentFormatException
. If the exception occurs the client
application should do the following:
Document
s
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
.