Here is an example of the return statement in question:

public boolean either24(int[] nums) {
boolean n1 = false;
boolean n2 = false;

  for (int i=0;i<nums.length-1;i++)
  
   if (nums[i]==2 && nums[i+1]==2) 
          n1 = true;
    else
    if (nums[i]==4 && nums[i+1]==4)
    n2 = true;
    
    return !n1 && n2 || n1 && !n2; //<----what is this really doing??
}

What does that line really mean?

Recommended Answers

All 20 Replies

Here is an example of the return statement in question:

public boolean either24(int[] nums) {
boolean n1 = false;
boolean n2 = false;

  for (int i=0;i<nums.length-1;i++)
  
   if (nums[i]==2 && nums[i+1]==2) 
          n1 = true;
    else
    if (nums[i]==4 && nums[i+1]==4)
    n2 = true;
    
    return !n1 && n2 || n1 && !n2; //<----what is this really doing??
}

What does that line really mean?

I haven't tested it yet but wouldn't it mean:

return false and true OR return true and false

I haven't tested it yet but wouldn't it mean:

return false and true OR return true and false

i know that...but how is it determining which to choose?

! = not
&& = and
|| = or;
Check on this:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/opsummary.html
hope it helps

yeah thats essentially what I have said:

!n1 means: NOT TRUE, SO FALSE
as n1 is assigned the value of true within the loop

&& = AND

n2 is assigned the value of FALSE unless it goes to the else statement.

It keeps going according to this pattern.

!n1 is true if n1 is false and vice-versa!!!

i know what the !n1 means...but how is the return statement choosing which one?

it is saying return false and true OR true and false.
How does it know which one to choose? Only one boolean type comes out from that, either its true, or its false?

look at that:

.....
boolean firstAnd =!n1 && n2;
boolean secondAnd=n& && !n2; 
boolean soloOr=firstAnd || secondAnd;
return soloOr;
...
}

look at that:

.....
boolean firstAnd =!n1 && n2;
boolean secondAnd=n& && !n2; 
boolean soloOr=firstAnd || secondAnd;
return soloOr;
...
}

So its returning whichever one is true? Ugh..i dont know why i cant get this.

No, it's returning the end result of the entire conditional. It will return either true or false.

i just dont get how it can tell if firstAnd || secondAnd; is either true or false. What exactly is it looking for?
if firstAnd is true OR secondAnd is true?<---- this?
if firstAnd is false OR secondAnd is false?<----or this? Or both?

Ok:
If firstAnd egale false AND secondAnd egale false the false result is false
otherwise the result is true.

John,

boolean whatever = first || second;

The above expression will store "true" into the variable "whatever" under the condition that first is true or second is true. If both "first" is false and "second" is false, it will store "false" into "whatever". So, to summarize, unless both "first" and "second" are false, the entire expression evaluates to true, and true gets stored into the variable whatever.

Ok:
If firstAnd egale false AND secondAnd egale false the false result is false
otherwise the result is true.

oh ok so if they are both the same value, say both false, then the result is fale? otherwise if it is false/true or true/false, the result is true?

what if they are both true? is the result true?

if they are both the same value, say both false, then the result is false? otherwise if it is false/true or true/false, the result is true?

what if they are both true? is the result true?

Exactly, everything you just said is correct. See my last post in this thread .

Ok I get it now

I understand now what you mean:
In all cases you function return false guess why??
n1 and n2 are complementary (only n1 or n2 will be true but but not both in the same time; if the two adjacent "cell" in the arry are egale to 2 or 4) in that case you function will return false. because n1 and n2 :
!n1 && n2 is always FALSE
n1 AND!n2 is allways false
so fase OR false is false.

if the to adjacent cases in the array enum are not egale 2 or 4
the result is false because n1 and n2 are initialize to false by defaulte.

It that what you mean?

Sorry I make a mistak
This is the Xor function of n1 and n2

This is not true.
this is !(n1 && n2).

!a && b || a && !b is effectivaly the representation of the Xor logic function.

the Xor function return false if the two values are both true or both false;
The only case where the two values are equal; in our case; is when the two addjacent "cell" are both not egal to 2 or 4;
If it is so we can simply write:

.....
 boolean result =(((nums[i]==2 && nums[i+1]==2)||(nums[i]==4 && nums[i+1]==4))==true)? true: false;
      return result;

this wil gives the same result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.