algorithm described in java