Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about assertion

 
Gaurav Chhabras
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic