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

Java Programming: 2D array operations: Write a program that creates a two-dimens

ID: 3932903 • Letter: J

Question

Java Programming:

2D array operations:

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.

-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.

Demonstrate each of the methods in this program

Explanation / Answer

TwoDimentionalArrayOperation.java


public class TwoDimentionalArrayOperations {
/**
* This method should accept a two-dimensional array as its argument
* @param a
* @return the total of all the values in the array.
*/
   int getTotal(int a[][])
{
   int total=0;
   for(int i=0;i<a.length;i++)
   {
       for(int j=0;j<a[i].length;j++)
       {
           total=total+a[i][j];
       }
   }
       return total;
}
   /**
   * This method should accept a two-dimensional array as its argument
   * @param a
   * @return the average of all the values in the array.
   */
   double getAverage(int a[][])
   {
       int total=0,count=0;
   for(int i=0;i<a.length;i++)
   {
       for(int j=0;j<a[i].length;j++)
       {
           total=total+a[i][j];
           count++;
       }
   }
   if(count>0)
       return total/count;
   else
   return 0.0;
   }
   /**
   * 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.
   * @param a
   * @param i
   * @return the total of the values in the specified row.
   */
   int getRowTotal(int a[][],int i)
   {
       int total=0;
       for(int j=0;j<a[i].length;j++)
       {
           total=total+a[i][j];
       }
       return total;
   }
   /**
   * 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.
   * @param a
   * @param j
   * @return the total of the values in the specified column.
   */
   int getColumnTotal(int a[][],int j)
   {
       int total=0;
       for(int i=0;i<a.length;i++)
       {
           for(int k=0;k<a[i].length;k++)
           {
               if(k==j)
               {
                   total=total+a[i][j];
               }
           }
       }
       return total;
   }
   /**
   * 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.
   * @param a
   * @param i
   * @return the highest value in the specified row.
   */
   int getHighestInRow(int a[][],int i)
   {
       int max=0;
       for(int j=0;j<a[i].length;j++)
       {
           if(max<a[i][j])
           {
               max=a[i][j];
           }
       }
       return max;
   }
   /**
   * 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.
   * @param a
   * @param i
   * @return the lowest value in the specified row.
   */
   int getLowestInRow(int a[][],int i)
   {
       int min=160000;//some highest number
       for(int j=0;j<a[i].length;j++)
       {
           if(min>a[i][j])
           {
               min=a[i][j];
           }
       }
       return min;
   }
}

TwoDimentionalArray.java

import java.util.Scanner;

public class TwoDimentionalArray {

   public static void main(String[] args) {
       TwoDimentionalArrayOperations twoDimentionalArrayOperations=new TwoDimentionalArrayOperations();
       Scanner sc = new Scanner(System.in);
       int a[][]={{2,4,5},{8,10,6},{3,7,1}};
       int choice;
       do {
           System.out.println("Enter choose");
           System.out.println("1:get total");
           System.out.println("2:get average");
           System.out.println("3:get row total ");
           System.out.println("4:get colum total");
           System.out.println("5:get highest in row");
           System.out.println("6:get lowest in row");
           choice = sc.nextInt();
           int i,intvalue;
           double doublevalue;
           switch (choice) {
           case 1:
               intvalue=twoDimentionalArrayOperations.getTotal(a);
               System.out.println("total is:"+intvalue);
               break;
           case 2:
               doublevalue=twoDimentionalArrayOperations.getAverage(a);
               System.out.println("average is:"+doublevalue);
               break;
           case 3:
               System.out.println("enter row number");
               i=sc.nextInt();
               intvalue=twoDimentionalArrayOperations.getRowTotal(a, i-1);
               System.out.println("total is:"+intvalue);
               break;
           case 4:
               System.out.println("enter colum number");
               i=sc.nextInt();
               intvalue=twoDimentionalArrayOperations.getColumnTotal(a, i-1);
               System.out.println("total is:"+intvalue);
               break;
           case 5:
               System.out.println("enter row");
               i=sc.nextInt();
               intvalue=twoDimentionalArrayOperations.getHighestInRow(a, i-1);
               System.out.println("max value is:"+intvalue);
               break;
           case 6:
               System.out.println("enter row");
               i=sc.nextInt();
               intvalue=twoDimentionalArrayOperations.getLowestInRow(a, i-1);
               System.out.println("min value is:"+intvalue);
               break;  
           default:
               sc.close();
               choice = 0;
           }

       } while (choice != 0);


   }

}

Output:

Enter choose
1:get total
2:get average
3:get row total
4:get colum total
5:get highest in row
6:get lowest in row
6
enter row
3
min value is:1
Enter choose
1:get total
2:get average
3:get row total
4:get colum total
5:get highest in row
6:get lowest in row
1
total is:46
Enter choose
1:get total
2:get average
3:get row total
4:get colum total
5:get highest in row
6:get lowest in row
4
enter colum number
2
total is:21
Enter choose
1:get total
2:get average
3:get row total
4:get colum total
5:get highest in row
6:get lowest in row

7

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