Solved

comparing sign of a number in  java

Posted on 2014-09-23
36
243 Views
Last Modified: 2014-09-26
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.
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 9
  • 8
  • +2
36 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40339408
Does 0 count as positive?
0
 
LVL 11

Expert Comment

by:Radek Baranowski
ID: 40339432
Int a,b;
If (a*b >=0) { // both positive or both negative or one or both are zero}
Else {// a and b have different signs}
0
 
LVL 84

Expert Comment

by:ozo
ID: 40339483
Are the integers small enough to be sure they won't overflow when multiplied?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 28

Expert Comment

by:dpearson
ID: 40339645
boolean bothPositive = (a > 0 && b > 0) ;              // Or >= 0 if you wish to include 0 as positive.
boolean bothNegative = (a < 0 && b < 0) ;
boolean bothSameSign = (bothPositive || bothNegative) ;

Doug
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 250 total points
ID: 40339683
Use the signum function of the Integer class -
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.)
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40339902
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");

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40340105
How do i say if given number (say a) signature is  negative then print like 'given number 'a' is a negative number'. Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40340109
if(Integer.signum(a)==-1){
  System.out.println("given number 'a' is a negative number");
  }

I wrote as above which is not working. please advise
0
 
LVL 11

Expert Comment

by:Radek Baranowski
ID: 40340114
System.out.println (a.toString () + "is a negative number");
0
 
LVL 7

Author Comment

by:gudii9
ID: 40340201
how to check below number a is negative or not in if condition using signum method?
if(Integer.signum(a)==-1){
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40340234
The return values from Integer.signum(int) is the check. Please read my post at ID: 40339683

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");}

Open in new window

0
 
LVL 28

Expert Comment

by:dpearson
ID: 40340276
Try:

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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40340290
I still haven't seen a clarification on whether 0 is to be considered positive.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40340293
Yes. 0 is positive for my case.(which what in general i believe? please correct me if i am wrong)
0
 
LVL 7

Author Comment

by:gudii9
ID: 40340297
I was checking api
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html

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

Expert Comment

by:ozo
ID: 40340299
the return type is int
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40340301
what is the return type of the signum()

int.

that should be obvious from the signature of the method.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40340302
i was checking
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;
}

Open in new window


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
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40340304
0 is NOT a negative value. Zero is one of three return values from Integer.signum() and should be treated as such.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40340306
what is the easy simple readable way to write solution for this challenge?
0
 
LVL 7

Author Comment

by:gudii9
ID: 40340307
I see it
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
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40340308
You should take advantage of Java's inbuilt static methods to ensure reliability and avoid coding (simpler) functions of your own which could inevitably lead to errors. Using Integer.signum() is a way of parsing the truth. And using the ternary operator as I illustrated, is clear and pretty bulletproof.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40340312
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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40340324
If 0 is to be considered positive

if( (a^b)>=0 ){
   System.out.println("a, b are both positive or both negative");
}
0
 
LVL 11

Expert Comment

by:Radek Baranowski
ID: 40340331
I'm lost here on what you are trying to achieve.

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";}
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 40340342
for http://codingbat.com/prob/p159227
public boolean posNeg(int a, int b, boolean negative) {
  return negative?a<0 && b<0:(a^b)<0;
}
0
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 125 total points
ID: 40340346
Or writing out the steps a bit more (you may find it easier to understand?):

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 ;
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 40340601
what is meaning of a^b

Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40340616
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;
}

Open in new window


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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40340779
There seem to be differences in whitespace formatting, in the order of the or clauses, in using fallthrough instead of else, and in having an explicit return false, but the behaviour seems to be identical.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 total points
ID: 40340785
^ is exclusive or.
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40346331
in using fallthrough instead of else

Can you please elaborate on above point.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40346339
I just saw the solution as below(earlier i have some typo)

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));
  }
}

Open in new window


when to use else and when to use fallthrough?
please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40346345
and in having an explicit return false

Open in new window


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
0
 
LVL 84

Expert Comment

by:ozo
ID: 40346346
since the if clause will always exit with a return, using else here is mainly a matter of style.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 40346421
I can't help you any further because your question is beyond comprehension.

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

Open in new window


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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question