Question

/** * 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 should
   * assume preconditions hold and can write your code knowing this is always be true.
   *
   * @param a The first matrix we will be adding
   * @param b The second matrix whose entries will be added
   * @return Newly allocated array whose entries are equal to the sum of the entries in a &amp; b
   * at the identical row and column.
   */
  public int[][] add(int[][] a, int[][] b) {

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

import java.util.Scanner;

class MatrixAddition

{

static int m, n;

public static void main(String args[])

{

  

Scanner in = new Scanner(System.in);

System.out.println("Enter the no of rows and columns of matrix");

m = in.nextInt();

n = in.nextInt();

int first[][] = new int[m][n];

int second[][] = new int[m][n];

int sum[][] = new int[m][n];

System.out.println("Enter the elements of matrix 1");

for (int c = 0 ; c < m ; c++ )

for (int d = 0 ; d < n ; d++ )

first[c][d] = in.nextInt();

System.out.println("Enter the elements of matrix 2 ");

for (int c = 0 ; c < m ; c++ )

for (int d = 0 ; d < n ; d++ )

second[c][d] = in.nextInt();

  

sum = add(first, second);

System.out.println("Sum of entered matrices:-");

  

for (int c = 0 ; c < m ; c++ )

{

for ( int d = 0 ; d < n ; d++ )

System.out.print(sum[c][d]+"\t");

System.out.println();

}

}

public static int[][] add(int[][]first , int [][]second){

int sum[][] = new int[m][n];

for (int c = 0 ; c < m ; c++ )

for (int d = 0 ; d < n ; d++ )

sum[c][d] = first[c][d] + second[c][d];

return sum;

}

}

// This will provide you the sum of matixes of the same size. First enter the index values and enter the matrix of your own values. you will get the sum of matrix as output, you need to change the + to - for subtraction of matrix

Please see below for the sample run in my local

terminated> MatrixAddition [Java Application] C:Program Files Enter the no of rows and columns of matrix 2 2 Enter the elemen

Add a comment
Know the answer?
Add Answer to:
/** * Class that defines a method which performs matrix addition. This gives students their first...
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
  • /** * 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 Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E>...

    Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E> { /** Reference to the first node in our linked list. This will be null if the list is empty. */ private Entry head; /** * Creates an empty list. */ public SinglyLinkedList() { reset(); } /** * This method, which is only used within the SinglyLinkedList class, returns the instance to its initial state. This * call will reset the head to be...

  • Language is C++ Basic principles and theory of structured programming in C++ by implementing the class:...

    Language is C++ Basic principles and theory of structured programming in C++ by implementing the class: Matrix Use these principles and theory to help build and manipulate instances of the class (objects), call member functions Matrix class implementation from the main function file and, in addition, the Matrix class must have its interface(Matrix.h) in a separate file from its implementation (Matrix.cpp). The code in the following files: matrix.h matrix.cpp matrixDriver.h Please use these file names exactly. Summary Create three files...

  • cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...

    cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...

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

  • Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the...

    Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the overlapping content of the bags. Intersections are explained in more detail in Chapter 1, #6. An intersecion might contain duplicates. The method should not alter either bag. The current bag and the bag sent in as a parameter should be the same when the method ends. The method header is: public BagInterface<T> intersection(ResizableArrayBag <T> anotherBag) Example: bag1 contains (1, 2, 2, 3) bag2 contains...

  • Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the...

    Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...

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

  • Modify the Auction class according to these exercises: 4.48: close method 4.49: getUnsold method 4.51: Rewrite getLot method 4.52: removeLot method For help watch Textbook Video Note 4.2 which sho...

    Modify the Auction class according to these exercises: 4.48: close method 4.49: getUnsold method 4.51: Rewrite getLot method 4.52: removeLot method For help watch Textbook Video Note 4.2 which shows you how to use and test the auction project, as well as solving the getUnsold exercise. Document with javadoc and use good style (Appendix J) as usual. Test your code thoroughly as you go. Create a jar file of your project. From BlueJ, choose Project->Create Jar File... Check the "Include...

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