my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about assertion" Watch "question about assertion" New topic

question about assertion

Gaurav Chhabras
Ranch Hand

Joined: Sep 21, 2005
Posts: 126
class E {
private boolean b1, b2, b3;
public void setB1(boolean b) {b1 = b;}
public void setB2(boolean b) {b2 = b;}
public void setB3(boolean b) {b3 = b;}
public void m1 (int i) {
b2 = i % 2 == 0;
if (!b3 & !b2 & !b1) {System.out.print("A");
} else if (!b3 & !b2 & b1) {System.out.print("B");
} else if (!b3 & b2 & !b1) {System.out.print("C");
} else { // Only b3 is true.
assert b3 & !b2 & !b1;
System.out.print(b1 + "," + b2 + "," + b3);
b1 = b2 = b3 = false;
public static void main (String[] args) {
E e = new E(); e.setB1(true); e.m1(2);

its one answer is -:
The combination of the if/else statements and the assert statement indicate that the programmer expects no more than one boolean, b1, b2 or b3, to be true.

Please clarify me that how it is possible.
Ashwin Kumar

Joined: Oct 13, 2005
Posts: 27
Consider this

f (!b3 & !b2 & !b1) {System.out.print("A");
} else if (!b3 & !b2 & b1) {System.out.print("B");
} else if (!b3 & b2 & !b1) {System.out.print("C");
} else { // Only b3 is true.
assert b3 & !b2 & !b1;

"A" will be printed - If all 3 values are false
"B" will be printed - If b1 is true and b2,b3 are false
"C" will be printed - If b2 is true and b1,b3 are false
Assertion Error will not be thrown - If b3 is true and b1,b2 is false.

In all other cases an Assertion Error will be thrown (Assume that b1,b2 is true and b3 is false, then an Assertion Error will be thrown). So the programmer is trying to assert that not more than 1 value is true at any given time.
Hope it is clear

SCJP 1.4<br />SCBCD 5.0<br />SCWCD 1.4 (Preparing)
I agree. Here's the link:
subject: question about assertion
It's not a secret anymore!