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

Java Programming: Write a program in Java in parrallel code that runs multiple t

ID: 3575629 • Letter: J

Question

Java Programming: Write a program in Java in parrallel code that runs multiple threads.

In your main class, you need to write an interface for the user to select the size of his/her desired matrices and the operation to be executed on the two matrices randomly generated. Have a text prompt the user to input the size of one or two of the parameters, such as the columns, the rows, or both at the same time. Once that is done, prompt the user to select an operation to use. They can be numbered from 1 to 3 as such: Select the operation to executed: 1. Add 2. Subtract 3. Multiply

Explanation / Answer

import java.util.*;

class Matrix
{
private int row, column;  

private double[][] matrixElements;
  
public Matrix(int rows, int columns)
   {

       this.row = rows;
       this.column = columns;
      
matrixElements = new double[row][column];
      
populatematrix(-100, 100);
   }
    public Matrix(double [][] matrixArray)
   {

       this.row = matrixArray.length;
       this.column = (matrixArray[0]).length;
      
matrixElements = new double[row][column];
        for(int i=0; i<row; i++)
       {
           for(int j=0; j<column; j++)
           {
               matrixElements[i][j] = matrixArray[i][j];
           }
       }
   }
    private void populatematrix(int min, int max)
   {
       Random randnum = new Random();
       Random rand = new Random();

for (int i=0; i<row; i++)
       {
           for (int j=0; j<column; j++)
           {
matrixElements[i][j] = rand.nextInt((max - min) + 1) + min;
           }
       }
   }
  
public Matrix add(Matrix otherMatrix)
   {

double[][] resultMatrixArray = new double[this.row][this.column];
      
for (int i=0; i<row; i++)
       {
           for (int j=0; j<column; j++)
           {
               resultMatrixArray[i][j] = this.matrixElements[i][j] + otherMatrix.matrixElements[i][j];
           }
       }
        return new Matrix(resultMatrixArray);
   }
    public Matrix subtract(Matrix otherMatrix)
   {
double[][] resultMatrixArray = new double[row][column];
      
       //Subtracting matrix elements
       for (int i=0; i<row; i++)
       {
           for (int j=0; j<column; j++)
           {
               resultMatrixArray[i][j] = this.matrixElements[i][j] - otherMatrix.matrixElements[i][j];
           }
       }
      
return new Matrix(resultMatrixArray);
   }
  
public Matrix dotProduct (Matrix otherMatrix)
   {
  
       double[][] resultMatrixArray = new double[row][column];
      
       double sum=0;
      
if ( this.column != otherMatrix.row )
           System.out.println(" Matrices Multiplication is not possible... Invalid Dimensions... ");
       else
       {
for ( int c = 0 ; c < this.row ; c++ )
           {
               for ( int d = 0 ; d < otherMatrix.column ; d++ )
               {   
                   for ( int k = 0 ; k < otherMatrix.row ; k++ )
                   {
                       sum = sum + ((this.matrixElements[c][k]) * (otherMatrix.matrixElements[k][d]));
                   }
                   resultMatrixArray[c][d] = sum;
                   sum = 0;
               }  
           }
       }
      
return new Matrix(resultMatrixArray);
   }
  
   public String getPrintableMatrix()
   {

       String result = "";

for (double[] row : matrixElements)
       {
           for (double j : row)
           {
               result += " " + j + " ";
           }
          
           result += " ";
       }
      
       return result;
   }
}  

class MatrixOperations
{
public static void main(String args[])
   {
       int row1, col1, row2, col2;   
       Scanner sc = new Scanner(System.in);
      
System.out.print(" Input Matrix 1 dimensions (ROWS space COLUMNS): ");
       row1 = sc.nextInt();
       col1 = sc.nextInt();
      

System.out.print(" Input Matrix 2 dimensions (ROWS space COLUMNS): ");
       row2 = sc.nextInt();
       col2 = sc.nextInt();
      
       int operation;
System.out.print(" Select the operation to executed: 1. Add 2. Subtract 3. Multiply > ");
       operation = sc.nextInt();
       Matrix result;
Matrix m1 = new Matrix(row1, col1);
       Matrix m2 = new Matrix(row2, col2);              
      
switch(operation)
       {
           case 1:
result = m1.add(m2);
                      
                       System.out.println(" First Matrix: " + m1.getPrintableMatrix());
                       System.out.println(" Second Matrix: " + m2.getPrintableMatrix());
                       System.out.println(" Resultant Matrix: " + result.getPrintableMatrix());
      
                       break;
                      
           case 2:
result = m1.subtract(m2);
                      
                       System.out.println(" First Matrix: " + m1.getPrintableMatrix());
                       System.out.println(" Second Matrix: " + m2.getPrintableMatrix());
                       System.out.println(" Resultant Matrix: " + result.getPrintableMatrix());
                      
                       break;
                      
           case 3:
result = m1.dotProduct(m2);
                      
                       System.out.println(" First Matrix: " + m1.getPrintableMatrix());
                       System.out.println(" Second Matrix: " + m2.getPrintableMatrix());
                       System.out.println(" Resultant Matrix: " + result.getPrintableMatrix());
                      
                       break;
                      
           default:   System.out.println(" Invalid operation..... "); break;
       }
      
       System.out.print(" ");
   }
}

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