gemini88mill gemini88mill - 6 months ago 10
Java Question

Why does my recursion fall through?

My recursive function is falling through in my program. I am making a method that constructs a relatively simple object array and populates them with certain values. i.e. object 1 has these values 3,2,5,6,7, object 2 has these values; 4,5,6,4,5. and so on

the recursion comes in when I have different parts of the method that do different things to the function. as shown below:

objectConstructor(Object foo, int switcherVar){
if(switcherVar == 1){
//terminating condition leave method
return 1;
} else {
if(switcherVar == 2){
//do something
objectConstructor(object foo, 1)
}
}
return 0;
}


When I check my return value i get a 0. The stuff that I'm actually doing in the method is unrelated to my recursive function and the function IS re cursing just at the end it falls through when its supposed to jump to the terminating condition. From my understanding the problem is the way I'm formatting my recursive function.

Below is the actual code It's an airplane seat constructor that give values to an airplanes seats, like if it is occupied and whatnot. the above is easier to read but if my syntax is off that might be the problem as well.

private int airplaneSeatConstructor(Airplane airplane, String className, int numberOfSeats){
/*Airplane Seat Creator, loops through the seats and attaches credentials to them based on the type of plane
* being formed.*/

//currently only one plane type. 777.
//777 seat number 257
//does have a first class section.
System.out.println("iteration");
if(className.equals("TERM")){
return 1;
}else {
if (className.equals("FIRST")) {
for (int x = 0; x < numberOfSeats; x++) {
airplane.getSeats()[x].setOccupied(false);
airplane.getSeats()[x].setFirstClass(true);
airplane.getSeats()[x].setBusinessClass(false);

if ((x % 4) == 0 || (x % 4) == 3) {
airplane.getSeats()[x].setWindowseat(true);
airplane.getSeats()[x].setAisleSeat(false);
} else {
airplane.getSeats()[x].setAisleSeat(true);
airplane.getSeats()[x].setWindowseat(false);
}
}
System.out.println("in first");
airplaneSeatConstructor(airplane, "BUSINESS", 40);
}
if (className.equals("BUSINESS")) {
for (int x = 0; x < numberOfSeats; x++) {
airplane.getSeats()[airplane.getNumberOfSeatsPerClass()[0] + x].setBusinessClass(true);
airplane.getSeats()[airplane.getNumberOfSeatsPerClass()[0] + x].setFirstClass(false);
airplane.getSeats()[airplane.getNumberOfSeatsPerClass()[0] + x].setOccupied(false);
}
System.out.println("in business");
airplaneSeatConstructor(airplane, "ECONOMY", 209);
}
if (className.equals("ECONOMY")) {
for (int x = 0; x < numberOfSeats; x++) {
airplane.getSeats()[airplane.getNumberOfSeatsPerClass()[0] + airplane.getNumberOfSeatsPerClass()[1] + x].setBusinessClass(false);
airplane.getSeats()[airplane.getNumberOfSeatsPerClass()[0] + airplane.getNumberOfSeatsPerClass()[1] + x].setFirstClass(false);
airplane.getSeats()[airplane.getNumberOfSeatsPerClass()[0] + airplane.getNumberOfSeatsPerClass()[1] + x].setOccupied(false);
}
System.out.println("in economy");
airplaneSeatConstructor(airplane, "SPECIAL", 26);
}
if (className.equals("SPECIAL")) {
System.out.println("in special");
airplaneSeatConstructor(airplane, "TERM", 273);
}
}
return 0;
}


my print lines all hit but I'm still getting 0 from my return value.

Answer

In your code, whatever recursion you do, in fact, whatever calculation you do is not going to count, since you're finally returning 0 for all cases but the base one (in which you return 1).

objectConstructor(Object foo, int switcherVar){
    if(switcherVar == 1){
        //terminating condition leave method
        return 1;
    } else {
        if(switcherVar == 2){
        //do something
        objectConstructor(object foo, 1)
        }
    }
    return 0;
}

Recursion in your program works this way: you do the first call with switcher == 2, which makes the recursive call whit switcher == 1. But then you discard that result and just return 0.

The correct or logic way of things for doing this is more similar to this:

objectConstructor(Object foo, int switcherVar){
    if(switcherVar == 1){
        //terminating condition leave method
        return 1;
    } else {
        if(switcherVar == 2){
        //do something
        return objectConstructor(object foo, 1)
        }
    }
}

Hope this helps.

However, looking closely to your code, I think that you are substituting sequence by recursion. I would refactor (i.e., divide the code of) your function by classes of seats, and make the needed calls. Recursion is not needed at all in your code. See below:

private void airplaneSeatConstructorFirstClass(Airplane airplane, int numberOfSeats)
{
            for (int x = 0; x < numberOfSeats; x++) {
                airplane.getSeats()[x].setOccupied(false);
                airplane.getSeats()[x].setFirstClass(true);
                airplane.getSeats()[x].setBusinessClass(false);

                if ((x % 4) == 0 || (x % 4) == 3) {
                    airplane.getSeats()[x].setWindowseat(true);
                    airplane.getSeats()[x].setAisleSeat(false);
                } else {
                    airplane.getSeats()[x].setAisleSeat(true);
                    airplane.getSeats()[x].setWindowseat(false);
                }
            }
}

private void airplaneSeatConstructorBussinessClass(Airplane airplane, int numberOfSeats)
{
    for (int x = 0; x < numberOfSeats; x++) {
                airplane.getSeats()[airplane.getNumberOfSeatsPerClass()[0] + x].setBusinessClass(true);
                airplane.getSeats()[airplane.getNumberOfSeatsPerClass()[0] + x].setFirstClass(false);
                airplane.getSeats()[airplane.getNumberOfSeatsPerClass()[0] + x].setOccupied(false);
            }
}

...and so on.

Now you just have to call:

airplaneSeatConstructorFirstClass( airplane, 80 );
airplaneSeatConstructorBussinessClass( airplane, 40 );

As you can see is far easier (unless I'm missing something big).