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

Write a program that creates a two-dimensional array initialized with test data.

ID: 3630163 • Letter: W

Question

Write a program that creates a two-dimensional array initialized with test data. Use any primitive data type 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 values in the specified row in 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 values in the specified row in the array.


** please comment each method (mentioned above) so I know what I am looking at. I am pretty lost and need to learn this. Thank you
Document your code, with annotated code throughout. Think of what comments needed if you were to look at the program a year from now.
You can initialize the array's data in the declaration to save time. Create methods to operate on two types of arrays, which means you'll have twelve methods. Use a class that performs the array operations, and a class that creates an instance of this class, for example:

ArrayOperations2D.java
ArrayOperations2DDemo.java

Here's some example output:


Explanation / Answer

please rate - thanks

import java.util.*;
    public class matrix
   {
      public static void main(String[] args)
      {Scanner in = new Scanner(System.in);
        int n,i,j;
        System.out.print("Enter size of matrix: ");
        n=in.nextInt();
        while(n<2)
           {System.out.println("size must be at least 2");
            System.out.print("Enter size of matrix: ");
           n=in.nextInt();
            }       
        int [][] a = new int [n][n];
      Random r=new Random();             //fill the matrix with random numbers
        for(i=0;i<n;i++)
           for(j=0;j<n;j++)
               {
                a[i][j]=r.nextInt(500);
                }
        System.out.println("The Matrix:");     //print matrix
        for(i=0;i<n;i++)
           {for(j=0;j<n;j++)
               System.out.print(a[i][j]+" ");
            System.out.println(" ");
            }
        System.out.println("The total of all elements is "+getTotal(a));
        System.out.println("The average of all elements is "+getAverage(a));
        System.out.println("The total of all elements in row 3 is "+getRowTotal(a,3));
        System.out.println("The total of all elements in column 3 is "+getColumnTotal(a,3));
        System.out.println("The highest element in row 3 is "+getHighestInRow(a,3));
        System.out.println("The lowest element in column 3 is "+getLowestInRow(a,3));
    }
public static int getTotal(int a[][])
{int i,j,tot=0;
for(i=0;i<a.length;i++)                                           //for each row
    for(j=0;j<a[0].length;j++)                                //for each column
          tot+=a[i][j];                                             //add the elements
return tot;
}
public static double getAverage(int a[][])
{return (double)getTotal(a)/(a.length*a[0].length);              //get the total and divide by number of elements
}
public static int getRowTotal(int a[][],int n)                    //add all elements in row n
{int j,tot=0;
    for(j=0;j<a[0].length;j++)
          tot+=a[n][j];
return tot;
}
public static int getHighestInRow(int a[][],int n)              //look for highest in row n
{int j,max=a[n][0];
    for(j=0;j<a[0].length;j++)
          if(a[n][j]>max)
                 max=a[n][j];
return max;
}
public static int getLowestInRow(int a[][],int n)                 //look for lowest in row n
{int j,low=a[n][0];
    for(j=0;j<a[0].length;j++)
          if(a[n][j]<low)
                 low=a[n][j];
return low;
}

public static int getColumnTotal(int a[][],int n)                 //add all elements in column n
{int j,tot=0;
    for(j=0;j<a.length;j++)
          tot+=a[j][n];
return tot;
}

}

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