I have write a program to determine the color of last bean from a tin (char[] beans). Beans in a tin are only green or blue.The rule is: If two beans taken out are both greens or blues, put 1 blue bean back, else put the green one back. Repeat until there are only 1 bean left. I don't know whether my code is right or not.public class CoffeeTinGame {    /** constant value for the green bean*/    private static final char GREEN = 'G';    /** constant value for the blue bean*/    private static final char BLUE = 'B';    /** constant for removed beans */    private static final char REMOVED = '-';    public static void main(String[] args) {        char[] beans = { GREEN, BLUE, BLUE, GREEN, GREEN };        // count number of greens        int greens = 0;        for (char b : beans) {             if (b == GREEN)                greens++;        }        // the expected last bean        final char last = (greens % 2 == 1) ? GREEN : BLUE;        // print the content of tin before the game        TextIO.putf(tin before: %s %n, Arrays.toString(beans));        // perform the game        char lastBean = tinGame(beans);        // print the content of tin and last bean        TextIO.putf(tin after: %s %n, Arrays.toString(beans));        // check if last bean as expected and print         if (lastBean == last) {             TextIO.putf(last bean: %c , lastBean);              } else {            TextIO.putf(Oops, wrong last bean: %c (expected: %c)%n,lastBean,last);        }    }    private static int takeTwo(char[] tin)  {        do {            int bi1= (int)(Math.random()*tin.length);         } while (tin[bi1] != REMOVED);        return b1;        do {            int bi2= (int)(Math.random()*tinLength);        } while (tin[bi2]!=REMOVED || bi2!=bi1);        return bi2;    }    private static char tinGame(char[] tin) {            int count = tin.length;        while (count >= 2) {            // remove b1, b2 from tin            int bi1, bi2= takeTwo(beans);            b1 = tin[bi1];                         b2 = tin[bi2];                      tin[bi1] = REMOVED;             tin[bi2] = REMOVED;                 if (b1 == BLUE && b2 == BLUE|| b1 == GREEN && b2 == GREEN) {                 // put B in bin                tin[bi2] = BLUE;                    } else { // BG, GB                    // put G in bin                tin[bi2] = GREEN;               }            count = count-1;                }        return tin;    }}
Determine the color of last bean in a tin
java