• 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

Math class

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Dan Chisholm's Mock exam

Question: Which of the boolean variables will never be initialized to the value true?

class Math7 {
public static void main (String[] args) {
double d1 = Math.random();
boolean b1 = (d1 < 0.0), b2 = (d1 <= 0.0), b3 = (d1 == 0.0);
boolean b4 = (d1 >= 0.0), b5 = (d1 < 1.0), b6 = (d1 <= 1.0);
boolean b7 = (d1 == 1.0), b8 = (d1 >= 1.0), b9 = (d1 > 1.0);
//System.out.println("d1:"+d1);
System.out.println("b1:"+b1);
System.out.println("b2:"+b2);
System.out.println("b3:"+b3);
System.out.println("b4:"+b4);
System.out.println("b5:"+b5);
System.out.println("b6:"+b6);
System.out.println("b7:"+b7);
System.out.println("b8:"+b8);
System.out.println("b9:"+b9);
}
}

I executed the above code and got results as

b1:false
b2:false
b3:false
b4:true
b5:true
b6:true
b7:false
b8:false
b9:false

My Question

1. As per specification the Random value produced will be greater than or equal to 0.0 and less than 1.0.

(b6) But here less than or equal to 1.0 is also is evaluated to true. Why is it so?
(b3) Why equals to zero is producing a false result?

Someone please explain this.

2. In the answers column of Dan Chisholm's Mock exam the answers provided are(b1 b7 b8 b9). I think they are wrong.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1. As per specification the Random value produced will be greater than or equal to 0.0 and less than 1.0.

(b6) But here less than or equal to 1.0 is also is evaluated to true. Why is it so?
(b3) Why equals to zero is producing a false result?


What do you want to ask on this question? In general terms, or specific to your output?

As you quote already, a random number MUST be less than 1.0, and thus, the condition for (d1 <= 1.0) is always true, because the value is equal OR less than 1.0 always. Your false result on b3 is just one of the cases, if the number generated by random is greater than 0.0, then b3 of course equals to false! Only if the random number equals to 0.0 and then b3 will be true.


2. In the answers column of Dan Chisholm's Mock exam the answers provided are(b1 b7 b8 b9). I think they are wrong.


Why you think they are wrong? The question is asking which variable is NEVER TRUE! (which means always false).

The random number is defined by: 0.0 <= random number < 1.0

b1 = (d1 < 0.0) Always false by definition
b2 = (d1 <= 0.0) Equals to true when random number = 0.0
b3 = (d1 == 0.0) Same as b2
b4 = (d1 >= 0.0) Always true by definition
b5 = (d1 < 1.0)[b] Always true by definition
[b]b6 = (d1 <= 1.0)
Always true by definition (as it is a OR condition)
b7 = (d1 == 1.0) Always false by definition (always < 1.0)
b8 = (d1 >= 1.0) Always false by definition (always < 1.0)
b9 = (d1 > 1.0) Always false by definition (always < 1.0)

Hope this help.

Nick
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This shows the difference between a theoretical answer and a practical one. The odds that a random 64 bit double (0.0 <= d < 1.0) will be exactly 0.0 are smaller than my chances of winning the lottery (and I don't even buy lottery tickets).
[ December 20, 2004: Message edited by: Mike Gershman ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic