Write java program to find sum of two matrices. Define appropriate class, constructor and methods in your program. Make necessary assumptions.
Here's a Java program that calculates the sum of two matrices using classes, constructors, and methods:
import java.util.Scanner;
class Matrix {
private int rows;
private int cols;
private int[][] data;
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
data = new int[rows][cols];
}
public void inputMatrix() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter matrix elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = scanner.nextInt();
}
}
_____ _____ _____ _____ ________ ________ _____ _______ _______.
__________ _____ _________ ______ _______ ________ __________ _____ _________ ______.
________ __________ _________ _______ ________ ___ ________ _______ ____ ________.
_____ _______ ____ ________ _________.
________ _______ _____ ____ __________ ______ ____ __________ _____ __________ _________ ________.
_________ _______ _______ _____ _______.
_________ ________ ___ ________ __________ ________ ______ _____ ____.
______ ______ _________ ___ ______.
___ _______ ___ __________ _____.
_____ _________ ___ ___ ______.
____ _____ _______ _______ ______ _____ ____.
_______ _________ __________ _____ ______ __________ _________ _____ _____.
________ __________ ___ ____ ___ _________ ____ ________ _________ _______ ________ _________.
_______ _______ _____ __________ _______ __________ ________ __________ _____ ___ __________.
___ __________ ____ _____ _____ _________ _________ ____.
_____ ____ _________ ______ ____ ____.
________ ________ _______ _______ ______ _____ ____ ______ ______.
__________ _______ ________ ______ _______ ________ _______ _______ ____ _____ _______ __________.
_________ _______ _____ ____ _________.
____ _______ ________ ___ ____ _________ ________ _____.
_________ ______ _________ ____ _______ _________ ______ _____ _______.
__________ __________ ____ __________ ___ _________ _____.
_____ ______ _________ __________ ___ ____ ______.
_____ _________ _____ __________ ____ ____.
___ _______ ___ ________ ___ ________ ___ ___ ____ ______ __________ ____.
____ ________ ___ _______ ___ ____ ___ _________ _______ __________.
________ _____ _____ _________ ______ __________ ___ _________ __________.
_____ ____ _______ _______ ________ __________ ______ ___ ________.
___ _______ _________ ______ ______ _____ _____ ____ ________ _________ ____.
_____ _____ _______ ______ ________ ___ ________.
_____ ________ ________ __________ ____ ________ ______ ______ _________ __________ ____ ___.
___ __________ ________ _________ __________ _____ _______ ________.
__________ ________ ____ __________ __________ ______ ______ ____.
_____ ____ ________ __________ __________ ___ _______ __________.
__________ ____ ____ ______ _____ __________.
______ __________ ______ _____ __________ ___ __________ ___.
______ _________ _______ ________ ______ ______ ___.
___ _____ ________ ___ _________ _______ __________ ________ _______ _____.
___ _______ _________ _____ ______ ______ ___ ___ ____ ______ _________ ___.
________ ______ __________ _________ ________ _______ ________ ____ _______ ____.
_________ _____ ____ ___ ________ ____.
________ ________ __________ ____ __________ _________ ________ ______ _________ __________ ____.
______ _______ __________ ________ ________ ________ ______ ___ ____ ________ ______ _________.
__________ _______ ______ __________ ______ _____ _______ ____.
______ __________ ______ ___ ____ _____ _________.
__________ _____ ______ __________ _______ _____ ____ ____ ____.
_______ _______ ____ _____ _________ __________ __________ _______ ________ ________ ___ ____.
___ ______ _________ _________ ________.
______ ____ _________ ______ _______.
_______ __________ _______ _____ ____ _____ ____ ________ ____ ____ _____ ____.
____.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required