Written Exam 7/6/2005

A ski rental company needs to keep track of the skis that are being rented in various cities.

Part 1. (4 points) For each Ski object, the information of interest is:

• the code (a string) that identifies the ski;
• the brand (a string);
• the minimum height (in cm) of a person for which the ski is suited (an integer);
• the maximum height (in cm) of a person for which the ski is suited (an integer).
Realize a Java class `Ski`, to represent Ski objects, that exports the following functionalities:
• creation of a Ski, given its code, brand, minimum height, and maximum height;
• getCode: that returns the code of the ski;
• getBrand: that returns the brand of the ski;
• getMinHeight: that returns the minimum height of the ski;
• getMaxHeight: that returns the maximum height of the ski.

Solution: class `Ski`

Part 2. (16 points) For each city (identified by its name, a string), the skis available in that city are stored in a SkiPool data structure. Realize a Java class `SkiPool`, to represent SkiPool objects, that exports the following functionalities:

• creation of a SkiPool, given the name of the city; the created SkiPool is initially empty (i.e., it contains no skis);
• getCity: that returns the name of the city;
• newSki: that, given a Ski object, inserts it into the ski pool, if a ski object with the same code is not already present in the pool, throws an exception otherwise;
• getSki: that, given the code c of a ski, returns the ski object with code c in the ski pool, if such an object is present in the pool, `null` otherwise;
• removeSki: that, given the code c of a ski, eliminates from the ski pool the ski object with code c, if such an object is present in the pool, throws an exception otherwise;
• numSkis: that returns the number of skis in the pool;
• skisForHeight: that, given a positive integer h representing the height of a person, returns an array of strings containing the codes of all skis in the pool that are suitable for height h; a ski is suitable for a height h if h is between the minimum and maximum heights of the ski.
In the proposed solution, you have to take into account that there is no bound on the number of skis that can be in a SkiPool. If you use arrays to represent the collection of skis, you should handle the recovery of unused memory.

Part 3. (6 points) Realize a static method `brandsforHeight`, client of the class `SkiPool` (i.e., external to such a class) that, given a `SkiPool` object sp, a positive integer h representing the height of a person, and the name f of a file, writes on the file f the brands of all those skis in sp that are suitable for height h. The method should handle exceptions due to input/output errors that may occur.

Solution

Additional files to test the solution: class `Main`, input file `skis.txt`

Part 4. (4 points) Discuss the types of errors that a program may contain, and give for each of them an example in terms of a (fragment) of Java program.