Monday, 6 November 2017

java methods theory problems



Question 04
Write some rounding functions
Ans
ceil(x)          x is rounded up to its nearest integer. This integer is returned as a double value.
floor(x)            x is rounded down to its nearest integer. This integer is returned as a double value.
rint(x) x           is rounded up to its nearest integer. If x is equally close to two integers, the even one is returned as a double value.
round(x)              Returns (int)Math.floor(x + 0.5) if x is a float and returns (long)Math.floor(x + 0.5) if x is a double.
Question 05
Give some examples of  rounding functions
Ans
Math.ceil(2.1) returns 3.0
Math.ceil(2.0) returns 2.0
Math.ceil(-2.0) returns -2.0
Math.ceil(-2.1) returns -2.0
Math.floor(2.1) returns 2.0
Math.floor(2.0) returns 2.0
Math.floor(-2.0) returns –2.0
Math.floor(-2.1) returns -4.0
Math.rint(2.1) returns 2.0
Math.rint(-2.0) returns –2.0
Math.rint(-2.1) returns -2.0
Math.rint(2.5) returns 2.0
Math.rint(4.5) returns 4.0
Math.rint(-2.5) returns -2.0
Math.round(2.6f) returns 3 // Returns int
Math.round(2.0) returns 2 // Returns long
Math.round(-2.0f) returns -2 // Returns int
Math.round(-2.6) returns -3 // Returns long
Math.round(-2.4) returns -2
Question 06
What is abs() method
Ans
It covert positive number into negative number
Question 07
Give some example of abs()
Ans
Math.abs(-9)  returns  9
Math.abs(-2.3) returns 2.3

No comments:

Post a Comment