aspose file tools*
The moose likes Beginning Java and the fly likes comparing boolean logic expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "comparing boolean logic expression" Watch "comparing boolean logic expression" New topic
Author

comparing boolean logic expression

Callie Jones
Greenhorn

Joined: May 30, 2011
Posts: 1
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

Joined: May 29, 2005
Posts: 479
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

Joined: Oct 25, 2008
Posts: 2700

You should check this out: De Morgan's laws


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And welcome to the JavaRanch!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing boolean logic expression