Solved

sample program

Posted on 2014-09-19
22
227 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
  • 10
  • 4
  • 3
  • +3
22 Comments
 
LVL 32

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
 
LVL 32

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 32

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now