Ad
  • Custom User Avatar

    If you ain't using 3 loops, you ain't trying!

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    Sometimes the answer is right in front of you... Looking at this makes me feel a bit silly about my solution :D

  • Custom User Avatar

    You shoul calculate percent/100 before while. And you might add +1. i.e. percent = percent/100 + 1. Cause x + x*0.1 = x.1.1

  • Custom User Avatar

    One small performance change. I would calculate "percent / 100D" before the while loop in order to prevent the slight extra work added for each pass. The rest is much cleaner than mine :)

  • Custom User Avatar

    From the perspective of the code that calls the method it doesn't matter at all, in Java parameters are always passed by value. Whether method code should be allowed to reassign parameter variables or not is primarily opinion based or just a matter of taste. With short code like this I personally don't see more disadvantages when modifying p0 compared to declaring another variable which might be considered additional noise. And with rather long or complex method code you have a different problem anyway: You're unable to quickly understand what it does anyway.

  • Default User Avatar

    I agree. The arguments should remain consistent throughout the function and a local variable used in-favour of modifying the argument.

  • Default User Avatar

    @AndreaLaforgia, in practice you should never "expect" too much about code; anything not declared final is modifiable.

    IMO best practice is make code intent clearer by getting into habit of using final modifier wherever appropriate - eg Unless you are going to change p0 then it should be declared final p0.

  • Custom User Avatar

    +1 for using the += operation to cast to int and auto floor it! We can't increase the population by 0.25 of a person.