Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2013
    Posts
    71
    Rep Power
    0

    Angry 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. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default 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
    }

    Now, your problem:
    "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?"

    Let's look at your code:

    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. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    7

    Default 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).

    Konrad

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

    Default Re: i dont understand :(

    thannnnnnnnkkkkkkkkk youuuuuuuuuuuu alllllllllllllllllllllllllllllllllllllllllllllllll lllllllllllllllll

Similar Threads

  1. I dont understand this error, help please
    By Miller786 in forum New To Java
    Replies: 7
    Last Post: 02-01-2014, 06:31 PM
  2. I dont understand this question...
    By Akirien in forum New To Java
    Replies: 8
    Last Post: 08-07-2012, 07:56 PM
  3. HELP ...DONT UNDERSTAND Error
    By ask4soteria in forum New To Java
    Replies: 6
    Last Post: 11-26-2010, 09:43 AM
  4. Replies: 3
    Last Post: 07-28-2010, 02:49 PM
  5. Dont understand Return Statement.
    By ocean in forum New To Java
    Replies: 6
    Last Post: 10-22-2009, 12:06 PM

Posting Permissions

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