Question: Create a valet parking program (Using JAVA) with the following classes and methods. Car.java : Create a class named Car which has four instance variables

Create a valet parking program (Using JAVA) with the following classes and methods.

Car.java: Create a class named Car which has four instance variables for make, model, year and color with appropriate accessibility levels. Add getter and setter methods for each member variable. Add a default constructor and a constructor that expects values for each member variable. Also, override the toString() method of java.lang.Object with an appropriate description of a vehicle.

ReservedSpace.java: Implement a class that represents a reserved space for valet parking. Initially there are five spots available. Every time it fills up, the number of spots doubles in size. The class has two instance variables: an array of Cars that represents the parking spots and a position variable that marks the next available free spot.

The instance variable must be a simple array of type Car[]. Initialize the array to size 5. A new array will be created twice as large as the previous array as described in the park method below. That means, initially the size of the array is 5, then 10, 20, 40, etc. The class must have the following methods:

void park(Car car): This method can be viewed as and add method which essentially stores the Car object passed to it to the next free spot in the array. That is, it puts the given car in the next available spot using the nextSpot member variable. After the car object is stored in the array, the nextSpot member variable is incremented by one to refer to the next empty position in the array. The first car is parked in spot 0, then next one in spot 1, etc. If no more spot is available, the reserved space is doubled. That is, the first car is put at index 0, the second at index 1 in the array, etc. If the array is full, create a new temporary array of twice the size of the current member array. Then copy all Car objects from the member array to the new temporary array. Finally, assign the temporary array to the member array. That means, initially the size of the array is 5. If the park(..) method gets called five times, the array is full. On the sixth call to the park(..)method, create a temporary array, copy all five car objects to the new array, then assign it to the member variable and add the new car to it. The member array now has a size of 10 with 6 cars in it

Car get(int spot) throws NoCarException: This method retrieves the car at the spot that is passed to it. Basically, it simply returns the car stored at the specified index in the array. If the index is out of bounds of if theres no car located at the spot specified, it throws an NoCarException*.

Car pullUp(int spot) throws NoCarException: This method removes one car at the spot that is passed to it. Any cars parked at subsequent spots will be moved by one spot to prevent empty spots. That is, there are never any empty spots between the very first car parked at spot 0 and the first available spot specified by the nextSpot member variable. If the spot index is out of bounds or if theres no car located at the spot specified, it throws an NoCarException*. The return type Car means that this function must return a car object. In this case, it is the car that is removed from the array.

NoCarException.java: Create a new exception class which is used in the get and pullUp methods above.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!