adding algorithm in java