Search code examples
javaloopsmethodsoutputnew-operator

Output doesn't as expected in java code problem


I am new in java. I am working on a problem to fill the corral with snails which is out to pasture based on random swing direction. But output is not as expected.

package fill;

import java.util.Random;

public class FillTheCorral extends Gate {

    public static final int sRANDOM_SEED=1234;

    private static final int sMAX_GATES=4;

    public static void main (String[] args) {
        Random randomNumber=new Random(sRANDOM_SEED);
        FillTheCorral mFillTheCorral=new FillTheCorral();
        Gate[] corral=new Gate[sMAX_GATES];
        for (int i=0; i<corral.length; i++) {
            corral[i]=new Gate();
        }
        do {
            mFillTheCorral.setCorralGates(corral , randomNumber);
        } while (!mFillTheCorral.anyCorralAvailable(corral));
    }
    
    public void setCorralGates(Gate[] gate, Random selectDirection) {
        System.out.println("Initial gate setup:");
        for(int i=0;i<gate.length;i++){
            int randDir = selectDirection.nextInt(3)-1; 
            gate[i].setSwing(randDir);
            System.out.println("Gate " + i + ": "+ randDir);
        }
    }

    public boolean anyCorralAvailable(Gate[] corral) {
        for(int i=0;i<corral.length;i++){
            if(corral[i].getSwingDirection() == IN)
                return true;
        }
        return false ;
    }
}

class Gate {
    public static final int OUT=-1;
    public static final int IN=1;
    public static final int CLOSED=0;
    private static int mSwing;

    public static
    int getSwingDirection () {
        return mSwing;
    }

    public static boolean setSwing (int dir) {
        mSwing=dir;
        if (mSwing == IN) return true;
        if (mSwing == OUT) return true;
        if (mSwing == CLOSED) return true;
        return false;
    }

    public int thru (int count) {
        if (getSwingDirection() == IN) {
            return +count;
        } else if (getSwingDirection() == OUT) {
            return -count;
        } else {
              count*=0;
              return count;
        }
    }
} 

Expected output : Initial gate setup: Gate 0: 1 Gate 1: 1 Gate 2: 1 Gate 3: -1

Actual output:

Initial gate setup: Gate 0: 1 Gate 1: 1 Gate 2: 1 Gate 3: -1

Initial gate setup: Gate 0: 1 Gate 1: -1 Gate 2: 0 Gate 3: 0

Initial gate setup: Gate 0: -1 Gate 1: 0 Gate 2: 0 Gate 3: 1

I am getting x3 times gate random direction.


Solution

  • Your problem is that in your Gate class you defined the field mSwing ands its getters/setters as static. A static field only exists once per class, meaning all your 4 created Gate objects will share the same value for mSwing instead of every gate having its own value for mSwing.

    For mor information please read: What does the 'static' keyword do in a class?

    If you change the field to a normal non-static one you will get the output you expect:

    private int mSwing;
    
    public int getSwingDirection () {
        return mSwing;
    }
    
    public boolean setSwing (int dir) {
        mSwing=dir;
        if (mSwing == IN) return true;
        if (mSwing == OUT) return true;
        if (mSwing == CLOSED) return true;
        return false;
    }