7. Given double pi = Math.PI; Which two are valid ways to round pi to an int?(Choose two.) A.int p = pi; B.int p = Math.round(pi); C.int p = (int)Math.round(pi); D.int p = (int)Math.min(pi + 0.5d); E.int p = (int)Math.floor(pi + 0.5d);

//CORRECT ANSWER IS C AND E, WHY IS E CORRECT ?? WHAT IS THE d mean ?

floor() returns the largest double value that is not greater than argument of floor() and is equal to an integer. d is used to signify double value though it is not needed as floating values are implicitly double it is given just confuse. Ashish

choice d gives an error b'coz it has a min method() which gives a minimum value of 2 but here is only a one value,it just adding a double value in pi. sonal

answer c and e is basically the same. The API's definition for Math.round() is (long)Math.floor(a+0.5d);

Sun Certified Java Programmer

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232

posted

0

thanks for the effort guys, but my question was WHY IS E CORRECT ? i did not get an answer to that, i know how the floor works, but why is the value of pi, added to 0.5d, what is 0.5d, where does it come from , what does it represent ? if it is a double value of 0.5, then after performing round, we would get 4 as a result and not 3, explain ?

Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133

posted

0

You perform floor(not round) after adding 0.5d, that's why it has the same effect as rounding numbers. The round method comes from floor. Hope this helps.

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232

posted

0

i am still not clear on this, why is the 0.5d value used, and wouldnt the result after rounding in this case be 4, instead of 3 ?

Tom Tang
Ranch Hand

Joined: Dec 24, 2000
Posts: 133

posted

0

0.5d value is used because it can achieve the rounding effect with floor. Choice e is :int p = (int)Math.floor(pi + 0.5d); No round method is used here and p will be floored to 3 just like the round method in choice c.

D is incorrect bcos Math.min(a,b) takes 2 arguments where a,b can be double , int ,long or float. but here it takes only one arguments.. HTH sunil.s ------------------ "Winners don't do different things They do things differently"

"Winners don't do different things<br /> They do things differently"