write a java function that implements gray code and returns the next sequence code after a given code in a given direction of rotation