Monday, October 10, 2011

Challenge 018

(1) double answer = (double) 13/5; will give him an answer of 2.6 because he is calling for the ddt of 13/5 which equals 2.6 because when you divide a double by an int, you get a double value.

(2) double answer = 13 / (double) 5; will give him an answer of 2.6 because we are naming 5 a ddt. When dividing an int by a double, you get a ddt answer.

(3) double answer = 13.0 / 5; will give him an answer of 2.6 because you again are dividing a double by an int, resulting in a double answer. When only 13.0 is stated, the compiler knows that this is intended to be a ddt.

(4) double answer = 13 / 5.0; will give him an answer of 2.6 because you are again dividing an int by a double, resulting in a ddt. When only 5.0 is stated, the compiler knows that this is intended to be a ddt.

(5) double answer = (double) (13/5); will NOT give him an answer of 2.6 because you are taking the quantity (13/5) and then taking the result and making it a double, with a result of 2.0

3 comments:

  1. Hi Ryan, just one item of follow-up, when we have something like:

    double answer = 13 / (double) 5

    Are we actually changing 5 to a double permanently?

    ReplyDelete
  2. We are not permanently making it a double, only acting as one to cause the output to become a double

    ReplyDelete
  3. Very good, thank you for the clarification here. 25 XPs awarded for this completion.

    ReplyDelete