Free University of Bozen-Bolzano
Faculty of Computer Science - Bachelor in Applied Computer Science
Introduction to Programming - A.A. 2005/2006
(new study plan - 8 CFU)
Written Exam 14/6/2006
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:
- the day of the month (an integer between 1 and 31),
- the month (an integer between 1 and 12),
- the name of the person (a string).
Realize a Java class Birthday
, to represent Birthday
objects, that exports the following functionalities:
- creation of a Birthday, given the day of the month, the month,
and the name of the person;
- getDay: that returns the day of the month;
- getMonth: that returns the month;
- getName: that returns the name of the person.
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
.
The class BirthdayList
should export the following functionalities:
- creation of a BirthdayList that is initially empty;
- numBirthdays: that returns the total number of birthdays in the
list;
- numBirthdays: that, given a day d (an integer) and a
month m (an integer), returns the number of persons in the list
whose birthday is on day d and month m;
- getBirthday: that, given the name n of a person, returns
the
Birthday
object for that person (i.e., for which the name
is n), if such an object exists; and returns null
otherwise;
- addBirthday: that, given a
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;
- addBirthday: that, given a day d (an integer), a
month m (an integer), and a name n (a string), inserts a
birthday with day d, month m, and name n in the list
of birthdays, if a birthday for name n is not already present in the
list. If such a birthday is already present in the list, an exception is
thrown;
- removeBirthday: that, given a name n, removes the birthday
for name n from the list of birthdays, if such a birthday is present
in the list. If such a birthday is not present in the list, an exception
is thrown;
- birthdaysOnDate: that, given a day d and a month m,
returns an array of strings containing the names of all persons in the
birthday list whose birthday is on day d and month m.
- nextBirthday: that, given a day d and a month m,
returns the next birthday in the list after the date d/m. If
there is more than one such birthday in the list, then a new birthday
object is returned with the appropriate day and month and with name equal
to
null
. Notice that a date after another one may
refer to the following year. If the birthday list is empty, an exception
is thrown.
In the proposed solution, you have to take into account that there is no bound
on the number of birthdays a BirthdayList can have. If you use an array
to represent the list of birthdays, you should handle the recovery of unused
memory.
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.