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: 3597449 • Letter: W

Question

Write a 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.(I NEED JAVA CODE)

Demonstrate each of the methods in this program.

OUTPUT SHOULD BE LIKE THIS:

input1.txt

input2.txt

input3.txt

input4.txt

Explanation / Answer

Here is the code with specified methods for you:

class TwoDArrayStatistics
{
    //• getTotal. This method should accept a two-dimensional array as its argument and
    //return the total of all the values in the array.
    public static double getTotal(double[][] array)
    {
       double sum = 0.0;
       for(int i = 0; i < array.length; i++)
           for(int j = 0; j < array[i].length; j++)
               sum += array[i][j];
       return sum;      
    }
   
    //• getAverage. This method should accept a two-dimensional array as its argument and
    //return the average of all the values in the array.
    public static double getAverage(double[][] array)
    {
       return getTotal(array) / (array.length * array[0].length);
    }
   
    //• 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.
    public static double getRowTotal(double[][] array, int rowIndex)
    {
       double sum = 0;
       for(int i = 0; i < array[rowIndex].length; i++)
           sum += array[rowIndex][i];
       return sum;  
    }
   
    //• 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.
    public static double getColumnTotal(double[][] array, int colIndex)
    {
       double sum = 0;
       for(int i = 0; i < array.length; i++)
           sum += array[i][colIndex];
       return sum;  
    }
   
    //• 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.
    public static double getHighestInRow(double[][] array, int rowIndex)
    {
       double highestInRow = array[rowIndex][0];
       for(int i = 1; i < array[rowIndex].length; i++)
           if(array[rowIndex][i] > highestInRow)
               highestInRow = array[rowIndex][i];
       return highestInRow;      
    }
   
    //• 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.
    public static double getLowestInRow(double[][] array, int rowIndex)
    {
       double lowestInRow = array[rowIndex][0];
       for(int i = 1; i < array[rowIndex].length; i++)
           if(array[rowIndex][i] < lowestInRow)
               lowestInRow = array[rowIndex][i];
       return lowestInRow;
    }
}

You just gave the input files. How to read these files, i.e., how many rows, how many columns etc information is not specified. Please give proper instructions if you want me to run some demo for these functions.

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