• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing boolean logic expression

 
Callie Jones
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am studying for an exam and after looking at a past paper I found out that we are being tested on something we haven't been taught in our course.

I don't have a clue how to tackle the question so I am wondering if anyone could explain what kind of method you should use? I don't know anything about maths/logic so idiot-level explanations would be appreciated! All we got taught was that Java has these boolean operators - but no information about how to work with them.

The question is:

Which two Java boolean expressions are equivalent to this expression?



Select two from the following:

 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "not" operator can be applied through the rest of the expression, reversing all the operators: || becomes &&, < becomes >=, etc.

If you reverse the expression

(count > 2) || (count <= 1)

You get

(count <= 2) && (count > 1)

which is D, since && does not depend on the order of its operands. The tricky part is reversing everything.

You can check it by evaluating what integer(s) will fit both expressions -- in this case, only the integer 2 fits the expression, assuming they mean to be talking about integers.

rc
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should check this out: De Morgan's laws
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the JavaRanch!
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule Ralph mentioned is called De Morgan's rule. (Which Wouter already mentioned...)

Now, not everybody knows that one. You could have checked all the possible solutions, limiting your possible values from 0 to 2; anything < 0 is handled by the 0 case, and anything >= 2 is handled by the 2 case. So the original expression would return true only if count == 2. You then check all of the other expressions and check out which other only returns true for count == 2. And that's two: D and E.

A is wrong because of the ||; it would return true for, well, any value of count. Likewise for B. C would return true for only count == 1. F is essentially the same as C.

E is right because it's the direct application of De Morgan's rule:
!((count > 2) || (count <= 1))
is the same as, De Morgan
!(count > 2) && !(count <= 1)
is the same as, && is associative
!(count <= 1) && !(count > 2) (answer E)
is the same as
(count > 1) && (count <= 2) (answer D)


Well actually, there is one flaw in this logic. && is a shortcut operator. That means that the right operand will not be evaluated if the left one is already true. Therefore, the associative swapping isn't always allowed. In this case there is no problem with it though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic