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

Problem: Matrix Implementation Create a class Matrix with the following method s

ID: 669621 • Letter: P

Question

Problem: Matrix Implementation

Create a class Matrix with the following method stubs for the following methods described:

1. read: reads in a matrix from the command line

2. display: prints the matrix to the command line

3. getRows: returns the number of rows

4. getCols: returns the number of columns

5. set: sets the double value at a particular column/row position

6. get: returns the value stored at a particular column/row position

7. Plus: returns a new Matrix object that is the result of adding this Matrix object with another Matrix object. Two matrices have to have the same dimensions to be added together. Otherwise the two matrices cannot be multiplied.

New Matrix [n,p] = This Matrix[n,m] + Other Matrix[n,p]

8. times: returns a new Matrix object that is the result of multiplying this Matrix object with another Matrix object. This Matrix has to have the same number of columns as the other Matrix object has number of rows. Otherwise the two matrices cannot be multiplied. The new Matrix object will have a number of rows equal to this Matrix and a number of columns equal to the other Matrix object. The values will be as follows:

Dimensions - This Matrix[n x m], Other Matrix[ m x p], New Matrix [n x p]

New Matrix [n,p] = This Matrix[n,1] *Other Matrix[1,p] +...+ This Matrix[n,m]*Other Matrix[m,p]

Explanation / Answer

package mani;
import java.util.Scanner;

public class Matrix
{
   double[][] a;
   int rows;
   int cols;
   public void reads(){
       Scanner input = new Scanner(System.in);
   System.out.println("Enter the number of rows and columns of matrix");
   rows = input.nextInt();
   cols= input.nextInt();
   a=new double[rows][cols];
   System.out.println("Enter the elements of 1st martix row wise ");
   for (int i = 0; i < rows; i++)
   {
   for (int j = 0; j < cols; j++)
   {
   a[i][j] = input.nextDouble();
   }
   }
   }
   public int getRows(){
       return rows;
   }
   public int getCols(){
       return cols;
   }
   public void set(int i,int j,double val){
       a[i][j]=val;
   }
   public double get(int i,int j){
       return a[i][j];
   }
   public void display(){
       for (int i = 0; i < rows; i++)
   {
   for (int j = 0; j < cols; j++)
   {
   System.out.print(a[i][j] + " ");
   }
   System.out.println();
   }
   }
   public Matrix plus(Matrix m1,Matrix m2) {
       Matrix m=new Matrix();
       if(m1.rows==m2.rows&&m1.cols==m2.cols)
       {   m.a=new double[m1.rows][m1.cols];
       for ( int c = 0 ; c < m1.rows ; c++ ){
   for (int d = 0 ; d <m1.cols ; d++ )
   {
   m.a[c][d] = m1.a[c][d] + m2.a[c][d];
   }
       }
       m.display();
       }else{
           System.out.println("Matrices cannot be added!!!!");
       }
   return m;
      
   }
   public Matrix times(Matrix m1,Matrix m2){
       Matrix m=new Matrix();
       if(m1.cols==m2.rows){
           m.a=new double[m1.rows][m2.cols];
           for (int i = 0; i < m1.rows; i++)
       {
       for (int j = 0; j < m1.cols; j++)
       {
       for (int k = 0; k < m2.cols; k++)
       {
       m.a[i][j] = m.a[i][j] + m1.a[i][k] * m2.a[k][j];
       }
       }
       }
       }else{
           System.out.println("Matrices cannot be Multiplied!!!!");
       }
       return m;
   }

  
}

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