Free University of Bozen-Bolzano
Bachelor in Production Engineering
Introduction to Programming - A.A. 2004/2005
Exam exercise
Class PhotoGallery
We want to realize a Java class PhotoGallery
that represents an
online photo gallery. Each PhotoGallery has an url (represented by a
string), and contains photos, each represented simply as a string that denotes
the link to the actual photo. The maximum number of photos a photo gallery may
contain is fixed the moment the photo gallery is created. The functionalities
of the objects of the class are:
- create: that takes as parameter a string representing an
url and a positive integer n and creates a
PhotoGallery object with the specified url, with a
capacity to contain n photos, and with no photo initially
stored;
- getUrl: that returns the url of the PhotoGallery;
- numPhoto: that returns the number of photos present in the
PhotoGallery;
- add: that takes a photo (i.e., a string) as parameter, and adds
it to the PhotoGallery, if it is not already present; otherwise
it does not do anything; if there is no more room to store the gallery
should not be changed, and a message should be printed on standard
output
;
- remove: that takes a photo (i.e., a string) as parameter, and
removes it from the PhotoGallery, if it is present; otherwise
it does not do anything;
- present: that takes a photo (i.e., a string) as parameter, and
returns
true
if the photo is present in the
PhotoGallery; false
otherwise;
- returnAllPhotos: that returns an array containing all the photos
(represented as strings) present in the PhotoGallery (note: the
returned array should contain only the photos actually present);
- removeAll: that eliminates all photos from the
PhotoGallery.
Part 1. Realize the class PhotoGallery.
Solution:
representation of
the objects,
skeleton of the class,
solution
Part 2. Realize a client class of the class
PhotoGallery
containing a static method readFromFile
that takes as parameters a string f that represents a filename
containing photos (i.e., strings) and a PhotoGallery g, and adds
to g all photos present in the file f.
Solution
Part 3. Briefly discuss the difference between instance
variables and local variables (with respect to initialization, lifetime, scope).