Free University of Bolzano/Bozen
Faculty of Computer Science - Bachelor in Applied Computer Science
Bachelor in Production Engineering
Introduction to Programming - A.A. 2004/2005

Exercise
Classes ParkingPlace and ParkingLot


We want to realize a program for the administration of a parking lot formed by a collection of parking places. For each parking place the following information has to be stored:


Part 1 Write a class ParkingPlace, for handling a parking place, that implements the following methods:

Use the example program TestParkingPlace.Java to test the class you have developed.


Part 2 Write a class UseParkingPlace that contains various public static methods that are clients of ParkingPlace. In the description of all methods below, we assume that a parking lot is always represented as an array of parking places. The class should contain the following methods:

  1. static int firstFreePlace(ParkingPlace[] p) : that, given a parking lot p, returns the index of the first free parking place in p;
  2. static int countFreePlaces(ParkingPlace[] p) : that, given a parking lot p, returns the number of free parking places in p;
  3. static int[] freePlaces(ParkingPlace[] p) : that, given a parking lot p, returns an array of integers containing the indices of all free parking places in p;
  4. static void carEnters(ParkingPlace[] p, String a, int hour, int minutes) : that, given a parking lot p, modifies the array p by inserting the car a (represented by its plate) in the first free parking place available in p, assigning hour and minutes as arrival time; if there is no free parking place in p, the method does nothing;
  5. static void carExits(ParkingPlace[] p, String a) : that, given a parking lot p, modifies the array p by freeing the parking place where the car a is parked; if the car a is not present in the parking lot, the method does nothing;
  6. static int longestParkedCar(ParkingPlace[] p) : that, given a parking lot p, returns the index of the parking place in which the longest parked car is present; if there is more than one such car, the method should return one of the indexes (chosen arbitrarily); if there is no car parked in the parking lot, the method should return -1;
  7. static String[] allParkedCars(ParkingPlace[] p) : that, given a parking lot p, returns an array of strings that represent the plates of all cars present in p;
  8. static ParkingPlace[] readParkingLotFromFile(String filename) : that, given the name of a file containing parkingPlace records, constructs an array of ParkingPlace that represents the parking lot having exactly the parking places stored on the file. Each parkingPlace record has the following form:
    free
    license-plate
    hour
    minutes
    
    where Suggestion: introduce an auxiliary method for reading a single record;
  9. static void writeParkingLotToFile(ParkingPlace[] p, String filename) : that, given a parking lot p and the name of a file, writes on the file the parkingPlace records (see above) corresponding to the entire parking lot.

Part 3 Realize a class ParkingLot to represent parking lots, and whose objects support the same functionalities as those implemented in UseParkingPlace through static methods. The class should export also a constructor that takes as parameter a positive integer n and constructs a parking lot with n parking places that initially are all empty. Notice that the method readParkingLotFromFile, that takes as parameter a string representing a filename containing parkingPlace records (see above), and constructs a parking lot having exactly the parking places stored on the file, should be implemented as a static method that creates and returns an object of the class ParkingLot.