Free University of Bozen-Bolzano
Bachelor in Production Engineering
Introduction to Programming - A.A. 2004/2005
Written Exam 28/6/2005
A company manufacturing bicycles needs to keep track of its bikes that are
being sold in various shops.
Part 1. (4 points)
For each Bike object, the information of interest is:
- the code (a string) that identifies the bike;
- the color (a string);
- the minimum weight (in kg) of a person for which the bike is suited (an
integer);
- the maximum weight (in kg) of a person for which the bike is suited (an
integer).
Realize a Java class Bike
, to represent Bike objects,
that exports the following functionalities:
- creation of a Bike, given its code, color, minimum weight, and
maximum weight;
- getCode: that returns the code of the bike;
- getColor: that returns the color of the bike;
- getMinWeight: that returns the minimum weight of the bike;
- getMaxWeight: that returns the maximum weight of the bike.
Solution:
class Bike
Part 2. (16 points)
For each shop (identified by its name, a string), the bikes available in that
shop are stored in a BikePool data structure. A BikePool has
a size, which determines the maximum number of bikes it can contain.
Realize a Java class BikePool
, to represent BikePool
objects, that exports the following functionalities:
- creation of a BikePool, given the name of the shop and the size
of the pool; the created BikePool is initially empty (i.e., it
contains no bikes);
- getShop: that returns the name of the shop;
- getSize: that returns the size of the pool;
- newBike: that, given a Bike object, inserts it into the
bike pool, if a bike object with the same code is not already present in the
pool, does nothing otherwise; if the bike pool is full the method does
nothing;
- getBike: that, given the code c of a bike, returns the
bike object with code c in the bike pool, if such an object is
present in the pool,
null
otherwise;
- removeBike: that, given the code c of a bike, eliminates
from the bike pool the bike object with code c, if such an object is
present in the pool, does nothing otherwise;
- numBikes: that returns the number of bikes in the pool;
- bikesForWeight: that, given a positive integer h
representing the weight of a person, returns an array of strings containing
the codes of all bikes in the pool that are suitable for weight h; a
bike is suitable for a weight h if h is between the minimum
and maximum weights of the bike.
Solution:
representation of the
objects,
skeleton of the class,
class BikePool
Part 3. (6 points)
Realize a static method ColorsforWeight
, client of the class
BikePool
(i.e., external to such a class) that, given a
BikePool
object bp, a positive integer h
representing the weight of a person, and the name f of a file, writes
on the file f the colors of all those bikes in bp that are
suitable for weight h.
Solution
Additional files to test the solution:
class Main
,
input file bikes.txt
Part 4. (4 points)
Discuss the notions scope and of lifetime of a variable. Emphasize the
differences with respect to scop and lifetime between the different types of
variables that a Java program may contain.