E41e6fdad12edcfb97ab662e83bfb9ab

It compiles, although when I try to run it, I get an exception.

public class Parks {
    private String[] parkingBays;
    private int numCars;

    public Parks(int capacity) {
        parkingBays = new String[numCars];

        for (int i = 0; i <= parkingBays.length; i++) {
            parkingBays[i] = "Empty";
        }
    }
    public boolean park(String car) {
        
        if (numCars > parkingBays.length){
            parkingBays[++numCars] = car;
        }
        else {
            return false;
        }
        return true;
    }


    /**
     * For Testing
     */
    public static void main(String [] args) {
        Parks p = new Parks(20);
        boolean spaces = true;
        while (spaces) {
            spaces = p.park("car1");
            spaces = p.park("car2");
        }
        for (String car : p.parkingBays)
            System.out.println(car);
    }
}

Refactorings

No refactoring yet !

995e3bfeeaae71d6edcf0ecb96bdf786

silly.bear

November 5, 2007, November 05, 2007 08:03, permalink

3 ratings. Login to rate!

Hi,

maybe this is the problem.

Fix it and it should work..

public Parks(int capacity) 
{
  //use capacity not numCars that should be inited to 0.
  //(before) parkingBays = new String[numCars];
  parkingBays = new String[capacity];

  for (int i = 0; i <= parkingBays.length; i++) {
    parkingBays[i] = "Empty";
  }
}
Eae2eab9d7bc165641ffd5250aff7677

BelliOS

November 5, 2007, November 05, 2007 08:12, permalink

2 ratings. Login to rate!
public class Parks {
    private String[] parkingBays;
    private int numCars;

    public Parks(int capacity) {
        parkingBays = new String[capacity];

        for (int i = 0; i < parkingBays.length; i++) {
            parkingBays[i] = "Empty";
        }
        //parkingBays = new String[numCars];
        //
        //for (int i = 0; i <= parkingBays.length; i++) {
        //	parkingBays[i] = "Empty"; //<-- parkingBays is zero and try to add one elem 
        //}
    }
    public boolean park(String car) {
        
        if (numCars < parkingBays.length){
            parkingBays[numCars++] = car;
        }
        else {
            return false;
        }
        return true;
    }


    /**
     * For Testing
     */
    public static void main(String [] args) {
        Parks p = new Parks(20);
        boolean spaces = true;
        while (spaces) {
            spaces = p.park("car1");
            spaces = p.park("car2");
        }
        for (String car : p.parkingBays)
            System.out.println(car);
    }
}
E41e6fdad12edcfb97ab662e83bfb9ab

Mark Webb

November 5, 2007, November 05, 2007 08:38, permalink

No rating. Login to rate!

I see, I didnt instantiate the array correctly. Thank you.

0706636fd5e30fa66019d7ffacdb5b11

Marco Valtas

November 5, 2007, November 05, 2007 11:58, permalink

2 ratings. Login to rate!

Here is my corrections they are identified in the code.

You got your contructor argument wrong, the lenght of a array should be tested with a "<" not a "<=" since arrays in java start at zero, but the length have the size. So a array as "new String[10]" will have 10 elements starting at 0 to 9, That was your first ArrayOutOfBoundsException.

The park() method tested with a ">", but should be "<" for the same reason as above.

And finally "numCars" should be incremented with "numCars++", because integers has a default of "0", so you want park the car then increment, not increment then park the car.

Hope this helps.

public class Parks {
    private String[] parkingBays;
    private int numCars;

    public Parks(int capacity) {
        parkingBays = new String[capacity]; // changed "numCars" to "capacity"

        for (int i = 0; i < parkingBays.length; i++) { // changed "<=" to "<"
            parkingBays[i] = "Empty";
        }
    }
    public boolean park(String car) {
        
        if (numCars < parkingBays.length){ // changed ">" to "<"
            parkingBays[numCars++] = car; // changed "++numCars" to "numCars++"
        }
        else {
            return false;
        }
        return true;
    }


    /**
     * For Testing
     */
    public static void main(String [] args) {
        Parks p = new Parks(20);
        boolean spaces = true;
        while (spaces) {
            spaces = p.park("car1");
            spaces = p.park("car2");
        }
        for (String car : p.parkingBays)
            System.out.println(car);
    }
}

Your refactoring





Format Copy from initial code

or Cancel