15-100 Exam #1, Question #1 (Morning Edition)
Please implement a class, ApartmentDirectory, to model a list,
indexed by apartmentNumber, of Familys living within an
apartment building. You are provided with the
Family class specification
which describes simply objects composed of only a familyName
and a size of the family.
The ApartmentDirectory class should have the following properties:
- It should model a fixed number of whole apartments, numbered 0
through some maximum numberOfApartments. This size should
be fixed when the ApartmentDirectory is initialized.
- It should be possible to recordAFamily in the directory. This
associates a Family object with a particular
apartmentNumber. Apartments are numbered 0 through
numberOfApartments-1. Recording a Family in the
directory should destroy any prior association between a Family
and the same apartmentNumber, if any.
- It should be possible to getTheApartmentNumber for a particular
Family. In other words, given a Family, an
ApartmentDirectory should be able to return the
apartmentNumber where they live.
Two different types of errors might occur during the execution of
ApartmentDirectory methods. In the interest of time, you must
handle only one of these two types of errors - your pick. You can assume that
the user will be well behaved and not generate the other. You should use the
typical Java mechanism to ensure that if the error condition occurs, the
affected method does not return normally, but instead inform the caller of
the condition.
- FamilyNotFound, if the user attempts to
getTheApartmentNumber for a Family that is not recored
within the ApartmentDirectory.
- InvalidApartmentNumber, if the user attempts to
recordAFamily as residing at a non-existent apartment number.