aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  a question from Poddar's exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " a question from Poddar Watch " a question from Poddar New topic
Author

a question from Poddar's exam

shilpa gupta
Greenhorn

Joined: Feb 09, 2001
Posts: 26
this q is from poddar's mock test
{
float f4 = -5.5f;
float f5 = 5.5f;
float f6 = -5.49f;
float f7 = 5.49f;
System.out.println("Round f4 is " + Math.round(f4));
System.out.println("Round f5 is " + Math.round(f5));
System.out.println("Round f6 is " + Math.round(f6));
System.out.println("Round f7 is " + Math.round(f7));
}
and the ans is
1] Round f4 is -5
2] Round f5 is 6
3] Round f6 is -5
4] Round f7 is 5
i am unable to understand ans 1 & 3
please explain
Sivalingam Sivasuthan
Ranch Hand

Joined: Jan 12, 2001
Posts: 75
Hi shilpa gupta:
Math.round() method
Rounds to the nearest integer. So, if the value is more than half way towards the higher integer, the value is rounded up to the next ingeter. If the number is less than this the next lowest integer is returned.
So for example if the input to round is f then :
5.0 <=f < 5.5 then Math.round(f)==5.0
5.5 <=f < 6.0 then Math.round(f)==6.0
-5.0 <=f <= -5.5 then Math.round(f)==-5.0
-5.5 < f < -6.0 then Math.round(f)==-6.0

Siva.

- Siva<br /> Sun Certified Programmer for the Java™2 Platform
shilpa gupta
Greenhorn

Joined: Feb 09, 2001
Posts: 26
thank you sooo much siva
shilpa
Bala Arul
Greenhorn

Joined: Feb 09, 2001
Posts: 29
Hi Siva,
Originally posted by Sivalingam Sivasuthan:

-5.0 <=f <= -5.5 then Math.round(f)==-5.0
-5.5 < f < -6.0 then Math.round(f)==-6.0

There is not such number 'f' that,
-5.0 <=f <= -5.5 or -5.5 < f < -6.0,
since -5.0 is greater than -5.5 and -5.5 is greater than -6.0.
The correct way to find a rounded value is to find the floor value of given number plus 0.5.
So, floor(-5.5 + 0.5) = floor(-5.0) = -5.0
floor(-5.49 + 0.5) = floor(-4.99) = -5.0
NB: floor value of a number is defined as 'the highest integer value that is less than or equals to the given number.
... -7.0, -6.0, -5.0 are less than or equals to -4.99 or -5.00, but -5.0 is the highest.

Arul.

[This message has been edited by Bala Arul (edited February 16, 2001).]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi all,
The answers given above are correct with one noted exception. In Java the Math.round() method return either an integer (given float parameter) or a long (given double parameter). Therefore the statement:
Math.round(f) = X.0
is not technically correct in Java. It should be:
Math.round(f) = X
Regards,
Manfred.
Bala Arul
Greenhorn

Joined: Feb 09, 2001
Posts: 29
Thanks Manfred,
You are right. I used deciamals to explain mathematically, but forget to mention that there is no decimals with the return values.
Sivalingam Sivasuthan
Ranch Hand

Joined: Jan 12, 2001
Posts: 75
Thank you very much Bala Arul and Manfred for correcting me.
You both are correct.
Siva.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question from Poddar's exam