Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

comparing sign of two numbers

Hi,
I was going through below link
http://codingbat.com/prob/p192082



I wrote solution as below

public boolean icyHot(int temp1, int temp2) {
  if(temp1<0 && temp2>100)
  {
  return true;
  }
 
   if(temp1>0 && temp2<100)
  {
  return true;
  }
  return false;
}



I saw given solution as below


public boolean icyHot(int temp1, int temp2) {
  if ((temp1 < 0 && temp2 > 100) || (temp1 > 100 && temp2 < 0)) {
    return true;
  } else {
    return false;
  }
  // Could be written as: return ((temp1 < 0 && ...));
}

can the natural flow of next line can be written always after || (which is the only difference i see from my program.

Please advise
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you please clarify your question?
Avatar of gudii9

ASKER

public boolean icyHot(int temp1, int temp2) {


if( (a) || (b) ){
  return true;
}


}

Open in new window


why above code wont compile?  Giving return statement in if loop alone is not sufficient. Do i need to give outside if loop also?
please advise.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Because you didn't define a and b

(The equivalent form would also not compile for equivalent reason)

I see no loop in your code, so I don't understand your last comment.
Avatar of dpearson
dpearson

Your original solution is fine and just as good as the "given solution" on the website.

As you said, the only difference is that they put the two conditions together with an "||" (or) and you broke them out into separate lines.  Either approach is fine.

I think you misunderstood what ozo posted.  He was basically saying that the two solutions are equivalent (he just said it in code).

Doug