This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Hi,

I want to check to see if given integers digits a, b are both positive or both negative. How do i do that in java.

Please advise. Any sample code, resources highly appreciated. thanks in advance.

I want to check to see if given integers digits a, b are both positive or both negative. How do i do that in java.

Please advise. Any sample code, resources highly appreciated. thanks in advance.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If (a*b >=0) { // both positive or both negative or one or both are zero}

Else {// a and b have different signs}

boolean bothNegative = (a < 0 && b < 0) ;

boolean bothSameSign = (bothPositive || bothNegative) ;

Doug

public static int signum(int i)

Returns the signum function of the specified int value. (The return value is -1 if the specified value is negative; 0 if the specified value is zero; and 1 if the specified value is positive.)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial```
int pos =1;
int neg =- 1;
int sum;
String result;
sum = (Integer.signum(pos)+Integer.signum(neg));
System.out.println(result = sum>0?"Both Positive":sum<0?"Both Negative":"One is negative, one positive");
```

System.out.println("given number 'a' is a negative number");

}

I wrote as above which is not working. please advise

if(Integer.signum(a)==-1){

but yes, your line above should work like that.

...which would need to be something like ;

```
if(Integer.signum(a) == -1){System.out.println(" a is negative");}
else{System.out.println("a is positive");}
```

if(a < 0){

System.out.println("The number given " + a + " is a negative number") ;

}

(While Integer.signum(a) == -1 may also be a fine solution, I think it's a bit of overkill, no?)

Doug

http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html

what is the return type of the signum() . Please advise

what is the return type of the signum()

int.

that should be obvious from the signature of the method.

http://codingbat.com/prob/p159227

solution seems complicated. Trying to write more simplified way.

public boolean posNeg(int a, int b, boolean negative) {

if (negative) {

return (a < 0 && b < 0);

}

else {

return ((a < 0 && b > 0) || (a > 0 && b < 0));

}

}

I tried like below

```
public boolean posNeg(int a, int b, boolean negative) {
if(Integer.signum(a)!=Integer.signum(b)){
return true;
}
if(Integer.signum(a)==-1 & Integer.signum(b)==-1 & negative==true){
return true;
}
return false;
}
```

Failing test cases for

posNeg(-4, 5, true) → false true X

posNeg(1, -1, true) → false true X

posNeg(-1, 1, true) → false true X

please advise

signum

public static int signum(int i)

Returns the signum function of the specified int value. (The return value is -1 if the specified value is negative; 0 if the specified value is zero; and 1 if the specified value is positive.)

Returns:

the signum function of the specified int value.

Since:

1.5

Returns the signum function of the specified int value

No if DOESNT return the signum function of the specified int - it returns one of 3 values as an int, telling you whether your argument is negative, positive or neither.

if( (a^b)>=0 ){

System.out.println("a, b are both positive or both negative");

}

Once again:

public void checkNumbers (int a,int b){

if (a>=0 && b>= 0){ System.out.println("both numbers positive";}

else {System.out.println("one of numbers negative";}

}

public boolean posNeg(int a, int b, boolean negative) {

return negative?a<0 && b<0:(a^b)<0;

}

public boolean posNeg(int a, int b, boolean negative) {

boolean aNeg = (a < 0) ;

boolean bNeg = (b < 0) ;

boolean ok = (aNeg && !bNeg) || (!aNeg && bNeg) ;

if (negative)

ok = aNeg && bNeg ;

return ok ;

}

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

I wrote code as above. I wonder how this is different from the solution in given in the link

public boolean posNeg(int a, int b, boolean negative) {

if(negative==true){

return (a<0&& b<0);

}

if((a>0&&b<0)||(a<0&&b>0))

return true;

}

return false;

}

Please advise

The solution using ^ may behave differently from the solution given in the link when one of the values is 0 and the other is negative, but it is based on http:#a40340293 specifying that 0 is to be considered positive.

The link itself seems ambiguous on this, and the checks done from that link do not seem to consider such cases.

```
public boolean posNeg(int a, int b, boolean negative) {
if (negative) {
return (a < 0 && b < 0);
}
else {
return ((a < 0 && b > 0) || (a > 0 && b < 0));
}
}
```

when to use else and when to use fallthrough?

please advise

```
and in having an explicit return false
```

i thought explicit return is needed outside the if and else blocks. Is it is not needed?

When you need to have explicit return? please advise

```
public boolean posNeg(int a, int b, boolean negative) {
if (negative) {
return (a < 0 && b < 0);
}
```

. . . what are you trying to do here at all?

Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.