/**
* 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 & 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 & b
* at the identical row and column.
*/
public int[][] add(int[][] a, int[][] b) {
}
}
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](http://img.homeworklib.com/questions/7663c9a0-9cbc-11eb-a8ad-af359d57060b.png?x-oss-process=image/resize,w_560)
/** * Class that defines a method which performs matrix addition. This gives students their first...
/** * 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: 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...
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...
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...
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 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 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...
Write a program that thoroughly tests the class LinkedBag. Write the "LinkedBag" class, and a main program named "Project1.java" testing the methods defined for the LinkedBag object. The interface file: BagInterface.java. /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.0*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /**...
Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment...
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...