Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Chapter 7. PC #16. 2D Array Operations Write a JAVA program that creates a two-d

ID: 3741383 • Letter: C

Question

Chapter 7. PC #16. 2D Array Operations

Write a JAVA program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods:

• getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array.

• getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array.

• getRowTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the total of the values in the specified row.

• getColumnTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a column in the array. The method should return the total of the values in the specified column.

• getHighestInRow. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the

subscript of a row in the array. The method should return the highest value in the specified row of the array.

• getLowestInRow. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the lowest value in the specified row of the array.

Demonstrate each of the methods in this program.

View required output

Row 0 total: 1012.55
Row 1 total: 1802.0
Row 2 total: 814.33 Row 3 total: 1012.55
Row 4 total: 1802.0
Row 5 total: 814.33
Row 6 total: 1012.55
Row 7 total: 1802.0
Row 8 total: 814.33
Column 0 total: 4706.3099999999995
Column 1 total: 5404.26
Column 2 total: 776.07
Array total: 10886.64

Explanation / Answer

package org.students;

import java.util.Scanner;

public class TwoDarray {

public static void main(String[] args) {

double nos[][]=new double[3][3];

double rowTot=0,colTot=0,rowHighest,rowlowest;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

for(int i=0;i<nos.length;i++)

{

for(int j=0;j<nos[0].length;j++)

{

System.out.print("Enter Element ("+i+","+j+") :");

nos[i][j]=sc.nextInt();

}

}

  

double tot=getTotal(nos);

double average=getAverage(nos);

for(int i=0;i<nos.length;i++)

{

rowTot=getRowTotal(nos,i);

System.out.println("Row "+i+" total:"+rowTot);

}

for(int i=0;i<nos.length;i++)

{

colTot=getColumnTotal(nos,i);

System.out.println("Column "+i+" total:"+colTot);

}

for(int i=0;i<nos.length;i++)

{

rowHighest=getHighestInRow(nos,i);

System.out.println("Row#"+(i)+" Highest :"+rowHighest);

}

for(int i=0;i<nos.length;i++)

{

rowlowest=getLowestInRow(nos,i);

System.out.println("Row#"+(i)+" Lowest :"+rowlowest);

}

}

//This method will calculate the minimum value in the Row

private static double getLowestInRow(double[][] nos, int row) {

double min=nos[row][0];

for(int i=0;i<nos.length;i++)

{

if(min>nos[row][i])

min=nos[row][i];

}

return min;

}

//This method will calculate the maximum value in the Row

private static double getHighestInRow(double[][] nos, int row) {

double max;

max=nos[row][0];

for(int i=0;i<nos.length;i++)

{

if(max<nos[row][i])

max=nos[row][i];

}

return max;

}

//This method will calculate the total of a column in 2-D array

private static double getColumnTotal(double[][] nos, int col) {

double tot=0;

for(int i=0;i<nos.length;i++)

{

tot=+nos[i][col];

}

return tot;

}

//This method will calculate the total of a row in 2-D array

private static double getRowTotal(double[][] nos, int row) {

double tot=0;

for(int i=0;i<nos.length;i++)

{

tot=+nos[row][i];

}

return tot;

}

//This method will calculate the average of 2-D array

private static double getAverage(double[][] nos) {

return ((double)(getTotal(nos)))/(nos.length+nos[0].length);

}

//This method will calculate the total of 2-D array

private static double getTotal(double[][] nos) {

double tot=0;

for(int i=0;i<nos.length;i++)

{

for(int j=0;j<nos[0].length;j++)

{

tot+=nos[i][j];

}

}

return tot;

}

}

___________________

Output:

Enter Element (0,0) :45
Enter Element (0,1) :56
Enter Element (0,2) :67
Enter Element (1,0) :78
Enter Element (1,1) :89
Enter Element (1,2) :54
Enter Element (2,0) :43
Enter Element (2,1) :32
Enter Element (2,2) :90
Row 0 total:67.0
Row 1 total:54.0
Row 2 total:90.0
Column 0 total:43.0
Column 1 total:32.0
Column 2 total:90.0
Row#0 Highest :67.0
Row#1 Highest :89.0
Row#2 Highest :90.0
Row#0 Lowest :45.0
Row#1 Lowest :54.0
Row#2 Lowest :32.0

____________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote