Algorithm described in Java