When the code for Challenge 10 is written in BlueJ™, an answer of 2.0 pops out. Initially, this makes no sense, as the equation appears to be d = √(4 + 5) ∕ 2, which should equal 2.1213… However, the variables for 4 and 5 have “int” before them in the code, forcing the equation to act like a Greatest Integer Function, which rounds the result to the greatest integer that is less than or equal to the nearest integer. Therefore, 2.1213 is rounded down to 2. When the phrase “int” is replaced with “double”, the result is 2.1213, supporting my hypothesis.
−Phoenix
Solid beginning, and you're nearly there.
ReplyDeleteLet's focus on this part: "forcing the equation to act like a Greatest Integer Function, which rounds the result to the greatest integer that is less than or equal to the nearest integer. Therefore, 2.1213 is rounded down to 2."
There is a technical inaccuracy here, and to demonstrate this, try running:
double n = 99/100;
System.out.println(n);
Revise your response from that point forward.
Thank You.
…However, the variables for 4 and 5 have “int” before them in the code, making their sum the “int” value 9. The next code segment is very important: because the value 2 in the statement “double d = Math.sqrt((m + n) / 2);” is written as “2” instead of “2.0”, the result of 9 ÷ 2 is the integer value “4”, rather than the double value “4.5”. The square root of the “int” value 4 is thus the “int” value 2, which is then converted to the double value 2.0. Had the code been instead written with a “2.0”, the answer would be the square root of the “double” value 4.5, or 2.1213…
ReplyDelete−Phoenix
Very good, thank you for this clarification. 15 XPs awarded.
ReplyDelete