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 8/2/2006

A parcel delivery service needs to manage parcels that have to be sent to various countries. For a country (identified by its name, a string), the sequence of parcels to be sent to the country are stored, in the order in which they have to be sent, in a ParcelsToSend data structure. Each parcel in the ParcelsToSend data structure is represented simply by a string (denoting the address to which the parcel has to be sent).

The ParcelsToSend objects support the following functionalities:

Part 1. (20 points)

Realize a Java class ParcelsToSend to represent ParcelsToSend objects. In the proposed solution you have to take into account that there is no bound on the number of parcels that can be in a ParcelsToSend object. If you use arrays to represent the collection of parcels, you should handle the recovery of unused memory.

Solution: representation of the objects, skeleton of the class, class ParcelsToSend, class ParcelsException

Part 2. (6 points) Realize a static method getParcelsByAddress client of the class ParcelsToSend (i.e., external to such a class) that, given a ParcelsToSend object ps, a string s, and the name f of a file, writes to the file f the position in the sequence and the address string for all those parcels for which the address string starts with s, one parcel per line. The method should handle exceptions due to input/output errors that may occur.

Solution

Part 3. (4 points) Explain the concept of recursion, discussing also the issue of termination of a recursive method invocation. Give a simple example of a recursive method in Java and show an example of the evolution of the stack of activation records when the method is executed.