• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

k&b doubt 2

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi in chapter 6...

in one question...


Given the following,
1. public class Example {
2. public static void main(String [] args) {
3. double values[] = {-2.3, -1.0, 0.25, 4};
4. int cnt = 0;
5. for (int x=0; x < values.length; x++) {
6. if (Math.round(values[x] + .5) == Math.ceil(values[x])) {
7. ++cnt;
8. }
9. }
10. System.out.println("same results " + cnt + " time(s)");
11. }
12. }
what is the result?
A. same results 0 time(s)
B. same results 2 time(s)
C. same results 4 time(s)
D. Compilation fails.
E. An exception is thrown at runtime.


its anser is "B"

but don't understand why... i am not able to understand its explaination...
kindly clear the doubt...

thanx
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to run this code.

[ May 10, 2005: Message edited by: Srinivasa Raghavan ]
 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amit taneja:
hi in chapter 6...

in one question...


Given the following,
1. public class Example {
2. public static void main(String [] args) {
3. double values[] = {-2.3, -1.0, 0.25, 4};
4. int cnt = 0;
5. for (int x=0; x < values.length; x++) {
6. if (Math.round(values[x] + .5) == Math.ceil(values[x])) {
7. ++cnt;
8. }
9. }
10. System.out.println("same results " + cnt + " time(s)");
11. }
12. }
what is the result?
A. same results 0 time(s)
B. same results 2 time(s)
C. same results 4 time(s)
D. Compilation fails.
E. An exception is thrown at runtime.


its anser is "B"

but don't understand why... i am not able to understand its explaination...
kindly clear the doubt...

thanx



The logic is as follows:

-2.3 and 0.25 are the correct answers.

For -2.3:

(-2.3 + .5) == -1.7
-1.7 Rounded == -2.0

-2.3 Ceiled == -2

Therefore -2.0 == 2


For 0.25:

(0.25 + .5) == 0.75
0.75 Rounded == 1.0

0.25 Ceiled == 1

Therefore 1.0 == 1


-1.0 is not correct because:

(-1.0 + .5) == -.5
-.5 Rounded == 0

-1.0 Ceiled == -1.0

Therefore 0 != -1.0


4 Is incorrect because:

(4 + .5) == 4.5
4.5 Rounded == 5

4 Ceiled == 4

Therefore 4 != 5
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Therefore -2.0 == 2


It should be -2.0 == -2.0
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasa Raghavan:

It should be -2.0 == -2.0



No, it should be -2.0 == 2

From Java Language Specification (chapter 5.6.2):


If either operand is of type double, the other is converted to double.



Therefore 2 is promoted to be 2.0 and the two results are equal.

You can run the following code for testing:

 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep i agree this

If either operand is of type double, the other is converted to double.

But it should be negative right or am i going wrong any where ?
I mean -2.0 == -2
[ May 10, 2005: Message edited by: Srinivasa Raghavan ]
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx
[ May 10, 2005: Message edited by: amit taneja ]
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasa Raghavan:
Yep i agree this

If either operand is of type double, the other is converted to double.

But it should be negative right or am i going wrong any where ?
I mean -2.0 == -2

[ May 10, 2005: Message edited by: Srinivasa Raghavan ]



Yes it should be negative. Sorry for the typo
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic