Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 5)
5.
What will be the output of the program?
public class Example 
{
    public static void main(String [] args) 
    {
        double values[] = {-2.3, -1.0, 0.25, 4};
        int cnt = 0;
        for (int x=0; x < values.length; x++) 
        {
            if (Math.round(values[x] + .5) == Math.ceil(values[x])) 
            {
                ++cnt;
            }
        }
        System.out.println("same results " + cnt + " time(s)");
    }
}
same results 0 time(s)
same results 2 time(s)
same results 4 time(s)
Compilation fails.
Answer: Option
Explanation:

Math.round() adds .5 to the argument then performs a floor(). Since the code adds an additional .5 before round() is called, it's as if we are adding 1 then doing a floor(). The values that start out as integer values will in effect be incremented by 1 on the round() side but not on the ceil() side, and the noninteger values will end up equal.

Discussion:
11 comments Page 1 of 2.

Saurabh said:   1 decade ago
======math.floor example ======

double E1 = Math.floor(14.2345);

double E2 = Math.floor(14.5345);

double E3 = Math.floor(-14.2345);

double E4 = Math.floor(-14.5345);

System.out.println("Math floor value :"+E1);
System.out.println("Math floor value :"+E2);
System.out.println("Math floor value :"+E3);
System.out.println("Math floor value :"+E4);

// o/p

Math floor value :14.0
Math floor value :14.0
Math floor value :-15.0
Math floor value :-15.0


======math.ceil example ======


double E1 = Math.ceil(15.156);

double E2 = Math.ceil(-15.156);

double E3 = Math.ceil(15);
double E4 = Math.ceil(-0.65);


System.out.println("Math ceil value :"+E1);
System.out.println("Math ceil value :"+E2);
System.out.println("Math ceil value :"+E3);
System.out.println("Math ceil value :"+E4);


O/P:

Math ceil value :16.0
Math ceil value :-15.0
Math ceil value :15.0
Math ceil value :-0.0

1st loop

-2.3+.5 = -1.8 converted into -2 == -2.3 convert into 3 .

-2 == 3 false .

cnt = 0.

2nd time.

-1.0 +.5= -0.5 converted into -1.0 == - 1 .0.

cnt = 1.

3 rd.

.25+ .5 = .75 convered into 0 == .25 converted into 1

cnt = 0.

4th.

4 + .5 =4.5 converted into 4 == 4

cnt = 2.

John said:   8 years ago
First Loop:

(x=0; 0<4; x++) // true.
(-2.3 plus 0.5) with round function = -2 == (-2.3) with ceiling function = -2.0.
-2 == -2.0 = True so cnt =1.

Second Loop:

(x=1; 1<4; x++) // true
(-1.0 plus 0.5) with round function = 0 == (-1.0) with ceiling function = -1.0.
0 == -1.0 = False so cnt =1.

Third Loop:

(x=2; 2<4; x++) // true
(0.25 plus 0.5) with round function = 1 == (0.25) with ceiling function is 1.0.
1 == 1.0 = True, so cnt=2.

Fourth Loop:

(x=3; 3<4; x++) // true
(4.0 plus 0.5) with round function = 5 == (4.0) with ceiling function is 4.0.
5 == 4 = False, so cnt=2.

Fifth Loop:

(x=4; 4<4; x++) // false ( 4<4 ) so, loop finish.
Print: same results 2 time(s).
(1)

Swathi said:   1 decade ago
Loop Math.round(values[x] + .5) Math.ceil(values[x]) Convert
--------------------------------------------------------------------------------

1. -2.3+0.5 = -1.8 = -2 -2.3 = -2.0 1.

2. -1.0+0.5 = -0.5 = 0 -1.0 = -1.0 1+0 = 1.

3. 0.25+0.5 = 0.75 = 1 0.25 = 1 1+1 = 2.

4. 4+0.5 = 4.5 = 5 4 = 4 2+0 = 2.
(1)

Canan said:   8 years ago
-2.3 plus 0.5 with round function is -2.
-2.3 with ceiling function is -2.0.
-1.0 plus 0.5 with round function is 0.
-1.0 with ceiling function is -1.0.
0.25 plus 0.5 with round function is 1.
0.25 with ceiling function is 1.0.
4.0 plus 0.5 with round function is 5.
4.0 with ceiling function is 4.0.

Same results 2 time(s).

Austin said:   1 decade ago
But doesn't java permit floating point numbers with decimal point only between two digits?

Is .5 a valid float no in java?

Just this doubt in my mind.

Mayur said:   1 decade ago
Please give a step by step explanation for this problem.

Sagar said:   8 years ago
I am not understanding this, please explain in detail.

Abhijit said:   1 decade ago
I also need a step by step explanation.

Tamizh said:   9 years ago
Please give clear explanation for me.

Subha said:   1 decade ago
I need a clear explanation for this


Post your comments here:

Your comments will be displayed after verification.