Okay so my program shows kids getting on a bus and sitting in a specific order. Then two kids are supposed to swap seats in which it'll change the order. After that two kids leave and another gets on the bus. This is what the program does. Everything works find except my swap method in which two kids are supposed to switch seats.
Here's the code:
public class Bus
{
private String names[] = new String[10];
public void getOn(String name)
{
int i = findFirstEmpty();
if (i >= 0)
{
names[i] = name;
}
else
{
System.out.println("The bus is at maximum capacity");
}
}
public int findFirstEmpty()
{
for (int i = 0; i <= names.length; i++){
if (names[i] == null){
return i;
}
}
return -1;
}
public void print()
{
for (int i = 0; i < 10; i++)
{
if (names[i] != null){
System.out.println(i + " , " + names[i]);
}
}
}
public void getOff(String name)
{
for(int i=0;i<10;i++)
{
if (name.equals(names[i])){
names[i] = null;
}
}
}
public void swap(String name1, String name2)
{
String temp = name1;
name1 = name2;
name2 = temp;
}
public static void main(String[] args)
{
System.out.println("The bus pulls up and the children load onto it.");
System.out.println();
Bus bus = new Bus();
String name = "Joe";
bus.getOn(name);
name = "Jeff";
bus.getOn(name);
name = "Erica";
bus.getOn(name);
name = "Bob";
bus.getOn(name);
System.out.print("After loading the children onto the bus, this is who the bus contains: ");
System.out.println();
bus.print();
System.out.println();
bus.swap("Jeff", "Bob");
System.out.print("After swapping Jeff's and Bob's seats, this is who remains on the bus: ");
System.out.println();
bus.print();
System.out.println();
name = "Erica";
bus.getOff(name);
name = "Bob";
bus.getOff(name);
System.out.print("After Erica and Bob exited the bus, this is who remains: ");
System.out.println();
bus.print();
name = "Nancy";
bus.getOn(name);
System.out.println();
System.out.print("After Nancy enters the bus, this is who the bus contains: ");
System.out.println();
bus.print();
}
}
Here's what it prints out:
The bus pulls up and the children load onto it.
After loading the children onto the bus, this is who the bus contains: 0 , Joe 1 , Jeff 2 , Erica 3 , Bob
After swapping Jeff's and Bob's seats, this is who remains on the bus: 0 , Joe 1 , Jeff 2 , Erica 3 , Bob
After Erica and Bob exited the bus, this is who remains: 0 , Joe 1 , Jeff
After Nancy enters the bus, this is who the bus contains: 0 , Joe 1 , Jeff 2 , Nancy
notice Jeff and Bob are supposed to swap, but they don't. Can anyone help me out i've been trying to figure it out, but im not sure whats wrong.