Algorithm practice in Java