Please remember that homework must be handed in on time. No late homework will be accepted.
------------------------------------------------- ------- | _ | _ | _ | _ | _ | | | | | | | /_\ | /_\ | /_\ | /_\ | /_\ | | | | ... | | | O O | O O | O O | O O | O O | | | | | | ------------------------------------------------- ------- 0 1 2 3 4 5 6 7 99We will use the Car class defined in lecture to create a class named ParkingLot that represents a collection of cars. The cars in the parking lot will be stored in the ParkingLot class in a field that will be an array called parkingSpace, and the number of cars parked in the parking lot (and thus, stored in the array) will be stored in a field named numParked. The constructor for ParkingLot creates an array to hold a maximum of 100 cars, initially empty:
public class ParkingLot { private Car[] parkingSpace; private int numParked; public ParkingLot() // constructor { parkingSpace = new Car[100]; numParked = 0; } public String toString() { String result; result = "Number of cars parked: " + numParked + "\n"; for (int i = 0; i < numParked; i++) result += i + ": " + parkingSpace[i].toString() + "\n"; return result; } // other ParkingLot methods go here // ... }
As we store cars in the array that represents the parking lot, we will store the cars so that the first car referenced will be in position 0 of the array, and all of the other cars will be referenced in subsequent positions in the array (no gaps). Thus, if there are numParked cars in the lot, they will be referenced in positions 0 through numParked-1 in the array.
We also included a toString method that returns the current state of the parking lot including the number of cars parked, and information about each car in the lot (assuming the Car class has its own toString method). Note that in the toString method, the loop runs up to but not including numParked, not parkingSpace.length, since the array may not be completely full with cars.
public void insertAtEnd(Car automobile) { }
public Car getCar(int spaceNumber) { return parkingSpace[spaceNumber]; }
public int getTotalMileage() { }
public int countMake(String carMake) { }
Complete the ParkingLot method below that takes a reference to a Car given in the parameter for the method and inserts the car in parking space 0, shifting all other cars over one space to make room for the new car. If the parking lot is full, however, output an error message instead and do not insert the new car.
public void insertCarAtZero(Car automobile) { if (__________________________) { System.out.println("NO CAR INSERTED. LOT IS FULL."); return; // exit method immediately } // Shift all cars over one space to make room for new automobile for (int i = _______________; _____________________; _________) { parkingSpace[_______________] = parkingSpace[i]; } // Insert the car into space 0 parkingSpace[0] = automobile; numParked = ________________________________; }
public void removeCar(int mileage) { // Search for position of matching car int position = 0; while (position < ___________________ && ____________________________________ <= mileage) { position++: } // If we found a match, shift subsequent cars over one // position toward the beginning of the array. if (____________________________________) { for (int i = _________; _________________; __________) { parkingSpace[_______] = parkingSpace[i]; } parkingSpace[numParked-1] = null; // clear last space that was used numParked = _____________________________; } }
Consider the following main method in another Java class:
public static void main(String[] args) { Car homer = new Car("Pink Sedan", 15100); Car marge = new Car("Canyonero", 1000); Car barney = new Car("Plow King", 4000); Car flanders = new Car("Geo", 160); Car burns = new Car("Roadster", 225000); ParkingLot p = new ParkingLot(); // --code for parts a and b goes here-- System.out.println(p.toString()); }