15-110 FALL 2009 (CORTINA)

HOMEWORK 8 - due Friday, November 13, 2009 in class

Please remember that homework must be handed in on time. No late homework will be accepted.


Refer to the ParkingLot class described in Homework 7 for problems 1-4.

  1. (1.5 pts) Write a method that returns the mileage of the car currently in the parking lot that has the minimum mileage. If the parking lot is empty, return 0 instead. REMEMBER: The parking lot may not be full, but all cars that are parked are stored in the array starting at index 0 with no empty spaces, and the field numParked tells you how many cars are parked in the parking lot currently.

    public int getMinimumMileage()
    {
    
    
    
    
    }
    

  2. (1 pt) Assume that all makes are in uppercase letters only (e.g. "HONDA", "FORD", "CHRYSLER", etc.). Complete the method below for the ParkingLot class that returns true if the cars in the parking lot are parked in alphabetical order based on their makes, false otherwise. (If the lot is empty, this method returns true.) The entire lot of cars is in alphabetical order if each adjacent pair of cars are in alphabetical order.

    public boolean isAlphabetical() {
    
    	if (numParked == 0)  return true;
    
    	for (int i = 0; i < ___________________; i++) {
    
    		// Compare car i with car i+1 in parking lot:
    
    		if (_________________________________________) {
    
    			return false;  // not in alphabetical order, return false
    
    		}
    	}
    	return true;
    }
    

  3. (1.5 pts) Write a method for the parkingLot class named reverse that reverses the order of the cars in the parking lot. You should do this by setting up a loop that swaps the first and last cars, second and second-to-last cars, etc. This method requires no parameter (since you will reverse the entire parking lot) and does not return any value. REMEMBER: The last car is always in space numParked-1. If the lot is empty, don't change the parking lot.

    public void reverse() {
    
    
    
    
    
    
    }
    

  4. (1 pt) The following method is written for the ParkingLot class to create a new parking lot consisting of only those cars that match the given make exactly as specified in the parameter. You may assume that the insertAtEnd method from Homework 7 exists.

    public ParkingLot buildNewLot(String carMake)
    {
    	// Creates and returns a new parking lot that contains
    	// references to the cars in this parking lot (in the
    	// same relative order) that have a make that matches
    	// the car make given in the parameter.
    
    	ParkingLot newLot = new ParkingLot();
    	for (int i = 0; i < numParked; i++)
    	{
    		Car currentCar = parkingSpace[i];
    		if (currentCar.getMake().equals(carMake))
    			insertAtEnd(currentCar);
    	}
    	return newLot;
    }
    

    This method contains a logical error, however. Carefully explain what happens specifically if this method is called and at least one car in the current parking lot matches the make given in the parameter.


  1. (3 pts) Let t be a two-dimensional array of int that represents the temperature readings at the airport on each hour over a one-week period, declared as follows:

    int[][] t =  new int[7][24];
    

    Each row represents one day (row 0 is Sunday, row 1 is Monday, etc.), and each column represents one hour (column 0 is 12AM, column 1 is 1AM, etc.). You may assume that the array is completely initialized with temperature readings.

    1. Write a Java code fragment that prints the minimum temperature on Friday.

    2. Write a Java code fragment that prints the average temperature at 2PM for the whole week.

    3. Write a Java code fragment that prints the number of readings that are at least 55 degrees for the entire set of temperature readings.

  2. (2 pts) A matrix m is symmetric if it is a square matrix and mij = mji for all valid i and j.

    For example, the first matrix below is symmetric while the second and third matrices are not:

    1  4  3  9         2  4  5  7         3  1  10  
    4  5  7 12         4  5  7  1         1  9   6
    3  7  8  0         5  7  1  6        10  8   4 
    9 12  0  2
    
    Symmetric          Not Symmetric     Not Symmetric
                       (not square)      (m[2][1] ≠ m[1][2])
    

    Complete the method isSymmetric that takes a two-dimensional array m of integers and returns true if the array m is symmetric and false otherwise. Complete the method so it performs only those computations that are absolutely necessary to determine the answer.

    public static boolean isSymmetric(int[][] m) {
    
    	int numRows = ___________________________;
    
    	int numColumns = ___________________________;
    
    	if (numRows != numColumns)
    
    		return false;
    
    	for (int row = ______; row < _______________; row++) {
    
    		for (int col = _______; col < ________________; col++) {
    
    			if (_________________________________________)
    
    				return false;
    		}
    
    	}
    
    	return true;
    
    }
    

    NOTE: Check your answer by tracing your method for the symmetric matrix given above. Include this with your answer.