programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

# Example from Sun

Ranch Hand
Posts: 116
• • Number of slices to send:
Optional 'thank-you' note:
• • 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);

This topic has been discussed last days , i just cannot find thread.
Gonna be B and E???

Greenhorn
Posts: 15
• • Number of slices to send:
Optional 'thank-you' note:
• • I think b is incorrect. Math.round(double d) returns a long which would require an explicit cast to int as in c.

Ranch Hand
Posts: 167
• • Number of slices to send:
Optional 'thank-you' note:
• • i think E is correct too.

Ranch Hand
Posts: 116
• • Number of slices to send:
Optional 'thank-you' note:
• • b is corerect for sure, there is two methods , one returns int, second - long.
But what was really interesting for me - second answer, i found it today in class spesifications, it is e.

Scott Kiel
Greenhorn
Posts: 15
• • Number of slices to send:
Optional 'thank-you' note:
• • I agree the second part is the more interesting but for the first part b does not work. There are two methods for round but as I read the question it is not calling the method that returns an int. It is calling round with an argument that is a double. This returns a long which requires a cast to be squeezed into an int.

Ranch Hand
Posts: 56
• • Number of slices to send:
Optional 'thank-you' note:
• • C is correct.
E is also correct, as Math.floor() also gives 3.

Ranch Hand
Posts: 56
• • Number of slices to send:
Optional 'thank-you' note:
• • Could any one give me the link to Sun's examples we are talking?
-------------
Regards - Wahid [This message has been edited by Wahid Sadik (edited January 07, 2001).]

Ranch Hand
Posts: 116
• • Number of slices to send:
Optional 'thank-you' note:
• • Scott,
you right.
round(double) - Static method in class java.lang.Math
Returns the closest long to the argument.
round(float) - Static method in class java.lang.Math
Returns the closest int to the argument.

[This message has been edited by Vladimir Kositsky (edited January 07, 2001).]

Ranch Hand
Posts: 71
• • Number of slices to send:
Optional 'thank-you' note:
• • I think the answer is C & E.
C, because, without casting result would be 3.00 and with (int) cast result would be 3
E, because, without cast, result would be floor(3.14 + 0.5= 3.64), which equals 3.00 and with cast(int) result would be 3
Agree???
Thanks.
Nielsh look! it's a bird! it's a plane! It's .... a teeny tiny ad a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com