title: find the color of the last bean in Java
body: <p>I have an assignment that is a coffeeTinGame program. I have written it but the result is not meeting my expectations. My program returns:</p>
  
  <blockquote>
    <p>Tin before : [G, B, B, G, G]<br>
    Tin after : [G, B, -, G, G]<br>
    Last bean:
    G</p>
  </blockquote>
  
  <p>I want the result look like this:</p>
  
  <blockquote>
    <p>Tin before : [G, B, B, G, G]
    Tin after : [-, -, -, -, G]
    Last bean: G</p>
  </blockquote>
  
  <p>What is wrong with it? Can somebody help me?</p>
  
  <pre><code> public class CoffeeTinGame
      {
          // constant value for green bean
          private static final char GREEN = 'G';
          // constant value for blue bean
          private static final char  BLUE = 'B';
          // constant for removed beans 
          private static final char REMOVED = '-';
  
            /**
            * the main procedure
            */
           public static void main(String [] args){
              // initialise somme beans
              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;
              // p0 = green parity /\ 
              // (p0=1 -&gt; last=GREEN) /\ (p0=0 -&gt; last=BLUE)
  
              // print the content of tin before the game
              putf("Tin before : %s %n", Arrays.toString(beans));
  
              // perform the game
              char lastBean = tinGame(beans);
              // lastBean = last \/ lastBean != last
  
              // print the content of tin and last bean
              putf("Tin after : %s %n", Arrays.toString(beans));
  
              // check if last bean as expected and print 
              if (lastBean == last) {
                  putf("Last bean: %c", lastBean);
              } else {
                  putf("Oops, wrong last bean: %c (expected: %c) %n", lastBean, last);
              }
           }
  
           /** 
            * Takes out any two beans randomly
            * @requirement: tin is not null /\ tin.length &gt; 0
            * @modifies tin
            * @effects: repeatedly remove two beans from tin
            *     create an array to store two beans
            *     that are taken out from tin
            *     takes out two beans randomly
            */
           private static int[] takeTwo(char[] tin){
               int[] b = new int[2];
               int count = tin.length;
              // takes one bean randomly
              // until length of tin &gt;= 2 and this bean is not remove
  
                   do {
                       b[0] = (int)(Math.random()*count);
                   } while (count &gt;= 2 &amp;&amp; tin[b[0]] == REMOVED);
                  // takes the next bean randomly
                  // until length of tin &gt;= 2 and this bean is not remove
                  // and this bean is different the first bean
                   do {
                       b[1] = (int)(Math.random()*count);
                   } while (count &gt;= 2 &amp;&amp; tin[b[1]] == REMOVED &amp;&amp; b[0] == b[1]); 
  
               return b;
           }
  
           /**
            * determine the color of the last bean
            * @requirement: tin is not null /\ tin.length &gt; 0
            * @effects: 
            *     {@link #takeTwo()}after taking out two beans
            *     if they are the same color 
            *         throw them both away
            *         put a Blue Mountain bean back in
            *     else 
            *         throw away the blue one
            *         put the green one back
            *     until the bean in the tin = 1
            */
            private static char[] updateTin(char[] tin){
           int b1, b2;
           // count is assigned to the length of tin
           int count = tin.length;
           /**
            * check condition: if two beans are the same color
            *     throw both away
            *     put Blue bean back 
            *  otherwise 
            *     put Green bean back
            */
           while (count &gt;= 2){
               int[] a = takeTwo(tin);
               // remove beans in tin
               b1 = tin[a[0]];
               tin[a[0]] = REMOVED;
               b2 = tin[a[1]];
               tin[a[1]] = REMOVED;
               if (b1 == BLUE &amp;&amp; b2 == BLUE || b1 == GREEN &amp;&amp; b2 == GREEN){
                   // put blue in tin
                   tin[a[0]] = BLUE;
               } else {
                   // put green in tin
                   tin[a[0]] = GREEN;
               }
  
               // reduce the lengrh of tin
               count = count-1;
          }
           // return the bean in the tin
           return tin;
       }
  
           /**
            * perform the coffee tin game
            * @requirement: tin is not null /\ tin.length &gt; 0
            * @effects:
            *     return color of the lastBean
            */
           private static char tinGame(char[] tin) {
               // invoke updateTin()
               char[] a = updateTin(tin);
               // initialise the last bean is removed
               char lastBean = REMOVED;
               int l = tin.length;
  
               for (int i = 0; i &lt; l; i++){
                   if (a[i] != REMOVED){
                       // a[i] is not remove
                       // lastBean is a[i]
                       lastBean = a[i];
                       break;
                   }
               }
               // return color of the lastBean
               return lastBean;
           }
      }
  </code></pre>