Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sample program

Posted on 2014-09-19
22
Medium Priority
?
235 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 2000 total points
ID: 40332906
They are either both smiling, OR both not smiling.
0
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

715 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