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

Can you help with this in JAVA? Write the following public static methods (put i

ID: 3664440 • Letter: C

Question

Can you help with this in JAVA?

Write the following public static methods (put in the same class):

1) a getDoubMatrixmethod (with 2 int parameters for the first and second dimensions), do the following:

·allocate memory for a 2-dim. array of doubles using the row size and column size parameters (assign to a local 2-dim. array) ·randomly assign numbers >= 0.0 an d <100.0 to each element (use nested for loops)

·return the 2-dim. array

2) an addMatricesmethod which adds two 2-dim. arrays of doubles:

·Check if the size of the first dimension and the size of the 2nd dimension of the 2-dim. arrays are the same -- if they are NOT the same, return a 0 X 0 2-dim. array, otherwise do the following;

·Allocate memory for a local 2-dim. array with the same sizes as one of the 2-dim. array parameters

·Add each corresponding element in the parameter 2-dim. arrays and store the result in the correspondingelement of the local 2-dim. array (use nested for loops)

·Return the local 2-dim. array

3) a printDoubMatrixmethod, that prints (to the screen) the String parameter on a separate line, then the 2-dim. array. Be sure to print each element of the parameter 2-dim. array formatted (so the numbers line up), and each element in the 2nd dimension are on the same line (usual way).

4) a transposeMatrixmethod which transposes a 2-dim. array of doubles:

· Allocate memory for a local 2-dim. array with the REVERSED sizes as the 2-dim. array parameter (for example, if the parameter had 3 rows and 4 columns, the local array will have 4 rows and 3 columns)

·Assign each column of the parameter to the corresponding row of the local array

·Return the local 2-dim. array

