Return to lecture notes index

15-100 Lecture 20 (Friday, October 27, 2006)

Today's Quiz

//1. Declare and create an array of 5 int's.
int[] numbers;
numbers = new int[5];

//2. Store, within this array, the numbers 0 - 4.
numbers[0] = 0;
numbers[1] = 1;
numbers[2] = 2;
numbers[3] = 3;
numbers[4] = 4;

// ... or ...
for (int index=0; index < 5; index++)
  numbers[index] = index;


// 3. Print out (via access to the array) the content of the 3rd (0th, 1st, 2nd,   //    3rd, ...)item of the array
   System.out.println(numbers[3]);

Array Class Example

Today we just did an example class using an array. The important thing to note is that we can use an array with types other than primitives, in this case we are using a Car, which is an Object.


class ParkingLot {

  private Car[] spaces;
  
  private static final int DEFAULT_MAX_CARS = 25;
  private static final String NL = System.getProperty ("LINE_SEPARATOR");
 
 
  public ParkingLot(int maxNumberOfCars) {
    spaces = new Car[maximumNumberOfCars];
    
    for (int spaceNumber=0; spaceNumber < spaces.length; spaceNumber++)
      spaces[spaceNumber] = null;  
  }

  public ParkingLot() {
    spaces = new Car[DEFAULT_MAX_CARS];
   
    for (int spaceNumber=0; spaceNumber < spaces.length; spaceNumber++)
      spaces[spaceNumber] = null;
  }


  public int park(Car c) {
    int index;
    for (index=0; index < spaces.length; index++) {
      if (spaces[index] == null) break;
    }
    
    // Parking lot full ?
    if (index == spaces.length)
      throw FullException("All spaces are full.");
      
    //Have a space, so park
    spaces[index] = c;
    
    return index;
  }
  
  
  public void getCar(Car c, int spaceNumber) throws Exception {
    if ((spaceNumber < 0) || (spaceNumber >= spaces.length))
      throw new InvalidSpaceNumberException ("No space number: " +
                                              spaceNumber);
    if (!spaces[spaceNumber].equals(c))
      throw new IncorrectSpaceNumber("" + c + " not found in space " +  
                                     spaceNumber);      
    spaces[spaceNumber] = null;
  }


  public String toString() {
    String retString = "";
    
    for (int index=0; index < spaces.length; index++) {
      if (spaces[index] == null)
        retString += "[EMPTY]" + NL;
      else
        retString += spaces[index] + NL; 
    }
    
    return retString;
  }

  public boolean equals (Object o) {
    ParkingLot p = (ParkingLot) o;
    
    if (spaces.length != p.spaces.length)
      return false;
    
    for (int index=0; index < spaces.length; index++) 
      if (!spaces[index].equals(p.spaces[index])
        return false;
    
    return true;
  }
  
 }

class Car {
  private String type;
  private String driver;
  
  
  public Car (String type, String driver) {
    this.type = type;
    this.driver = driver;
  }
  
  
  public String getType() {
    return type;
  }
  
  
  public String getDriver() {
    return driver;
  }
  
  
  pubic String toString() {
    return "[" + type + ", " + driver + "]";
  }
  
  
  public boolean equals(Object o) {
    Car c = (Car) o;
    
    if (!driver.equals(c.driver))
      return false;
      
    if (!type.equals(c.type))
      return false;
      
    return true;
  }

}