0

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.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571

6 Answers6

1
public void swap(String name1, String name2)
{
        String temp = name1;
        name1 = name2;
        name2 = temp;
}

Here you are swapping the values of local variables name1 and name2. It doesn't affect your names array, so this doesn't actually do anything.

You need to actually access the names array, find the existing positions in the array of name1 and name2, and swap them in the array.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
1

Java is not like C/C++ which you can pass object by reference. Therefore, in order to swap two objects, you can wrap your name string in a class called Person, and then do the following:

public void swap(Person p1, Person p2)
{
        String temp = p1.name;
        p1.name = p2.name;
        p2.name = temp;
}
tonga
  • 11,749
  • 25
  • 75
  • 96
  • We are suppose to use the method the way i have it written down, so i'm not sure how to make it work like this –  Feb 19 '14 at 02:33
  • 1
    If you're not allowed to change the swap method signature like in your post, then there is no way to do the swap in Java. – tonga Feb 19 '14 at 02:39
  • sure there is - look up the positions of the strings in the array. – Blorgbeard Feb 19 '14 at 18:49
1

Since, Java is strictly pass-by-value, the swapping done inside swap() method is not reflected to the calling method.

You could re-write the swap method as follows:

public void swap(String name1, String name2) {

        int position1 = -1;
        int position2 = -1;

        for (int i = 0; i < 10; i++) {

            if (name1.equals(names[i])) {
                position1 = i;
            }

            if (name2.equals(names[i])) {
                position2 = i;
            }
        }

        if ( position1 != -1 && position2 != -1 ) {

            String temp = names[position1];
            names[position1] = names[position2];
            names[position2] = temp;
         }
    }
Manoj Shrestha
  • 4,246
  • 5
  • 47
  • 67
0

You should look here Is Java "pass-by-reference" or "pass-by-value"?, bearing in mind that strings are immutable objects.

Community
  • 1
  • 1
John3136
  • 28,809
  • 4
  • 51
  • 69
0

The only way to do array swapping in Java is something like this:

public void swap(Object[] array, int i, int j) {
  // TODO: validate i and j...
  Object iVal = array[i];
  array[i] = array[j];
  array[j] = iVal;
}

There is no other way around it. It's a little awkward and on top of that it won't work for built in types (int, double, float, byte, char, long) but only for their boxed counterparts, requiring specialized versions for built in types (just like in java.util.Arrays).

As a side note, it's worth noting that Object[] is a supertype of all the arrays of objects inheriting from Object, i.e., String[] is a subtype of Object[], but byte[] is not.

Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94
-3

I suggest to use StringBuilder objects

public void swap(StringBuilder name1, StringBuilder name2)
{
        StringBuilder temp = name1;
        name1 = name2;
        name2 = temp;
}
ManZzup
  • 526
  • 4
  • 12