1

The program should return how many positive and negative numbers there are in a sequence.

The main method will read a sequence of numbers from input and for each of them evaluate if it is positive or negative using this method below.

    public static int positiveOrNegativeNumber(int n) {
    int positiveNumber; //counter for positive numbers
    int negativeNumber; //counter for negative numbers

    positiveNumber=0;
    negativeNumber=0;

    if (n>0) {
        positiveNumber = positiveNumber + 1;
    } else if (n<0) {
        negativeNumber = negativeNumber + 1;
    }
    return positiveNumber & negativeNumber;
}

does the expression return positiveNumber & negativeNumber return them both?

edit: So, following Java Geo suggestion I got this:

    public static NumCounter positiveOrNegativeNumber(int n, NumCounter numCounter) {

if (n>0) {
    numCounter.setPositiveNumCounter(numCounter.getPositiveNumCounter()+1);
} else if (n<0) {
    numCounter.setNegativeNumCounter(numCounter.getNegativeNumCounter()+1);
}
return numCounter;

}

But I don't know what I should add in the main method to print it out

    public static void main(String[] args) {
    int n;

    while(!Lettore.in.eoln()) { //while the line has not ended
        n = Lettore.in.leggiInt(); //read n from input
        // what is missing here??
        }
    }

Don't worry about Lettore.in.eoln and Lettore.in.leggiInt, are just methods that make getting things from input easier without having to use the scanner.

  • For the second part of your question, **no**. `&` means "bitwise AND" (unless Java has completely different syntax from other languages in this respect), it doesn't mean "return both of these". – Arc676 May 22 '16 at 09:02
  • You can change the method's return type to `int[]` and return `return new int[]{positiveNumber, negativeNumber}` – Titus May 22 '16 at 09:03
  • Your method gets only **one** number so it can only return if it is positive or negative, but can't count **how many** positive and negatives. – TDG May 22 '16 at 09:07
  • If you want to track both the numbers, put them in a map or an custom object and return. – Sunil Dabburi May 22 '16 at 09:11

6 Answers6

2

I will suggest to create a class "NumCounter"

public class NumCounter {

    int positiveNumCounter;
    int negativeNumCounter;
    public int getPositiveNumCounter() {
        return positiveNumCounter;
    }
    public void setPositiveNumCounter(int positiveNumCounter) {
        this.positiveNumCounter = positiveNumCounter;
    }
    public int getNegativeNumCounter() {
        return negativeNumCounter;
    }
    public void setNegativeNumCounter(int negativeNumCounter) {
        this.negativeNumCounter = negativeNumCounter;
    }
}

Then modify your method like

public static NumCounter positiveOrNegativeNumber(int n, NumCounter numCounter) {

    if (n>0) {
        numCounter.setPositiveNumCounter(numCounter.getPositiveNumCounter()+1);
    } else if (n<0) {
        numCounter.setNegativeNumCounter(numCounter.getNegativeNumCounter()+1);
    }
    return numCounter;
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Java Geo
  • 149
  • 2
  • 15
  • I like this suggestion but how do I print it out? in the main method I can't just write **positiveOrNegativeNumber(n)** because I miss the numCounter part, what should I put there? –  May 22 '16 at 09:49
1

No, positiveNumber & negativeNumber returns the result of Bit-wise AND of the two integers, and given your logic, will always return 0 (0 & 1 == 0 and 0 & 0 == 0).

If you want to return multiple values, you can return an int[] or an instance of a class that contains multiple fields.

That said, your method doesn't count anything. It receives a single number and even if you returned the two counters, one of them will be 0 and the other 1 (unless the input is 0). If you want to count the number of positive and negative values in a sequence, you should pass an array of input numbers to your method.

If you want to pass a single number of a sequence to each call of your method, you can make your positiveNumber and negativeNumber counters static members of your class, and initialize them to 0 outside your method. In that case, your method won't have to return anything at all, since you'll be able to access the counters from outside your method.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

No, you can return two primitive type values as you have shown above.

But Java has an nice feature to return object. In above case you can try to return a Map or your own custom object which will contain {"positiveNumber", X} and {"negetiveNumber", Y}

       //Define in class as:
    static Map<String, Integer> map =new  HashMap<String, Integer>();

    public static Map<String, Integer> positiveOrNegativeNumber(int n) {


       if (n > 0) {
           Integer positiveNumber = map.get("positiveNumber");
             if(positiveNumber != null){
                  positiveNumber = positiveNumber + 1;
                  map.put("positiveNumber", positiveNumber);
             }
             else
                 map.put("positiveNumber", 1);
       } else if ( n < 0 ) {
           Integer negetiveNumber = map.get("negetiveNumber");
           if(negetiveNumber != null){
               negetiveNumber = negetiveNumber + 1;
               map.put("negetiveNumber", negetiveNumber);
           }
           else
             map.put("negetiveNumber", 1);
       }

       return map;
   }
sauumum
  • 1,638
  • 1
  • 19
  • 36
0

No. But you can return a array. These numbers may stored in array and can return the array. Therefore can return the int array.

0

try this

 public static ArrayList<Integer> positiveOrNegativeNumber(int n) {
    int positiveNumber; //counter for positive numbers
    int negativeNumber; //counter for negative numbers

    positiveNumber=0;
    negativeNumber=0;

    if (n>0) {
        positiveNumber = positiveNumber + 1;
    } else if (n<0) {
        negativeNumber = negativeNumber + 1;
    }
    ArrayList<Integer> array = new ArrayList();
    array.add(positiveNumber );
    array.add(negativeNumber );
    return array ;
}

Now array.get(0) is the number of positiveNumber, And array.get(1) is the number of negativeNumber.

Vishwesh Jainkuniya
  • 2,821
  • 3
  • 18
  • 35
0

you can try to pack and unpack those numbers.

For example: Constructing an int with those int variables: positive and negative masked:

int packed = positive << 16 | negative;

you can return that value in your positiveOrNegativeNumberfunction and when you need to read those values, unpack them with:

int unpackedNegative = packed & 0xffff;
int unpackedPositive = packed >> 16;
OscarBcn
  • 626
  • 4
  • 11