In java I need help on this specific code for this lab. Problem 1:

Create a Class named Array2D with two instance methods:

public double[] rowAvg(int[][] array)

This method will receive a 2D array of integers, and will return a 1D array of doubles containing the average per row of the 2D array argument.

The method will adjust automatically to different sizes of rectangular 2D arrays.

Example: Using the following array for testing:

int [][] testArray =

{

{ 1, 2, 3, 4, 6},

{ 6, 7, 8, 9, 11},

{11, 12, 13, 14, 16}

};

must yield the following results:

Averages per row 1 : 3.20

Averages per row 2 : 8.20

Averages per row 3 : 13.20

While using this other array:

double[][] testArray =

{

{1, 2},

{4, 5},

{7, 8},

{3, 4}

};

must yield the following results:

Averages per row 1 : 1.50

Averages per row 2 : 4.50

Averages per row 3 : 7.50

Averages per row 4 : 3.50

public double[] colAvg(int[][] array)

This method will receive a 2D array of integers, and will return a 1D array of doubles containing the average per column of the 2D array argument.

The method will adjust automatically to different sizes of rectangular 2D arrays.

Example: Using the following array for testing:

int [][] testArray =

{

{ 1, 2, 3, 4, 6},

{ 6, 7, 8, 9, 11},

{11, 12, 13, 14, 16}

};

must yield the following results:

Averages per column 1: 6.00

Averages per column 2: 7.00

Averages per column 3: 8.00

Averages per column 4: 9.00

Averages per column 5: 11.00

While using this other array:

double[][] testArray =

{

{1, 2},

{4, 5},

{7, 8},

{3, 4}

};

must yield the following results:

Averages per column 1: 3.75

Averages per column 2: 4.75

My code is:

public class ArrayDemo2dd

{

public static void main(String[] args)

{

int [][] testArray1 =

{

{1, 2, 3, 4, 6},

{6, 7, 8, 9, 11},

{11, 12, 13, 14, 16}

};

int[][] testArray2 =

{

{1, 2 },

{4, 5},

{7, 8},

{3,4}

};

// The outer loop drives through the array row by row

// testArray1.length has the number of rows or the array

for (int row =0; row < testArray1.length; row++)

{

double sum =0;

// The inner loop uses the same row, then traverses all the columns of that row.

// testArray1[row].length has the number of columns of each row.

for(int col =0 ; col < testArray1[row].length; col++)

{

// An accumulator adds all the elements of each row

sum = sum + testArray1[row][col];

}

//The average per row is calculated dividing the total by the number of columns

System.out.println(sum/testArray1[row].length);

}

} // end of main()

}// end of class

However, it says there's an error... I'm not sure how to exactly do this type of code... So from my understanding do we convert it?

Solved
Show answers

Ask an AI advisor a question