This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

question about assertion

 
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.
 
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
 
He baked a muffin that stole my car! And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic