Free University of Bozen-Bolzano
Bachelor in Production Engineering
Introduction to Programming - A.A. 2006/2007
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 author (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;
- getAuthor: that returns the author 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. A BookList has a size, which determines the maximum
number of books it can contain.
Realize a Java class BookList
, to represent BookList
objects, that exports the following functionalities:
- creation of a BookList, given the topic and the size; the
created BookList is initially empty (i.e., it contains no
books);
- getTopic: that returns the topic;
- getSize: that returns the size of the book list;
- 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); if the book list is full,
the method does nothing;
- 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 the method returns
null
;
- 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, and does nothing 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 with author s.
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, and a string s (representing
the name of an author), writes to standard output the title, publisher, and
year of all those books in bl with author s, one book per
line.
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.