We want to design a Java class for representing and manipulating lists of birthdays of persons.
Part 1. (4 points) For each Birthday object, the information of interest is:
Birthday
, to represent Birthday
objects, that exports the following functionalities:
Part 2. (16 points)
Realize a Java class BirthdayList
, to
represent BirthdayList objects. Each birthday list is constituted by a
sequence of birthdays, where each birthday is an instance of the
class Birthday
. A BirthdayList has also a size, which
determines the maximum number of birthdays it can contain.
The class BirthdayList
should export the following functionalities:
Birthday
object for that person (i.e., for which the name
is n), if such an object exists; and returns null
otherwise;
Birthday
object b,
inserts the birthday b in the list of birthdays, if a birthday with
the same name as the one for b is not already present in the list.
If such a birthday is already present in the list, an exception is thrown.
If the birthday cannot be inserted because the number of birthdays in the
list is already equal to the size of the list, an exception is thrown;
null
. Notice that a date after another one may
refer to the following year. If the birthday list is empty, an exception
is thrown.
Part 3. (6 points)
Realize a static method personsWithBirthday
, client of the class
BirthdayList
(i.e., external to such a class) that, given a
BirthdayList
object bl, a day d (an integer), a
month m (an integer), and the name f of a file, writes on the
file f the names of all those persons in the list whose birthday is on
day d and month m, one name per line. The method should
explicitly handle exceptions due to input/output errors that may occur.
Part 4. (4 points) Discuss the mechanism of dealing with errors in Java through exceptions. Give a complete example, where you show how a class for handling exceptions is defined, how an exception is thrown, and how an exception is handled by catching it.