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

Need to write a program that contains the following: public class Lab8{ public s

ID: 3808989 • Letter: N

Question

Need to write a program that contains the following:

public class Lab8{

    public static void main(String[] args){

   

               //1 . Instantiate a 4x4 2D array.                     

                             

               // Test all methods by invoking them.

            // Output the matrix or return values to show that the

            // methods work properly.

               }

   

      /* 2. Populate the 2D array with random integers between 1 and 50 without duplicates

       */

      public static void populateMatrix(int[][] data){

      }

   

      /* 3. Output the 2D array to screen in following format

        matrix = 1   3   5 11

                        2    4   6 12

                        8    9   7 13

                       14 15 16 17

       */

       public static void outputMatrix(int[][] data){

       }

   

     /* 4. Returns the location of largest element of the 2D array.

        This location is stored in an array of two elements, where

        location[0] is the row, location[1] is the column.

      */

    

               public static int[] findLocationOfLargest(int[][] data){

              

               }

              

               /* 5. Returns the diagonal elements of a 2D array where

                  # of rows == # of cols (square matrix).

        */

               public static int[] getDiagonal(int[][] data){

              

               }

              

               /* 6. Returns the transpose of the data array.

                    Example

                     data = 1 3 5    data^T = 1 2 8

                            2 4 6             3 4 9

                            8 9 7             5 6 7

        */

               public static int[][] transpose(int[][] data){

              

               }

              

               /* 7. Returns the position(row and column) of the search key

               */

        public static int[] find(int[][] data, int key){

        }

   

               /*

                 8. Sort a specific row in descending order. Uses bubble sort.

               */

               public static int sortedRow(int[][] data, int row){

               }

                   

        /* 9. find sum of each row of the 2D array, returns an array of sums

         */

        public static int[] rowSum(int[][] data){

        }

   

        /* 10. Create a duplicate of the 2D array using arraycopy method.

           returns the duplicate of the original 2D array.

           See Systems.arraycopy.

         */

       public static int[][] duplicate(int[][] data){

       }

   

   

}//class

Explanation / Answer

package arr;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;

public class Lab8 {
   public static void main(String[] args) {

       int[][] array = new int[4][4];
       populateMatrix(array);
       // 1 . Instantiate a 4x4 2D array.

       // Test all methods by invoking them.
       // Output the matrix or return values to show that the
       // methods work properly.

   }

   /*
   * 2. Populate the 2D array with random integers between 1 and 50 without
   * duplicates
   */
   public static void populateMatrix(int[][] data) {

       System.out.println("Populating Array:");
       int size = 50;
       ArrayList<Integer> list = new ArrayList<Integer>(size);
       for (int i = 1; i <= size; i++) {
           list.add(i);
       }

       Random rand = new Random();

       for (int i = 0; i < 4; i++) {
           for (int j = 0; j < 4; j++) {
               int index = rand.nextInt(list.size());
               data[i][j] = list.remove(index);
           }
       }
       outputMatrix(data);
       findLocationOfLargest(data);
       /*
       * while(list.size() > 0) { int index = rand.nextInt(list.size());
       * System.out.println("Selected: "+); }
       */
   }

   /*
   * 3. Output the 2D array to screen in following format matrix = 1 3 5 11 2
   * 4 6 12 8 9 7 13 14 15 16 17
   */
   public static void outputMatrix(int[][] data) {

       System.out.println("Printing the Array:");
       for (int i = 0; i < 4; i++) {
           for (int j = 0; j < 4; j++) {
               System.out.print(data[i][j] + " ");
           }
           System.out.println();
       }
   }

   /*
   * 4. Returns the location of largest element of the 2D array. This location
   * is stored in an array of two elements, where location[0] is the row,
   * location[1] is the column.
   */

   public static int[] findLocationOfLargest(int[][] data) {

       int[] pos = new int[2];
       int max = Integer.MIN_VALUE;
       String position = "";
       for (int i = 0; i < 4; i++) {
           for (int j = 0; j < 4; j++) {
               if (max < data[i][j]) {
                   max = data[i][j];
                   position = i + " th row, " + j + " th column";
                   pos[0] = i;
                   pos[1] = j;
               }
           }
       }
       System.out.println("Location of the Largest number is: " + position);
       getDiagonal(data);
       return pos;
   }

   /*
   * 5. Returns the diagonal elements of a 2D array where # of rows == # of
   * cols (square matrix).
   */
   public static int[] getDiagonal(int[][] data) {

       int[] diagonal = new int[4];
       System.out.println("Diagonal elements are:");
       for (int i = 0; i < 4; i++) {
           for (int j = 0; j < 4; j++) {
               if (i == j) {
                   diagonal[i] = data[i][j];
                   System.out.println(data[i][j]);
               }
           }
       }
       transpose(data);
       return diagonal;
   }

   /*
   * 6. Returns the transpose of the data array. Example data = 1 3 5 data^T =
   * 1 2 8 2 4 6 3 4 9 8 9 7 5 6 7
   */
   public static int[][] transpose(int[][] data) {

       System.out.println("Transpose Matrix:");
       for (int i = 0; i < 4; i++) {
           for (int j = 0; j < 4; j++) {
               if (j < i) {
                   int temp = data[i][j];
                   data[i][j] = data[j][i];
                   data[j][i] = temp;
               }
           }
       }
       outputMatrix(data);
       find(data, 23);
       return data;
   }

   /*
   * 7. Returns the position(row and column) of the search key
   */
   public static int[] find(int[][] data, int key) {
       boolean found = false;
       String position = "";
       int[] pos = new int[2];
       for (int i = 0; i < 4; i++) {
           for (int j = 0; j < 4; j++) {
               if (data[i][j] == key) {
                   found = true;
                   position = i + " th row, " + j + " th column";
                   pos[0] = i;
                   pos[1] = j;
                   break;
               }
           }
       }
       if (found) {
           System.out.println(key + "found at: " + position);
       } else {
           System.out.println("Key not found!");
       }
       sortedRow(data, 1);
       return pos;
   }

   /*
   * 8. Sort a specific row in descending order. Uses bubble sort.
   */
   public static int sortedRow(int[][] data, int row) {

       System.out.println("Sorintg the " + row + " row:");

       for (int i = 0; i < 4; i++) {
           for (int j = 0; j < 4; j++) {
               if (data[row][i] > data[row][j]) {
                   int temp = data[row][i];
                   data[row][i] = data[row][j];
                   data[row][j] = temp;
               }
           }
       }
       System.out.println("Sorted row: " + row);
       outputMatrix(data);
       rowSum(data);
       return 1;
   }

   /*
   * 9. find sum of each row of the 2D array, returns an array of sums
   */
   public static int[] rowSum(int[][] data) {
       int[] sums = new int[4];

       for (int i = 0; i < 4; i++) {
           int sum = 0;
           for (int j = 0; j < 4; j++) {
               sum += data[i][j];
           }
           sums[i] = sum;
           System.out.println("Sum of "+i+" th row is: "+sum);
       }
       return sums;
   }

   /*
   * 10. Create a duplicate of the 2D array using arraycopy method. returns
   * the duplicate of the original 2D array. See Systems.arraycopy.
   */
   public static int[][] duplicate(int[][] data) {
       int[][] duplicate = null;
       System.arraycopy(data, 0, duplicate, 0, 4);
       return duplicate;

   }

}// class

Output:

Populating Array:
Printing the Array:
23   17   37   38  
12   26   33   28  
6   3   4   22  
1   25   40   5  
Location of the Largest number is: 3 th row, 2 th column
Diagonal elements are:
23
26
4
5
Transpose Matrix:
Printing the Array:
23   12   6   1  
17   26   3   25  
37   33   4   40  
38   28   22   5  
23found at: 0 th row, 0 th column
Sorintg the 1 row:
Sorted row: 1
Printing the Array:
23   12   6   1  
26   25   17   3  
37   33   4   40  
38   28   22   5  
Sum of 0 th row is: 42
Sum of 1 th row is: 71
Sum of 2 th row is: 114
Sum of 3 th row is: 93

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