5) a multiplyMatricesmethod (I'm calling the parameters doubMat1and doubMat2),

·Check if the size of 2nd dimension of doubMat1equals the size of the 1st dimension of doubMat2

-- if NOT, return a 0 X 0 2-dim. array, otherwise do the following;

·Allocate memory for a local 2-dim. array with the size of the 1st dimension the same as the size of the 1st dimension of doubMat1 and the size of the 2nd dimension the same as the size of the 2nd dimension ofdoubMat2

·Multiply the 2-dim. array parameters and store the result in the local 2-dim. array.

Write a Java application program (could be in the same class as the above methods) in which you declare the following arrays in main—doubMatrix1, doubMatrix2 and doubMatrix3 (all 2-dim. array of doubles). In main, assign 2 random numbers (ints between 3 and 10) for the first dimension and the second dimension. Then main will call ALL of the methods you wrote above, but the getDoubMatrix method 2 times (once for doubMatrix1, then once for doubMatrix2). Use doubMatrix3for the adding and multiplying results. Each time a 2-dim. array changes, call the printDoubMatrix for it.

DO NOT USE ANY CLASS-SCOPE VARIABLES unless they're static AND final! ALL METHODS ARE static. ALWAYS check the array bounds correctly (both dimensions)!

HOW TO USE get pseudo-randomly generated integers between MIN and MAX (inclusive): (int)(Math.random() * (MAX-MIN+1) ) + MIN

HOW TO USE get pseudo-randomly generated doubles >=0 and

SUGGESTION: To test this, hard-code some test 2-dim. arrays, and pass themto the methods. Then try with the random arrays.

Explanation / Answer

package com.chegg.test;

import java.util.Random;

/**
* Class to perform matrix operations
*
* @author Penchal
*
*/
public class MatrixTest {
   public static void main(String[] args) {
       Random rand = new Random();
       int rowSize = 0;
       int columnSize = 0;
       double[][] doubMatrix3 = null;
       rowSize = rand.nextInt(10 - 0 + 1) + 0;
       columnSize = rand.nextInt(10 - 0 + 1) + 0;
       double[][] doubMatrix1 = getDoubMatrixmethod(rowSize, columnSize);
       rowSize = rand.nextInt(10 - 0 + 1) + 0;
       columnSize = rand.nextInt(10 - 0 + 1) + 0;
       // First matrix values
       System.out.println("First matrix");
       printDoubMatrixmethod(doubMatrix1);
       double[][] doubMatrix2 = getDoubMatrixmethod(rowSize, columnSize);
       // second matrix values
       System.out.println("Second matrix");
       printDoubMatrixmethod(doubMatrix2);
       // addition of matrix

       doubMatrix3 = addMatricesmethod(doubMatrix1, doubMatrix2);
       if (doubMatrix3.length != 0) {
           System.out.println("The result of addition of matrix:");
           printDoubMatrixmethod(doubMatrix3);
       }
       // multiplication of matrix
       doubMatrix3 = multiplyMatricesmethod(doubMatrix1, doubMatrix2);
       if (doubMatrix3.length != 0) {
           System.out.println("The result of multiplication of matrix:");
           printDoubMatrixmethod(doubMatrix3);
           // Transpose of a matrix
           transposeMatrixmethod(doubMatrix3);
       }

   }

   /**
   * Populating double dimension matrix
   *
   * @param rowSize
   * @param columnSize
   * @return doubleMatrix
   */
   public static double[][] getDoubMatrixmethod(int rowSize, int columnSize) {
       double[][] doubleMatrix = new double[rowSize][columnSize];
       Random rand = new Random();
       for (int c = 0; c < rowSize; c++) {
           for (int d = 0; d < columnSize; d++) {
               doubleMatrix[c][d] = rand.nextDouble() * 100.0;
           }
       }
       return doubleMatrix;
   }

   /**
   * method to add two matrices
   *
   * @param doubMatrix1
   * @param doubMatrix2
   * @return doubMatrix3
   */
   public static double[][] addMatricesmethod(double[][] doubMatrix1,
           double[][] doubMatrix2) {
       double[][] doubMatrix3 = null;
       try {
           int aRows = doubMatrix1.length;
           int aColumns = doubMatrix1[0].length;
           int bRows = doubMatrix2.length;
           int bColumns = doubMatrix2[0].length;

           if (aColumns != bRows) {
               System.out.println("Matrix addition not possible");
               doubMatrix3 = new double[0][0];

           } else {
               doubMatrix3 = new double[aRows][bColumns];
               for (int i = 0; i < doubMatrix1.length; i++) {

                   for (int j = 0; j < doubMatrix2[0].length; j++) {

                       doubMatrix3[i][j] = doubMatrix1[i][j]
                               + doubMatrix2[i][j];
                   }
               }

           }
       } catch (ArrayIndexOutOfBoundsException aioe) {
           doubMatrix3 = new double[0][0];
       }

       return doubMatrix3;
   }

   /**
   * Method to print result matrix
   *
   * @param doubMatrix3
   */
   public static void printDoubMatrixmethod(double[][] doubMatrix3) {
       for (int i = 0; i < doubMatrix3.length; i++) {

           for (int j = 0; j < doubMatrix3[0].length; j++) {
               System.out.print(" " + doubMatrix3[i][j]);
           }
           System.out.println();
       }
       System.out.println();

   }

   /**
   * Method to transpose the matrix
   *
   * @param doubMatrix32
   *
   * @return doubMatrix3
   */
   public static void transposeMatrixmethod(double[][] doubMatrix) {
       try {
           int m = doubMatrix.length;
           int n = doubMatrix[0].length;
           double[][] doubMatrix3 = new double[m][n];

           for (int x = 0; x < n; x++) {
               for (int y = 0; y < m; y++) {
                   doubMatrix3[x][y] = doubMatrix[y][x];
               }
           }

           System.out.println("The result of trnaspose of matrix:");
           printDoubMatrixmethod(doubMatrix3);
       } catch (ArrayIndexOutOfBoundsException aioe) {
           System.out.println("Transpose not possible");
       }
   }

   /**
   * method to multiplicate the matrices
   *
   * @param doubMatrix1
   * @param doubMatrix2
   * @return doubMatrix3
   */
   public static double[][] multiplyMatricesmethod(double[][] doubMatrix1,
           double[][] doubMatrix2) {
       double[][] doubMatrix3 = null;
       try {
           int aRows = doubMatrix1.length;
           int aColumns = doubMatrix1[0].length;
           int bRows = doubMatrix2.length;
           int bColumns = doubMatrix2[0].length;

           if (aColumns != bRows) {
               System.out.println("Matrix multiplication not possible:");
               doubMatrix3 = new double[0][0];
           } else {
               doubMatrix3 = new double[aRows][bColumns];
               for (int i = 0; i < aRows; i++) { // aRow
                   for (int j = 0; j < bColumns; j++) { // bColumn
                       for (int k = 0; k < aColumns; k++) { // aColumn
                           doubMatrix3[i][j] += doubMatrix1[i][k]
                                   * doubMatrix2[k][j];
                       }
                   }
               }
           }
       } catch (ArrayIndexOutOfBoundsException aioe) {
           doubMatrix3 = new double[0][0];
       }
       return doubMatrix3;
   }
}

Output:

First matrix
   96.03997198376793   57.530872051115544
   31.609003232010313   42.7287493676452

Second matrix
   80.68086359656228   70.54244051311396
   27.81615632612703   64.50094192792966

The result of addition of matrix:
   176.7208355803302   128.0733125642295
   59.42515955813734   107.22969129557487

The result of multiplication of matrix:
   9348.875609992285   10485.689447778235
   3738.7912502154404   4985.820811788459

The result of trnaspose of matrix:
   9348.875609992285   3738.7912502154404
   10485.689447778235   4985.820811788459

Note:

for testing purpose i have taken the size as 2*2

please commnent if you have any doubts

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