Since I used some bitwise operations, I might as well briefly explain a little about what they mean. First of all, I defined 4 constant integers:
public final int UP=1, DOWN=2, LEFT=4, RIGHT=8;
This is common practice where something needs a value but it's not any basic data type. I'm using these for directions. The question is, why did I pick 1, 2, 4, and 8? Why not 1, 2, 3, and 4 or 0, 1, 2 and 3? The reason is the binary representations of the numbers:

1

1

2

10

4

100

8

1000

Notice each number has exactly one '1' and they're in different places. Now, about basic bitwise operations- I used | (OR) and & (AND). What happens when you bitwise-OR two numbers together is if either one of the numbers has a 1 in that spot, so will the result. If both numbers have a 0 in that spot, the result will have a 0. Example:
9 | 5 = 1001 | 0101 = 1101 = 13
When you bitwise-AND two numbers together, BOTH of the numbers must have a 1 in a position for the result to be 1 in that position, and if EITHER number has a 0 in a position, the result will have a 0 in that postion. Example:
9 & 5 = 1001 & 0101 = 0001 = 1
In my program, I do this to identify directions. First of all, a |= b means a = a | b. The same thing can be done with the & operation. So it seems like the bounce operation can only take in 4 possible integers, but in reality it can take in 16. It will only effectively process 8, though. Let's take a look at the code again:

First of all, what I'm doing is called 'bitmasking'. If you bitwise AND anything with 0, you get 0. for the first if statement, if the last bit is a 1, the answer will be 1, which is greater than 0, so it's up. If the last bit is 0, then direction & UP will be 0, and it will go to the else statement. The else statement bitmasks the second to last bit by ANDing with 0010. If the second to the last bit of direction is 1, then the result will be greater than 0 and it will bounce down. Notice by using the else statement, I don't allow it to bounce both up and down at the same time, even if both the last two bits are 1's- it will just choose up because I checked that first. After that it checks direction&LEFT, which will be greater than 0 if the third to last bit is 1. Else, it checks the 4th bit from the right by doing the bitwise AND on direction and RIGHT. So, let's summarize here: