Free University of Bozen-Bolzano
Faculty of Computer Science - Bachelor in Applied Computer Science
Introduction to Programming - A.A. 2005/2006
Exam exercise
Class Counter
The municipality needs a system to handle the queues at their offices.
At the counter, persons in need of documents from the offices can get a unique,
progressive number that defines their position in the queue. The requests are
handled according to this number.
Part 1.
Realize a class Request
with the following attributes:
- the name of the person that issued the request (a string);
- the requests's progressive number (an integer).
The class should export the following functionalities:
- a constructor to create a new request, given a person's name and a
progressive number;
- getPerson that returns the person's name;
- getNumber that returns the progressive number;
- returning a String representation of a request object, by overriding
the
toString
method of Object
.
Solution:
skeleton of the class,
class Request
Part 2.
Realize a class Counter
, each of whose objects stores a queue of
requests issued at the counter. The maximum number of requests that can be in
the queue is determined the moment a counter is created. The class should
export the following functionalities:
- a constructor that, given a positive integer representing the maximum
number of requests that can be in the queue, creates an empty counter
(i.e., a counter with an empty queue),
- addRequest that, given a person's name appends a
new request to the counter's queue and returns the progressive number
associated to this request; if there is no room in the queue, -1 is
returned;
- handleRequest that gets the next request from the queue;
if there are no requests left,
null
is returned;
- giveUp that, given a progressive number eliminates the
corresponding request from the queue;
if there is no such request, nothing is done;
- waitingTime that, given a progressive number
returns the number of requests that are queued before that request; if
there is no such request, -1 is returned;
- toString that returns a string representation of the request
object, with one request per line.
Solution:
representation of the
objects,
skeleton of the class,
class Counter
Part 3.
Implement a client class for Counter
that contains a static method
readFromInput()
that, given a Counter
c:
- reads from standard input an integer n and then n names of
persons, one per line,
- adds for each person read from input a request to counter c, and
- returns an array containing the progressive numbers associated to the
requests.
Solution
Part 4.
Explain how objects are allocated in a Java program, and when and how the
memory occupied by an object is again made available for other objects.
Note: You can test the code you have developed by using the class
defined in the file Main.java and redirecting
standard input from the example file paperopoli.txt
(containing the italian names of Disney comic book characters) as follows:
C:\> java Main < paperopoli.txt