Forums Register Login

question about assertion

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 757 times.
Similar Threads
questions about assert
New Assertions Exam
Assertions Doubts
how to compile java source code with assertion ?
class invariant ,internal invariant
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:03:24.