Computers and Technology
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?

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 11:00, swelch2010
Describe three characteristics of at-risk drivers. a. b. c. describe three characteristics of safe drivers. a. b. c. describe three driver errors that could cause a collision. a. b. c. how will this information affect you as a driver now and in the future? (2-3 sentences)
Answers: 2
image
Computers and Technology, 24.06.2019 03:00, SiegeHatake4534
Will do anything for brainlest so can you guys me out i will try my best to you out
Answers: 1
image
Computers and Technology, 24.06.2019 13:30, nina288
What process should be followed while giving a reference? sam has given a reference of his previous manager in his resume. sam should him in advance that the potential employers will him.
Answers: 1
image
Computers and Technology, 24.06.2019 17:30, NEONREDBLADE
Match the following. 1. formatting that is applied when cell data meets certain criteria 2. borders with formats not found on the border tool 3. a shortcut bar that contains tools for commonly used formats 4. formats that can be applied to numbers conditional formatting custom borders format bar number formats
Answers: 3
Do you know the correct answer?
In java I need help on this specific code for this lab. Problem 1:

Create a Class named...

Questions in other subjects:

Konu
Mathematics, 23.01.2020 01:31
Konu
Business, 23.01.2020 01:31