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

Write a complete Java program containing the following methods: a) A method that

ID: 3632190 • Letter: W

Question

Write a complete Java program containing the following methods:
a) A method that generates an n X n matrix with values in rage of low-high (inclusive)
using the following header:
public static [][] generateMatrix(int n,int low,int high)
b) A method that prints an n X n matrix using the following header:
Public static void printMatrix(int[][] m)
c) A method that sums all the integers in the major diagonal in an n X n matrix of integers using the following header:
public static int sumMajorDiagonal(int[] [] m)
d) Write a main method to do the following:
1) Generate a 4x4 matrix with values in rage of 1-20
2) Print the matrix
3) Calculate and display the sum of all its elements on the major diagonal.
Here is a sample run:
The matrix is:
10 15 14 9
11 8 12 10
12 20 6 17
13 2 14 8
Sum of the elements in the major diagonal is 32

4) Change the main method to generate a 6x6 matrix with values in rage of 10-50
5) Print the matrix
6) Calculate and display the sum of all its elelments on the major diagonal

Explanation / Answer

public class AutoMatrix
{   
    public static int[][] generateMatrix(int n,int low,int high)
    {
        int[][] mat = new int[n][n];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                mat[i][j] = low + (int)(Math.random() * ((high - low) + 1));
            }
        }
        return mat;
    }
    public static void printMatrix(int[][] m)
    {
        int n = m.length;
        System.out.println("The matrix is: ");
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                System.out.print(m[i][j]+" ");
            }
            System.out.println("");
        }
    }
  
    public static int sumMajorDiagonal(int[] [] m)
    {
        int sum=0;
        int n=m.length;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(i==j)
                    sum = sum + m[i][j];
            }
        }
        return sum;
    }  
  
    public static void main(String[] args)
    {
        /*int[][] matrix = generateMatrix(4,1,20);
        printMatrix(matrix);
        int diagSum = sumMajorDiagonal(matrix);
        System.out.println("Sum of the elements in the major diagonal is " + diagSum);
        */
      
        int[][] matrix = generateMatrix(6,10,50);
        printMatrix(matrix);
        int diagSum = sumMajorDiagonal(matrix);
        System.out.println("Sum of the elements in the major diagonal is " + diagSum);
    }
}

Note: Uncomment the first 4 lines of code in main method and comment next 4 lines to execute case 3.

execute as it is to check case 4.

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