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