Question

/** * multiply a scalar value (constant) into a matrix. * IMPORTANT: the matrix passed should...

/**

* multiply a scalar value (constant) into a matrix.

* IMPORTANT: the matrix passed should NOT be modified.

* @param m: matrix from which scalar is to be subtracted

* @param b: scalar to be subtracted

* @return resulting Matrix

*/

public static Matrix multiply(Matrix m, double b)

{

Matrix result = new Matrix();

return result; //to be completed

}

/**

* add to matrices.

* IMPORTANT: neither of the passed matrices should be modified

* @param m: matrix 1

* @param n: matrix 2

* @return if m and n have the same dimensions, return

* result of adding the two matrices, otherwise return null

*/

public static Matrix add(Matrix m, Matrix n)

{

Matrix result = new Matrix();

return result; //to be completed

}

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

import java.util.Scanner;
public class Matrix {

   private double[][] array;
  
   public Matrix(int rows, int columns) {
       // TODO Auto-generated constructor stub
       array = new double[rows][columns];
   }
   public Matrix() {
      
   }
  
   public String toString(){
       String res = "";
       if(array==null)
           return res;
       for(int i=0;i<array.length;++i){
           res = res + "\n";
           for(int j=0;j<array[i].length;++j){
               res = res + array[i][j] + "\t";
           }
       }
       return res;
   }
  


/**

* multiply a scalar value (constant) into a matrix.

* IMPORTANT: the matrix passed should NOT be modified.

* @param m: matrix from which scalar is to be subtracted

* @param b: scalar to be subtracted

* @return resulting Matrix

*/

public static Matrix multiply(Matrix m, double b)

{

   if(m.array ==null)
       return null;

  
   Matrix result = new Matrix(m.array.length, m.array[0].length);
   for(int i=0;i<m.array.length;++i){
       for(int j=0;j<m.array[i].length;++j){
           result.array[i][j] = m.array[i][j] *b;
       }
   }

return result; //to be completed

}

/**

* add to matrices.

* IMPORTANT: neither of the passed matrices should be modified

* @param m: matrix 1

* @param n: matrix 2

* @return if m and n have the same dimensions, return

* result of adding the two matrices, otherwise return null

*/

public static Matrix add(Matrix m, Matrix n)

{
   if(m.array ==null || n.array==null)
       return null;

   if((m.array.length != n.array.length)   || (m.array[0].length != n.array[0].length) )
       return null;
  
   Matrix result = new Matrix(m.array.length, m.array[0].length);
   for(int i=0;i<m.array.length;++i){
       for(int j=0;j<m.array[i].length;++j){
           result.array[i][j] = m.array[i][j] + n.array[i][j];
       }
   }

return result; //to be completed

}

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       Matrix m = new Matrix(2, 2);
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter values: ");
       for(int i=0;i<m.array.length;++i){
           for(int j=0;j<m.array[i].length;++j){
               m.array[i][j] = sc.nextInt();
           }
       }
       System.out.println("Matrix is:");
       System.out.println(m.toString());
       Matrix mat = add(m, m);
      
       System.out.println(" Add Matrix m + m is:");
       System.out.println(mat.toString());
      
       Matrix mat1 = multiply(m, 10);
      
       System.out.println("Multiply Matrix m by 10 is:");
       System.out.println(mat1.toString());  

   }

}



======================================
See Output
58 return result; //to be completed 59 60 61 62 63 640/** 65 66 add to matrices. 67 68 IMPORTANT: neither of the passed matri

Thanks, let me know if there is any concern.

Add a comment
Know the answer?
Add Answer to:
/** * multiply a scalar value (constant) into a matrix. * IMPORTANT: the matrix passed should...
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
  • I need help filling in the the code in the methods add, multiply, and evaluate package...

    I need help filling in the the code in the methods add, multiply, and evaluate package poly; import java.io.IOException; import java.util.Scanner; /** * This class implements evaluate, add and multiply for polynomials. * * * */ public class Polynomial {       /**    * Reads a polynomial from an input stream (file or keyboard). The storage format    * of the polynomial is:    * <pre>    * <coeff> <degree>    * <coeff> <degree>    * ...    *...

  • 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...

  • Hi, can someone please help me implement the transpose, multiply(Matrix b), multiply(Matrix m, int threads), and...

    Hi, can someone please help me implement the transpose, multiply(Matrix b), multiply(Matrix m, int threads), and equals(Object in) methods? (This is Java) I really need help so please don't just refund the question. public class Matrix {1 public int[] [] matrix; 1 int[] [] trans; 1 public int x, y; 1 private boolean transposed; 1 1 public Matrix(int x, int y){1 matrix = new int[x][y1;1 this.x = x; this.y = y; 1 } 1 9 /*1 * This method takes...

  • 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...

  • /** * 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...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

  • Please code in MatLab or Octave Output should match Sample Output in the second picture Thank...

    Please code in MatLab or Octave Output should match Sample Output in the second picture Thank You 4. In this problem we will investigate using loops to calculate the product of two matrices. Given an mxn matrix A and an n x p matrix B, the matrix product C = AB is the m xp matrix with Cij = R=1 Qikbky. Write a program which calls a function get matrix.dimensions which should • print a description of the purpose of...

  • /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /**...

    /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; }    /** * Create a String that...

  • Java/LinkedList Need help with a few of the TODO parts, more info below in comments in...

    Java/LinkedList Need help with a few of the TODO parts, more info below in comments in bold. Thanks, package lab4; import java.util.IdentityHashMap; public class IntNode implements Cloneable {    private int data;    private IntNode next;       public IntNode(int d, IntNode n) {        data = d;        next = n;    }       public IntNode getNext() {        return next;    }          /// Override methods from Object       @Override   ...

  • // I need help with the following questions. Please use java programming ECLIPSE language to solve...

    // I need help with the following questions. Please use java programming ECLIPSE language to solve the questions. YOU ONLY NEED TO DIRECTLY COPY IT IN YOUR ECLIPSE APPLICATION AND RUN IT. I NEED THOSE PART WHICH IS SAYS --> "TO BE COMPLETED" I NEED HELP WITH [GET*] AND [REPLACE ALL] AND [ADD INT DOUBLE] PLEASE. import java.util.ArrayList; public class CustomArrayList { //instance variables public int[] data; //data.length gives the capacity public int nItems; //nItems gives items currently in the...

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