wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ceil(),round() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ceil(),round()" Watch "ceil(),round()" New topic
Author

ceil(),round()

Renuka Kilambi
Greenhorn

Joined: Apr 21, 2000
Posts: 23
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
Helen Yu
Greenhorn

Joined: Jul 13, 2000
Posts: 29
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).]


hyby2000@yahoo.com
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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
Hi Helelen and Ajith,
Thanks a lot for ur replies
Renuka
mita
Ranch Hand

Joined: Jul 19, 2000
Posts: 50
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
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
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 .

try it , and feel free to correct me.
vivek
Ken Lai
Ranch Hand

Joined: Aug 06, 2000
Posts: 30
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
OOPS! That was a bummer, thanks for noticing. I have edited my post
Ajith
MahaAdd
Greenhorn

Joined: Aug 28, 2000
Posts: 28
Hi,
is this -2 or 2 ??
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.

mehrar
Greenhorn

Joined: Aug 16, 2000
Posts: 19
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.


Raj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ceil(),round()
 
Similar Threads
== question
doubt in multiple assignment !
SCJP Mock Exam Question
I don't understand the result of this question, could somebody please explain me?
Operators and Assignments??