Solved

sample program

Posted on 2014-09-19
22
232 Views
Last Modified: 2014-09-22
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
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 4
  • 3
  • +3
22 Comments
 
LVL 34

Expert Comment

by:it_saige
ID: 40332640
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
 
LVL 84

Expert Comment

by:ozo
ID: 40332669
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
 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
ID: 40332906
They are either both smiling, OR both not smiling.
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 34

Expert Comment

by:it_saige
ID: 40332968
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 40333244
@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
 
LVL 34

Expert Comment

by:it_saige
ID: 40333291
I agree with krakatoa.

-saige-
0
 
LVL 7

Author Comment

by:gudii9
ID: 40335596
I have not clearly understood why saige answer is different or wrong. what is mistake in that. Please point it and advise.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40335613
(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
 
LVL 16

Expert Comment

by:krakatoa
ID: 40335721
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 40336197
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 40336863
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
 
LVL 14

Expert Comment

by:CPColin
ID: 40336870
No it won't. Why would it?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40336907
No it won't. Why would it?

No?

Have you tried it?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 40336913
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 40336925
That was my bad. I missed the "!"

System.out.println(!aSmile&&!bSmile);
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40336973
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
 
LVL 14

Expert Comment

by:CPColin
ID: 40337004
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 40337034
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
 
LVL 14

Expert Comment

by:CPColin
ID: 40337046
In that case, I no longer have any idea what you're getting at.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40337124
That's fine. In any case, I digress.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40337374
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question