Algorithm practice with java