Free University of Bozen-Bolzano
Bachelor in Production Engineering
Introduction to Programming - A.A. 2005/2006
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). A
ParcelsToSend object has a size, which determines the maximum number
of parcels it can contain.
The ParcelsToSend objects support the following functionalities:
- creation of a ParcelsToSend object, given the name of the
country and the size of the sequence; the created ParcelsToSend
object is initially empty (i.e., it contains no parcels);
- getCountry: that returns the name of the country;
- size: that returns the size of the sequence;
- insertParcel: that, given a string representing a new parcel,
inserts the new parcel into the sequence of parcels to be sent, respecting
the proper order (i.e., parcels inserted earlier will be sent earlier); if
the sequence is full, it should not be changed;
- nextParcel: that returns (the address of) the next parcel to be
sent (the one that was inserted first in the sequence); if the sequence is
empty, then
null
is returned;
- sendParcel: that returns (the address of) the next parcel to be
sent and removes it from the sequence; if the sequence is empty, nothing is
done;
- numParcels: that returns the number of parcels in the sequence;
- parcel: that, given an integer i returns (the address
of) the i-th parcel from the sequence that has to be sent
(starting to count from 0, i.e., the first parcel to be sent corresponds
to i=0, the second parcel to i=1, etc.); if the integer
does not correspond to any parcel in the sequence, then
null
is returned.
- parcelsForAddress: that, given a string s, returns an
array of integers containing the positions in the sequence of all parcels
whose address string starts with s.
Part 1. (20 points)
Realize a Java class ParcelsToSend
to represent
ParcelsToSend objects.
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 and a string s, writes
to standard output 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.
Part 3. (4 points)
Discuss the concepts of scope and of lifetime of variables, and point out the
difference between local variables and instance variables.