Implementing Bnakers Algorithm in java