Free University of Bozen-Bolzano
Faculty of Computer Science - Bachelor in Applied Computer Science
Introduction to Programming - A.A. 2004/2005
Exam exercise
Class Pier
We want to handle information relative to piers to which boats can be
attached. Each boat is represented by its name (a string). A Pier has
a code (a string) and a fixed number of boat positions, numbered sequentially
starting from 0. Each boat position can be either free or occupied by a boad.
The Pier objects support the following functionalities:
- given a code c (a string) and an integer n, creation of
a Pier object with code c and n boat positions that
initially are all empty;
- numBoatPositions, that returns the number of boat positions of the
pier;
- isFree, that, given the number n of a boat position, returns
true
if the position n is free, false
otherwise;
- assignPosition, that, given a boat position n and the name
b of a boat (a string), occupies the position n with the
boat b, if the position is free, does nothing otherwise;
- freePosition, that, given the number n of a boat position,
makes position n free;
- getBoat, that, given the number n of a boat position,
returns the name of the boat that occupies position n, if the
position is occupied, and
null
otherwise;
- getFreePosition, that returns the number of some free boat
position; if there is no free boat position, -1 is returned;
- searchHomonyms, that returns
true
if there are two
(or more) boats at the pier with the same name, and false
otherwise;
- compactBoats, that modifies the assignment of positions to the
boats that occupy the pier in such a way that all positions up to a
certain number are occupied, and the remaning ones are free.
Part 1. Realize a Java class Pier
to represent
Pier objects.
Solution:
representation of the
objects,
skeleton of the class,
class Pier
Part 2.
Realize a static method situationAtPier
, client of the class
Pier
that, given a pier p, returns an array a of
booleans, whose size is equal to the number of boat positions of pier p,
and such that the value in position i of the array is equal to
true
if boat position number i of p is occupied by a
boat, and false
otherwise.
Solution
Part 3.
Illustrate through examples the notions of overloading and of overriding, and
how they are realized in Java.