0

This is probably a really bad way of writing code but heres a program that calculates the total of each row and print it all in brackets.

public static int[] rowsSums(int[][] array) {
    int[][] numArray = {

                            {3, -1,  4,  0},
                            {5,  9, -2,  6},
                            {5,  3,  7, -8}

                       };
    int rowTotal = 0;
    int row2Total = 0;
    int row3Total = 0;
    for (int i = 0; i < numArray[0].length; i++) {
        rowTotal += numArray[0][i];
        row2Total += numArray[1][i]; 
        row3Total += numArray[2][i];
    }

    System.out.println("(" + rowTotal + "," + row2Total + "," + row3Total + ")");
    return null;

}

The output without JUnit is:

(6,18,7)

I am in the process of testing this with JUnit and my code for this:

@Test
public void rowsSums() {

    int [] i = new int [] {6, 18, 7};
    assertEquals(i, Exercise2.rowsSums(numArray));
}

Yes, I know my output is not supposed to be null because JUnit hates it. What other variable can I return without making JUnit fail or spit an error?

I have to keep these as it is

public static int[] rowsSums(int[][] array) {

int[][] numArray = {

UPDATE: No matter what I try, JUnit always comes up with this error PrntScrn of Error

LtMuffin
  • 219
  • 2
  • 14
  • why not return the string as on output? – shahaf Apr 12 '18 at 14:35
  • 1
    If your method is supposed to return an `int[]`, I'd expect an array containing the sums. – daniu Apr 12 '18 at 14:36
  • 1
    "*Yes, I know my output is not supposed to be null because JUnit hates it.*" - Could you elaborate? "*What other variable can I return without making JUnit fail or spit an error?*" - Everything that fits your return type. – Turing85 Apr 12 '18 at 14:36
  • You shouldn't be printing in the function. Return the row totals as an integer array, like you already have, then print outside the method. – Carcigenicate Apr 12 '18 at 14:36

6 Answers6

1

If your method is designed to just print a message and not return anything, you should declare its return type to be void.

public static void rowsSums(int[][] array) { ...

Then just remove the return statement.

However, based on your test case, it looks like you want to return an array containing the values that you calculated. Instead of having three internal variables to hold totals (rowTotal, row2Total, and row3Total), those variables could be combined into one array where the totals are stored, then returned.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • It seems on my paper. I have to keep it as `public static int[] rowsSums(int[][] array) {` – LtMuffin Apr 12 '18 at 14:39
  • 1
    @Leon185 In that case, you want to go with the second option and keep your totals in an array, then return that array from your method. (Printing your results can then also be done outside the method.) – Bill the Lizard Apr 12 '18 at 14:41
  • How would I do this as an example? – LtMuffin Apr 12 '18 at 14:58
1

To return the int[] containing the sums, you'd do

return new int[] { rowTotal, row2Total, row3Total };

That's something you can assert as well then

assertArrayEquals(i, Exercise2.rowsSums(numArray));

Note that it is good practice to separate calculation and output, ie you should move the System.out.println to another function accepting the returned array as a parameter.

daniu
  • 14,137
  • 4
  • 32
  • 53
  • I used this, then I changed something in JUnit code. `@Test public void rowsSums() { int [] i = new int [] {6, 18, 7}; Assert.assertArrayEquals(i, Exercise2.rowsSums(numArray)); } ` – LtMuffin Apr 12 '18 at 15:08
0

I think that your method should return the computation performed and not only print a message otherwise you would not have a clear way to test your method.
Besides method names should be verbs/actions and not common names.
So sum() would be clearer.

You could return the sum array of each row in your method and assert the result in your unit test :

public static int[] sum(int[][] array) {
  ...     
    return new int[] {rowTotal,  row2Total, row3Total};
}

And in your test :

int[] sumExpected = ...
Assert.assertArrayEquals(sumExpected, Exercise2.sum(numArray));
davidxxx
  • 125,838
  • 23
  • 214
  • 215
0
int[][] numArray = {

            {3, -1,  4,  0},
            {5,  9, -2,  6},
            {5,  3,  7, -8}

};

public static int[] rowsSums(int[][] array) {

    int[] sum =  new int[array.length];

    for (int i=0;i<sum.length; i++)
        sum[i] = 0;

    for (int i = 0; i < sum.length; i++) {
        for(int j=0; j< array[i].length; j++)
        sum[i] += array[i][j];
    }

    return sum;

}

int[] result = rowsSums(numArray);
shahaf
  • 4,750
  • 2
  • 29
  • 32
0

If you will return array as a result you will be able to use it in your jUnit test and compare expected result with actual.

System.out.println("(" + rowTotal + "," + row2Total + "," + row3Total + ")");
int [] result = new int[3];
result[0] = rowTotal;
result[1] = row2Total;
result[2] = row3Total;
return result;
Luk
  • 2,186
  • 2
  • 11
  • 32
0

Why do you pass int[][] array to the method, but literally never do anything with it at all?... I'm going to assume you want to use that array structure you passed in order to count each of it's arrays (rows). You also should never mix output with functions. You should return the String, then print it in your main method. As @bill-the-Lizard said, you want to have an array in which holds the row's values and then you would just return an int[] array which contains the rows' counts. You would then have a for loop in your current one which would input the counts into their corresponding array key.