Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

sample program

Hi,

I was going through below program
http://codingbat.com/prob/p181646

I have not understood difference between first and second expression below. (to me both say same thing)

  //   return ((aSmile && bSmile) || (!aSmile && !bSmile));

Please advise
Avatar of it_saige
it_saige
Flag of United States of America image

They do essentially mean the same thing.

(true && true) [true] OR (not true && not true) [true]
(true && false) [false] OR (not true && not false) [false]
(false && false) [true] OR (not false && not false) [true]

-saige-
Are you referring to
 (aSmile && bSmile)  
as the "first", and
 (!aSmile && !bSmile)
as the "second expression"?

 (aSmile && bSmile)  is true when aSmile and bSmile are both true
 (!aSmile && !bSmile) is true when aSmile and bSmile are both false
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My mistake.  ozo and krakatoa are spot on.  As krakatoa states, they are either both smiling OR both not smiling.

-saige-

(ozo and krakatoa are both smiling) while me (not smiling).  LOL ;)
@gudii9

I don't mean to be pedantic, and as 'nice' of you as it is to dish out points to everyone who has commented, the point of EE is that it is a resource for posterity, and future reference. Imagine if someone learning Java opens this question and thinks that saige's comment is correct (which he admits was a mistake, but someone might not read that far down).

You should try not to award points to wrong answers, as it pollutes the resource. I am not worried about getting more points for myself or anything mercenary like that, but you have to appreciate that things need to be kept as correct as possible, as programming, even when it does work, is already complicated enough without setting up phantom scenarios for ourselves above and beyond that. Thanks.
I agree with krakatoa.

-saige-
Avatar of gudii9

ASKER

I have not clearly understood why saige answer is different or wrong. what is mistake in that. Please point it and advise.
(true && true) [true] OR (not true && not true) [true]
(false && false) [true] OR (not false && not false) [true]
(not true && not true)  is not [true]
(false && false) is not [true]
class Truth {

static boolean aSmile;
static boolean bSmile;

public static void main(String[] args){

aSmile = true;
bSmile = true;

System.out.println("The truth is that the statement ((aSmile && bSmile) || (!aSmile && !bSmile)) is "+tellTruth()+" because aSmile is presently "+aSmile+" and bSmile is presently "+bSmile);

System.out.println("\n But now if we change aSmile to false, then we get this result: \n");

aSmile = false;

System.out.println("\nThe truth is that the statement ((aSmile && bSmile) || (!aSmile && !bSmile)) is "+tellTruth()+" because aSmile is presently "+aSmile+" and bSmile is presently "+bSmile);

System.out.println("\n. . whereas if we change bSmile to false, and set aSmile back to true again, we get :");

aSmile = true;
bSmile = false;

System.out.println("\nThe truth is that the statement ((aSmile && bSmile) || (!aSmile && !bSmile)) is "+tellTruth()+" because aSmile is presently "+aSmile+" and bSmile is presently "+bSmile);

aSmile = false;
bSmile = false;

System.out.println("\n. . And when both aSmile and bSmile are false then this : ");

System.out.println("\nThe truth is that the statement ((aSmile && bSmile) || (!aSmile && !bSmile)) is "+tellTruth()+" because aSmile is presently "+aSmile+" and bSmile is presently "+bSmile);


}

static boolean tellTruth(){return ((aSmile && bSmile) || (!aSmile && !bSmile));}


}

Open in new window


so you can see that the truth only comes out when either both are smiling, or both not smiling.

:)
------------------------------------------------------
I hate to say it, but this all depends on whether you want or need a "true" value. Just because the logic works like that, it doesn't mean that would be right in any particular scenario. In other words, understanding the truth table might not help understanding where the values came from, and whether they should be true or false.
gudii9 :

the simple answer is that the expressions in brackets on each side of the OR (||) operator both need to EVALUATE to true to get a TRUE value for that side of the entire statement. But then the "return" value is the result of asking whether at least one of the sides of the whole expression actually evaluated to TRUE, which is done by OR-ing them.

The difficult part is remembering that when you have "!aSmile", that if aSmile is FALSE, then !aSmile evaluates to TRUE.
One final thing :

the "dreadful" situation you have to look out for also, is this :

aSmile = false;
bSmile = false;
System.out.println(aSmile&&bSmile);
System.out.println(false&&false);

Open in new window


the first println will produce the result 'true'.
the second one will print 'false'.
No it won't. Why would it?
No it won't. Why would it?

No?

Have you tried it?
I just ran this code:

public class BooleanTest
{
   public static void main(String[] args)
   {
      boolean aSmile = false;
      boolean bSmile = false;
      System.out.println(aSmile&&bSmile);
      System.out.println(false&&false);
   }
}

Open in new window


It produced this output:

false
false

Open in new window

That was my bad. I missed the "!"

System.out.println(!aSmile&&!bSmile);
But philosophically, you have to ask yourself whether an expression like !aSmile&&!bSmile should be returned as false, even if both of them are false, because the truth of the expression is really that they are both false, which in itself is true. Even (false&&false) should be considered true, as again, they hold equal values. I know this does not hold for Boolean values, but if you were to Englishify such a statement, into something like : "I have no money, and you have no money, therefore it is true that between us we have no money", you wouldn't want the final clause in that statement to read ", and therefore it is not true that we have no money between us" instead.
Even (false&&false) should be considered true, as again, they hold equal values.

(false == false) should be considered true (and is). Not (false && false).

I have no money, and you have no money, therefore it is true that between us we have no money.

In this case, you're equating the boolean value false with the state of "having no money." You have to keep that consistent on both sides of the equation.
Your requotes are a bit out of context - I acknowledge that in computing terms it works that way, but I'm talking about everyday logic. And afaics the 'having no money' situation *is* equal on both sides of the equation - both sides of that 'and'. IOW, it's "true" that both conditions are false.
In that case, I no longer have any idea what you're getting at.
That's fine. In any case, I digress.
That was my bad. I missed the "!"

System.out.println(!aSmile&&!bSmile);

Open in new window


That is indeed a wholly different kettle of fish and will result in true (since your code set both to false). That's probably the cause of the confusion