1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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 !
silly.bear
November 5, 2007, November 05, 2007 08:03, permalink
Hi,
maybe this is the problem.
Fix it and it should work..
1 2 3 4 5 6 7 8 9 10
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"; } }
BelliOS
November 5, 2007, November 05, 2007 08:12, permalink
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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); } }
Mark Webb
November 5, 2007, November 05, 2007 08:38, permalink
I see, I didnt instantiate the array correctly. Thank you.
Marco Valtas
November 5, 2007, November 05, 2007 11:58, permalink
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.
Parks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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); } }
It compiles, although when I try to run it, I get an exception.