# Thread: i dont understand :(

1. Member
Join Date
Jun 2013
Posts
71
Rep Power
0

## i dont understand :(

Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative.
Java Code:
```public boolean posNeg(int a, int b, boolean negative) {
if(negative==true && a<0 && b<0) {
return true;
}
if ((a>0 && b<0) ||( a<0 && b>0) ) {
return true;
}
return false;
}```
Expected Run
posNeg(1, -1, false) → true true OK
posNeg(-1, 1, false) → true true OK
posNeg(-4, -5, true) → true true OK
posNeg(-4, -5, false) → false false OK
posNeg(-4, 5, false) → true true OK
posNeg(-4, 5, true) → false true X
posNeg(1, 1, false) → false false OK
posNeg(-1, -1, false) → false false OK
posNeg(1, -1, true) → false true X
posNeg(-1, 1, true) → false true X
posNeg(1, 1, true) → false false OK
posNeg(-1, -1, true) → true true OK
posNeg(5, -5, false) → true true OK
posNeg(-6, 6, false) → true true OK
posNeg(-5, -6, false) → false false OK
posNeg(-2, -1, false) → false false OK
posNeg(1, 2, false) → false false OK
posNeg(-5, 6, true) → false true X
posNeg(-5, -5, true) → true true OK

I dont get why im wrong. For example "posNeg(-5, 6, true) → false true X", : first two if statements doesnt fully satisfy (-5, 6, true), so it should go to the last statement which should run false. BUT IT RUNS TRUE! Why is that?

2. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Re: i dont understand :(

here is your prompt from codingbat:

"Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative."

Lets write this out in psuedocode:

if(negative is true) {
return ((numberOne < 0) and (numberTwo < 0)) // this is a boolean expression
} otherwise {
return ( (( numberOne < 0) and (numberTwo > 0)) or (( numberTwo < 0) and (numberOne > 0)) ) //this is a boolean expression
}

"I dont get why im wrong. For example "posNeg(-5, 6, true) → false true X", : first two if statements doesnt fully satisfy (-5, 6, true), so it should go to the last statement which should run false. BUT IT RUNS TRUE! Why is that?"

Java Code:
```public boolean posNeg(int a, int b, boolean negative) {
if(negative==true && a<0 && b<0) {
return true;
}
if ((a>0 && b<0) ||( a<0 && b>0) ) {
return true;
}
return false;
}```

The difference between your code and the psuedocode is a single word that is key here - an "else."
Try plugging your numbers into your function on paper and seeing how your function is handling them.
Last edited by AlexGraal; 05-01-2014 at 05:59 AM.

3. Senior Member
Join Date
Feb 2014
Posts
447
Rep Power
7

## Re: i dont understand :(

Alex pointed out a good solution and explanation but I have the feeling that the explanation of the wrong results are a little "hidden".

When we analyze your code, then you will first see the test for the case: negative is true and both values are negative.
But if that is not true, you only check that one is negative and one is positive. You no longer check the negative argument.

So I agree to Alex solution. But maybe it is easier to understand if you split up the return statements to a if / else statement with return true / return false (Which is the same. If you have an if (condition) { return true; } else { return false; } then it is the same as return condition;)

Another solution could be to add a check for negative==false to your second if statement if you prefer it that way (which I find harder to read and understand but it is your decision).

4. Member
Join Date
Jun 2013
Posts
71
Rep Power
0

## Re: i dont understand :(

thannnnnnnnkkkkkkkkk youuuuuuuuuuuu alllllllllllllllllllllllllllllllllllllllllllllllll lllllllllllllllll

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•