Question

Problem: Matrix Implementation Create a class Matrix with the following method stubs for the following methods...

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]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 \n");
   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;
   }

  
}

Add a comment
Know the answer?
Add Answer to:
Problem: Matrix Implementation Create a class Matrix with the following method stubs for the following methods...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Design a class to perform various matrix operations. A matrix is a set of...

    In C++ Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row position and a column position. If A is a matrix of five rows and six columns, we say that the matrix A is of the size 5 X 6 and sometimes denote it as Asxc. Clearly, a convenient place to store a matrix is in a two-dimensional array. Two...

  • Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elemen...

    Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...

  • Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix...

    Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix algebra: A vector is a one-dimensional set of numbers, such as [42 9 20]. The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of [42 9...

  • In this assignment, you are asked to: 1. create a Matrix class that stores and operate...

    In this assignment, you are asked to: 1. create a Matrix class that stores and operate on a dynamic two-dimensional array. The class has the following structure: Private member variables: - int ** mat; - int rows; - int cols; Public member functions: +Default constructor: sets both rows and cols to 3 and creates 3x3 matrix dynamically +Parameterized constructor: sets the rows and cols to the values passed to the constructor and create a matrix with the given dimensions. +Destructor:...

  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • Create a class called Lab7b and in it implement all of the methods below. Also, write...

    Create a class called Lab7b and in it implement all of the methods below. Also, write a main method with test calls to all of these methods. Don’t forget to turn in your file to Canvas before the end of the lab today. int[][] random(int N, int start, int end) returns an N-by-N matrix of random integers ranging from start to end; int rowSum(int[][] a, int i) returns the sum of the elements in row i of the 2-D array...

  • 47. For a matrix A(:, m:n) refers to _ a. Refers to the elements in all...

    47. For a matrix A(:, m:n) refers to _ a. Refers to the elements in all the rows of column n of the matrix A. b. Refers to the elements in all the rows between columns m and n of the matrix A. c. Refers to the elements in all the columns between rows m and n of the matrix A. d. Refers to the elements in all the columns of row n of the matrix A. 48. What does...

  • NO VECTORS PLEASE Start a simple Matrix template class In this lab, you’ll be writing a...

    NO VECTORS PLEASE Start a simple Matrix template class In this lab, you’ll be writing a template class to represent a matrix. Because it’s a template, your matrix class will be able to work with different types of underlying data. Start by creating a new file matrix.hpp. Inside this file, start to write your matrix template class. Here’s a start for the class definition: template <class T> class Matrix { private: int _rows; int _cols; T** data; public: Matrix(int rows,...

  • Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_RO...

    Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...

  • /** * Class that defines a method which performs matrix addition. This gives students their first...

    /** * Class that defines a method which performs matrix addition. This gives students their first practice at creating and using * the code needed for higher dimensional arrays. * * @author Matthew Hertz */ public class MatrixAddition { /** * Given two equally-sized matrices, return a newly allocated matrix containing the result of adding the two.<br/> * Precondition: a &amp; b must have the same number of rows and each row will have the same number of columns. You...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT