Free University of Bozen-Bolzano
Bachelor in Production Engineering
Introduction to Programming - A.A. 2005/2006
5 CFU
Written Exam 29/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
. A BirthdayList has also a size, which
determines the maximum number of birthdays it can contain.
The class BirthdayList
should export the following functionalities:
- creation of a BirthdayList, given its size. Initially, the list
is empty;
- maxBirthdays: that returns the maximum number of birthdays the
list can contain;
- 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, or if the birthday
cannot be inserted because the number of birthdays in the list is already
equal to the size of the list, then the method does nothing;
- 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, or if the
birthday cannot be inserted because the number of birthdays in the list is
already equal to the size of the list, then the method does nothing;
- 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, the method
does nothing;
- birthdaysInMonth: that, given a month m, returns an array
of strings containing the names of all persons in the birthday list whose
birthday is in month m.
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 and a month m (an integer),
writes on standard output the name and day of the month of the birthday for all
those persons in the list bl whose birthday is in month m, one
name per line.
Part 4. (4 points)
Discuss the different types of errors that a Java program may contain, and give
for each of them an example.