subject
Computers and Technology, 08.04.2021 07:40 AADJ4

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?

ansver
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 07:30, cireland
Write a program that inserts the digits of an integer into an array in originalorderfollowed by reverse order. first, promptthe user to enter a positive integer(> 0). determine the number of digits of the integer. create a dynamically allocated integer arrayof a size twice the number of digits. now insert the digits in original order which will occupy half of the array. then, insert the digits in reverse order. finally, output thedigits in thearray. use at least two functions to organize your program.
Answers: 3
image
Computers and Technology, 23.06.2019 16:30, jessisjawsome
If i wanted to include a built-in calendar in a document, what option could i select? draw table insert table insert chart quick tables
Answers: 1
image
Computers and Technology, 23.06.2019 16:30, 19thomasar
How to do this programming flowchart?
Answers: 3
image
Computers and Technology, 24.06.2019 00:50, JakeCline
Which of the following is not a key player in the sale of travel products?
Answers: 2
You know the right answer?
In java I need help on this specific code for this lab. Problem 1:

Create a Class named...

Questions in other subjects:

Konu
English, 17.05.2021 18:50
Konu
Mathematics, 17.05.2021 18:50