-1

I had a question in my exam. At first it was pretty easy. Let me explain the question:

  • Make the User Specify how many integers wants to put (Array)

  • Numbers should be between -1500 to 1500

  • (Tricky): Create a method that calculates the percentage of the numbers put, what percentage were 0, positive, negative. (All this should be in ONE method, no return to string, string buffer, the return result should be a double value)

I think I did it right, but when I tested it in real time, the problem that I run is that it returns the values 0.0, 0.0, 0.0 ... Or it returns the correct percentage only if the values fall within the same condition (eg. all zero).

Here is my code: (I hope you can make sense out of it). I'm just curious, I don't know how to solve it. I have also tried it with a static method, I run into the same problem.

import java.util.Scanner;

public class nPNZ {
    public int [] number;

    public nPNZ (int [] n){
        number = n;
    }

    public double [] allCalcs(){
        int countPos = 0; int countNeg = 0; int countZero = 0;
        for (int i = 0; i < number.length; i++) {
            if (number[i] == 0) {
                countZero++;
            }else if (number[i] > 0){
                countPos++;
            }else{
                countNeg++;
            }
        }
        //0 = 0 ; 1 = positive ; 2 = negative
        double total [] = new double [3];
        total[0] = (countZero/number.length)*100;
        total[1] = (countPos/number.length)*100;
        total[2] = (countNeg/number.length)*100;
        return total;
    }

    public static void main (String args[]){
        //min 20 number, -1500 to 1500
        Scanner input = new Scanner (System.in);

        final int MIN_SIZE = 5;
        int size = 0;

        while(size < MIN_SIZE){
            System.out.println("Specify the size of the array: ");
            size = input.nextInt();
            while(size<MIN_SIZE){
                System.out.println("Size should be greater than: " + MIN_SIZE);
                size = input.nextInt();
            }
        }

        input.nextLine();

        int num [] = new int [size];

        for (int i = 0; i < num.length; i++) {
            System.out.println("Write number " + (i+1) + " : ");
            num[i] = input.nextInt();
            while(num[i] < -1500 || num[i] > 1500) {
                System.out.println("The number should within the range of -1500      and 1500");
                num[i] = input.nextInt();
            }
        }

        nPNZ n = new nPNZ (num);
        System.out.println("Percentage of zero numbers is: " + n.allCalcs()[0] );
        System.out.println("Percentage of positive numbers is: " + n.allCalcs()[1] );
        System.out.println("Percentage of negative numbers is: " + n.allCalcs()[2]);
    }
}
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
  • 1
    what problem you faced? – Wasi Ahmad Mar 04 '17 at 22:03
  • Say: I put 5 numbers, if all of them are say: 0, 0, 0, 0, 0 --- I get a correct question from the output: Perc of zero numbers is: 100.0 Perz of positive nubers is: 0.0 Perc of Negative numbers is: 0.0 However if I put say: 0, 0, 0, 3, -1 The output follows: Perc of zero numbers is: 0.0 Perz of positive nubers is: 0.0 Perc of Negative numbers is: 0.0 – Genc Gashi Mar 04 '17 at 22:05

1 Answers1

1

I can see one problem in your code.

double total [] = new double [3];
total[0] = (countZero/number.length)*100;
total[1] = (countPos/number.length)*100;
total[2] = (countNeg/number.length)*100;
return total;

Here, countZero, countPos and countNeg are all integers and number.length is also integer. So, when you divide an integer by another integer, you will get an integer.

Since, countZero, countPos and countNeg are all less than number.length, you will get zero value in the total array.

Example:

System.out.println(2/3); // prints 0
System.out.println(2.0/3); // prints 0.6666666666666666
System.out.println((double)2/3); // prints 0.6666666666666666

There are several alternatives to solve your problem. One of them is, simply multiply the integer by 1.0 while dividing it by another integer.

You can do something like this.

public double [] allCalcs(){
    // your code goes here
    double total [] = new double [3];
    total[0] = (countZero * 1.0 / number.length) * 100;
    total[1] = (countPos * 1.0 / number.length) * 100;
    total[2] = (countNeg * 1.0 / number.length) * 100;
    return total;
}

OR, you can declare the variables (countZero, countPos and countNeg) as double.

Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
  • Ahhh, thank you sir. I made: double countPos = 0; double countNeg = 0; double countZero = 0; Thank you very much. I understand now, it fixed the problem. – Genc Gashi Mar 04 '17 at 22:12