Free University of Bozen-Bolzano
Faculty of Computer Science - Bachelor in Applied Computer Science
Introduction to Programming - A.A. 2006/2007
(new study plan - 8 CFU)
Written Exam 30/1/2007
A library maintains books on various subjects.
Part 1. (4 points)
For each Book object, the information of interest is:
- the title (a string);
- the authors (a string);
- the publisher (a string);
- the publication year (an integer).
Realize a Java class Book
, to represent Book objects,
that exports the following functionalities:
- creation of a Book, given its title, authors, publisher, and
publication year;
- getTitle: that returns the title of the book;
- getAuthors: that returns the authors of the book;
- getPublisher: that returns the publisher of the book;
- getYear: that returns the publication year of the book.
Part 2. (16 points)
For each topic (identified by its name, a string), the books on that topic
available in the library shop are stored in a BookList data structure.
Each book in a BookList is assigned a progressive number (an integer),
starting from 0.
Realize a Java class BookList
, to represent BookList
objects, that exports the following functionalities:
- creation of a BookList, given the topic; the created
BookList is initially empty (i.e., it contains no books);
- getTopic: that returns the topic;
- addBook: that, given a Book object, inserts it into the
book list, assigning it the next available progressive number (i.e., one plus
the number of the last book in book list);
- getBook: that, given an integer n, returns the book
object with progressive number n in the book list; if n
does not correspond to a number of a book in the list (i.e., n is
either negative or greater or equal than the number of books in the list),
then an exception is thrown;
- findBook: that, given the title t of a book, returns
the progressive number of the first book object with title t in the
book list, if such an object is present in the book list, and -1 otherwise;
- removeBook: that, given an integer n, removes from the
book list the book object with progressive number n, if n
corresponds to a number of a book in the list, throws an exception otherwise;
note that, when a book is removed from the book list, the progressive number
assigned to each subsequent book in the book list is decreased by 1;
- numBooks: that returns the number of books in the book list;
- booksByAuthor: that, given a string s (representing the
name of an author), returns an array of integers containing the progressive
numbers of all books in the book list for which s appears among the
authors (i.e., s is a substring of the string representing the
authors of the book).
Hint: use the method int indexOf(String s)
of the
String
class, which returns the index within this string of the
first occurrence of s
, if s
is a substring of this
string, and -1 otherwise.
In the proposed solution, you have to take into account that there is no bound
on the number of books that can be in a BookList. If you use arrays
to represent a book list, you should handle the recovery of unused memory.
Part 3. (6 points)
Realize a static method writeBooksByAuthor
, client of the class
BookList
(i.e., external to such a class) that, given a
BookList
object bl, a string s (representing the
name of an author), and the name f of a file, writes on the file
f the title and authors of all those books in bl for which
s appears among the authors, one book per line. The method should
catch exceptions due to input/output errors that may occur, and print suitable
error messages.
Part 4. (4 points)
Discuss the notion of parameter passing in Java. Illustrate the difference
between passing parameters of type (reference to an) object and of primitive
types. Provide an example in which this difference becomes evident.