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

You are to write a java program named matrix.java that can multiply and add 2 ma

ID: 2246571 • Letter: Y

Question

You are to write a java program named matrix.java that can multiply and add 2 matrices.

Requriements:

1. Your program should prompt the user for the dimensions of the two matrices, then check for compatibility (remember 2 matrices can be multiplied only if the column of the first is equal to the row of the second). You could prompt for ONE dimension and build two square matrices. Minimum dimension of matrix must be 25.

2. If the above is not met, prompt the user for new and compatible dimensions.

3. Now generate random integer numbers (ranging from 1 to 30) to fill both matrices.

4. Display these two matrices on the screen.

5. Multiply the two matrices and display the result on the screen.

6. Insert a clock to see how long it would take to multiply these two matrices and display the time (with a message to this effect).

7. Now add the two matrices and display the result on the screen.

8. Insert a clock to see how long it would take to add these two matrices and display the time (with a message to this effect).

9. Prompt the user asking if they want to repeat the program.

Explanation / Answer

import java.io.*;
import java.util.*;

public class matrix{

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        Random rand = new Random();
        String line;
        do {   
             System.out.println("Enter one dimension for two square matrices (Minimum value is 25):");
             int n = sc.nextInt();
             long[][] a = new long[n][n];
             long[][] b = new long[n][n];
             long[][] c = new long[n][n];
       
             for (int i = 0; i<n; i++)
                 for(int j=0; j<n; j++){
                    a[i][j] = rand.nextInt(30) + 1;
                    b[i][j] = rand.nextInt(30) + 1;
                 }

             System.out.println("------------Matrix A------------ ");
             for (int i = 0; i<n; i++){
                 for(int j=0; j<n; j++){
                    System.out.print(a[i][j] + " ");
                 }
                 System.out.println();
             }

             System.out.println(" ------------Matrix B------------ ");
             for (int i = 0; i<n; i++){
                 for(int j=0; j<n; j++){
                    System.out.print(b[i][j] + " ");
                 }
                 System.out.println();
             }
             long startTime = System.nanoTime();
             System.out.println(" ------------Product of A & B------------ ");
             for (int i = 0; i<n; i++){
                 for(int j=0; j<n; j++){
                    c[i][j] = 0;
                   for (int k = 0; k <n; k++){
                      c[i][j] = c[i][j] + a[i][k] * b[k][i];
                   }
                 }
             }
             long endTime = System.nanoTime();
             for (int i = 0; i<n; i++){
                for(int j=0; j<n; j++){
                   System.out.print(c[i][j] + " ");
                }
                System.out.println();
             }
             long duration = endTime - startTime;
             System.out.println("It took " + duration + " nanosecods for multiplication");
                   
             startTime = System.nanoTime();
             System.out.println(" ------------Sum of A & B------------ ");
             for (int i = 0; i<n; i++){
                for(int j=0; j<n; j++){
                   c[i][j] = a[i][j] + b[i][j];
             
                }
             }
             endTime = System.nanoTime();
             for (int i = 0; i<n; i++){
                for(int j=0; j<n; j++){
                   System.out.print(c[i][j] + " ");
                }
                System.out.println();
             }
             duration = endTime - startTime;
             System.out.println("It took " + duration + " nanosecods for addition");
            
             System.out.println("Please enter 'y' to repeat and 'n' to stop:");
             line = sc.nextLine();
             line = sc.nextLine();
       } while (line.charAt(0) != 'n');
    }
}

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