This week's book giveaway is in the Android forum. We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line! See this thread for details.

Hi everybody, This is a question from javaprepare sample exam. What gets written on the screen when the following program is compiled and run. Select the one right answer. public class test { public static void main(String args[]) { int i; float f = 2.3f; double d = 2.7; i = ((int)Math.ceil(f)) * ((int)Math.round(d));

System.out.println(i); } }

a. 4 b. 5 c. 6 d. 6.1 e. 9 The answer is e. I compiled the above code and got the answer 9. can anybody explain how it is to be calculated. Thanks in advance. Renuka

Originally posted by Renuka Kilambi: Hi everybody, This is a question from javaprepare sample exam. What gets written on the screen when the following program is compiled and run. Select the one right answer. public class test { public static void main(String args[]) { int i; float f = 2.3f; double d = 2.7; i = ((int)Math.ceil(f)) * ((int)Math.round(d));

System.out.println(i); } }

a. 4 b. 5 c. 6 d. 6.1 e. 9 The answer is e. I compiled the above code and got the answer 9. can anybody explain how it is to be calculated. Thanks in advance. Renuka

Hi,there: Let me try to help. ----------------------------------------------------- i = ((int)Math.ceil(f)) * ((int)Math.round(d));

step1: Math.ceil(f)=Math.ceil(2.3) will return a double value which is 3.0, we need the expilicty casting to int, so we get 3; step2: Math.round(d)=Math.round(2.7) will return a long value which is 3L, we also need to cast it into int, so we get 3, Step3: do 3*3; you will get 9. Hope this would help

[This message has been edited by Helen Yu (edited August 04, 2000).]

Renuka, ceil gives you the next highest number in the number-scale. In your case, the ceil of 2.3 is the next highest number, which is 3. round gives you the next highest number if the decimal point is 5 or greater( a fractional part which is more than 1/2 ), and the next lowest number if the decimal point is less than 5. Hence round of 2.5 is the same as round of 2.7, which is 3. Round of 2.4 is the same as round of 2.2 which is 2. In your example, ceil(f) returns 3 and round(d) returns 3. 3 * 3 = 9 and hence the answer. Note that it is slightly tricky to figure out ceil, floor and round for negative numbers. The best thing to do is to imagine the number-scale and the position of the given number in the scale. For ceil you always move to the next number towards the RIGHT, and for floor move towards the next number towards the LEFT on the scale. So, ceil of -2.3 is -2 moving RIGHT on the scale. Similarly, floor of -2.3 is -3 moving LEFT on the scale. Ajith [This message has been edited by Ajith Kallambella (edited August 04, 2000).]

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).

Renuka Kilambi
Greenhorn

Joined: Apr 21, 2000
Posts: 23

posted

0

Hi Helelen and Ajith, Thanks a lot for ur replies Renuka

Hi Ajith, As you said . While applying round for negative numbers, ignore the sign, apply the rule and apply the sign again!. How come the round of 2.5 & -2.5 gives 3 & -2. Please explain if I am missing something. Thanks in advance. Mita

Hi mita, I would like to change little bit to what ajith said:

round gives you the next highest number if the decimal point is 5 or greater( a fractional part which is more than 1/2 ), and the next lowest number if the decimal point is less than 5. While applying round for negative numbers, ignore the sign, apply the rule and apply the sign again!.

I would like to change the first statement little bit for negative numbers only. round gives you the next highest number if the decimal point is greater than 5( a fractional part which is more than 1/2 ), and the next lowest number if the decimal point is 5 or less .

Hi, For Math.round(d) use this to determine the outcome: (return_type)(d + 0.5) so when you have d = -2.5 the result will be: (return_type)(-2.5 + 0.5) = 2 Note: This is an overloaded method, so if d is of type double return_type would be long, if d is of type float then return_type would be int. Hope this helps.

Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782

posted

0

OOPS! That was a bummer, thanks for noticing. I have edited my post Ajith

Originally posted by Ken Lai: Hi, For Math.round(d) use this to determine the outcome: (return_type)(d + 0.5) so when you have d = -2.5 the result will be: (return_type)(-2.5 + 0.5) = 2 Note: This is an overloaded method, so if d is of type double return_type would be long, if d is of type float then return_type would be int. Hope this helps.

It should be -2. Cause when you round a number if the decimal is greater then 5 then it number is converted to the one higher in the scale. In the scale the negative numbers decrease to zero and positive numbers increase from zero, which means that for -2.5 the next higher will be -2. Hope this helps.