Free University of Bozen-Bolzano
Faculty of Computer Science - Bachelor in Applied Computer Science
Introduction to Programming - A.A. 2004/2005
(old study plan - 6 CFU)
Written Exam 2/2/2005
A post office needs to keep track of letters that have to be delivered to
various cities. For each city (identified by its name, a string), the letters
to be delivered to that city are stored, in the order of delivery, in a
LetterQueue data structure. Each letter in the LetterQueue
is represented simply by a string (denoting the name of the recipient). A
LetterQueue has a size, which determines the maximum number of letters
it can contain.
The LetterQueue objects support the following functionalities:
- creation of a LetterQueue, given the name of the city and the
size of the queue; the created LetterQueue is initially empty (i.e.,
it contains no letters);
- getCity: that returns the name of the city;
- size: that returns the size of the queue;
- newLetter: that, given a string representing a new letter,
inserts the new letter into the queue, respecting the order of arrival; if
the queue is full, an exception is thrown;
- firstLetter: that returns a string containing the name of the
first letter in the queue (the one that was inserted first in the queue); if
the queue is empty, an exception is thrown;
- removeLetter: that eliminates the first letter from the queue;
if the queue is empty, an exception is thrown;
- numLetters: that returns the number of letters in the queue;
- letter: that, given an integer i that represents the
position in the queue of a letter, returns the letter (i.e., the name of the
recipient) in that position; if the integer does not correspond to a valid
position in the queue, then an exception is thrown. The position in the
queue of a letter is equal to the number of letters that precede it in the
queue (e.g., the first letter in the queue has position 0, the following one
has position 1, etc.);
- shortLetters: that, given an integer len, returns an
array of integers containing the positions in the queue of all letters whose
string is shorter than len.
Part 1. (20 points)
Realize a Java class LetterQueue
to represent LetterQueue
objects.
Solution:
representation
of the objects,
skeleton of the class,
class LetterQueue
,
class
LetterQueueException
Part 2. (6 points)
Realize a static method lettersShortRecipient
client of the class
LetterQueue
(i.e., external to such a class) that, given a
LetterQueue
object q, a positive integer len,
and the name f of a file, writes on the file f the position
in the queue and the string (i.e., the name of the recipient) for all those
letters for which the string is shorter than len, one letter per line.
The method should handle exceptions due to input/output errors that may occur.
Solution
Part 3. (4 points)
Discuss the concepts of overloading and of overriding in Java. Give for both a
short example.