we are doing the following programming exercise: Death by Coffee.
The main task is to convert from integers to hexadecimals and add values. We have written the following code:
public class Dinglemouse {
  public static int[] coffeeLimits /*☕*/ (final int year, final int month, final int day) {
    System.out.println("year: "+year);
    System.out.println("month: "+month);
    System.out.println("day: "+day);
    long healthNumber = Long.parseLong(String.valueOf(year)+String.valueOf(month)+String.valueOf(day));
    System.out.println("healthNumber: "+healthNumber);
    long cafe = Long.valueOf("CAFE",16);
    long decaf = Long.valueOf("DECAF",16);
    int cafeLimit = getLimit(cafe, healthNumber);
    System.out.println("\ncafeLimit: "+cafeLimit);
    int decafLimit = getLimit(decaf, healthNumber);
    System.out.println("\ndecafLimit: "+decafLimit);
    return new int []{cafeLimit, decafLimit};
  }
  public static int getLimit(long coffee, long healthNumber){
    int limit=0;
    while(limit<=5000 && !Long.toHexString(healthNumber).contains("dead")){
      limit++;
      healthNumber+=coffee;
      System.out.println("new healthNumber: "+Long.toHexString(healthNumber));
    }
    return limit>5000 ? 0 : limit;    
  }
}
We wonder why the code passes the following tests, except exJohn:
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.*;
public class Tests {
  // Show returned limits
  private static int[] show(final int y, final int m, final int d, final int[] result) {
    System.out.println(String.format("%4d%02d%02d -> ",y,m,d)+Arrays.toString(result));
    return result;
  }
  @Test
  public void exJohn() {
    final int y=1950, m=1, d=19;
    assertArrayEquals(new int[]{2645,1162}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  }
  @Test
  public void exSusan() {
    final int y=1965, m=12, d=11;
    assertArrayEquals(new int[]{111,0}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  }
  @Test
  public void exElizabeth() {
    final int y=1964, m=11, d=28;
    assertArrayEquals(new int[]{0,11}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  }
  @Test
  public void exPeter() {
    final int y=1965, m=9, d=4;
    assertArrayEquals(new int[]{0,0}, show(y,m,d,Dinglemouse.coffeeLimits(y,m,d)));
  }
}
We have printed what we get inside getLimit's loop, and we see that the last healthNumber being output for cafe limit is:
...
new healthNumber: 2dc4cbb
new healthNumber: 2dd17b9
new healthNumber: 2dde2b7
new healthNumber: 2deadb5
cafeLimit: 889
And for the decaf limit we have:
...
new healthNumber: 10ff8a241
new healthNumber: 110068ef0
new healthNumber: 110147b9f
new healthNumber: 11022684e
decafLimit: 0
So then our code's result is: [889, 0]
When it should be: [2645, 1162]
We thought it could be caused by int overflow so we changed all variables to long, however the program behaves equal.
We have read:
 
    