15-110 FALL 2009 (CORTINA)

HOMEWORK 7 - due Friday, November 6, 2009 in class

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            99
We 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.


  1. (1 pt) Write the ParkingLot method insertAtEnd that has one parameter representing a Car to be inserted into the parking lot. If there is room in the lot, add this car into the next available space. If there is no room, output a warning message and do not change the parking lot. This method does not return any information to its caller.

    public void insertAtEnd(Car automobile) {
    
    
    
    }
    

  2. (1 pt) The following method is written for getCar which returns the Car in the parking space given by the integer parameter.

    public Car getCar(int spaceNumber) {
    	return parkingSpace[spaceNumber];
    }
    

    1. Assuming 0 ≤ spaceNumber < parkingSpace.length, what does this method return if there is no car parked in the given space number?

    2. What happens if spaceNumber is outside of the range given in part (a)? Correct the code so that this method returns the same result from part (a) in this case.

  3. (1.5 pts) Write the ParkingLot method getTotalMileage that has no parameters. This method should look at each Car reference in the array and add up and return the mileage of all cars in the parking lot. Remember that the number of cars stored in the array is numParked, which is not necessarily the same as parkingSpace.length, since the array may not be full.

    public int getTotalMileage() {
    
    
    
    }
    

  4. (1.5 pts) Write the ParkingLot method countMake that returns the number of cars in the parking lot that match the car make given as a parameter to this method. Remember that the number of cars stored in the array is numParked, which is not necessarily the same as parkingSpace.length, since the array may not be full.

    public int countMake(String carMake) {
    
    
    
    
    }
    

  5. (1.5 pts)

    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 = ________________________________;
    }
    

  6. (2 pts) Complete the ParkingLot method below that removes the first car in the parking lot that has a mileage that exceeds the mileage given in the parameter to this method. When you search the parking lot, start with parking space 0. If you find a match, move all subsequent cars in the parking lot over one position to fill in the gap made by the removed car. If there is no car in the parking lot that matches the condition, then do not change the parking lot.

    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 = _____________________________;
    	}
    
    }
    

  7. (1.5 pts)

    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());
    }
    

    1. Show how to insert the first four cars into the lot in the order given above and then insert the last car into the first space (space #0), moving all the other cars over one space in the lot.

    2. Show how to print the total mileage of all cars currently in the lot and then show how to remove first car with mileage greater than 10000 miles.

    3. Show the output from the instruction
      System.out.println(p.toString())
      assuming all of the instructions from parts a and b are executed first.