Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

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
0
gudii9
Asked:
gudii9
  • 10
  • 4
  • 3
  • +3
1 Solution
 
it_saigeDeveloperCommented:
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-
0
 
ozoCommented:
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
0
 
krakatoaCommented:
They are either both smiling, OR both not smiling.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
it_saigeDeveloperCommented:
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 ;)
0
 
krakatoaCommented:
@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.
0
 
it_saigeDeveloperCommented:
I agree with krakatoa.

-saige-
0
 
gudii9Author Commented:
I have not clearly understood why saige answer is different or wrong. what is mistake in that. Please point it and advise.
0
 
ozoCommented:
(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]
0
 
krakatoaCommented:
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.
0
 
krakatoaCommented:
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.
0
 
krakatoaCommented:
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'.
0
 
CPColinCommented:
No it won't. Why would it?
0
 
krakatoaCommented:
No it won't. Why would it?

No?

Have you tried it?
0
 
CPColinCommented:
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

0
 
krakatoaCommented:
That was my bad. I missed the "!"

System.out.println(!aSmile&&!bSmile);
0
 
krakatoaCommented:
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.
0
 
CPColinCommented:
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.
0
 
krakatoaCommented:
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.
0
 
CPColinCommented:
In that case, I no longer have any idea what you're getting at.
0
 
krakatoaCommented:
That's fine. In any case, I digress.
0
 
CEHJCommented:
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 10
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now