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();
___ _______ ___ _________ ____.
__________ __________ ________ ________ ____ ________ _____ _______.
___ ____ _________ ________ _____ ____ ___ ___ _________ ________.
______ __________ ______ _______ ____ __________ ______ ________ ____ _________ ________ _____.
__________ _______ ________ _____ __________ _________ ________ ________ ___.
_________ _____ ______ ______ ___ __________ ______ _________ ______ __________ ___ _____.
____ _________ ______ ____ __________ ______ ______ ____ __________ _______ _________ ______.
___ ________ _______ _____ _______ _______.
__________ _______ _____ ______ ________ ____ ____ _________ ____ _____ ________.
________ ________ ________ ________ ______ ___ _______.
___ ____ ________ ____ _______ _____ _______ _______ ______ __________ ____.
__________ ______ _____ ______ _______ ____ _______ ______ ______ ______ ____ ____.
________ _________ __________ _____ ___.
______ ______ ___ ______ _____ _________ ______ ________ ______ _______ ____ _____.
____ _______ _________ ____ ________ ___ ______ ________ ________.
____ ________ __________ ____ _____ _______ _______ _________ ___ _____ ____ ___.
____ _________ _______ __________ ______ ____ ___ _____ ____ ____ _____.
_______ ____ _______ _______ _____ ____ ______ _____ _____ _________ ________.
____ __________ ________ ______ ______ ____ ______ ______ _________.
_________ _________ _____ _____ ________.
___ ___ ______ _____ ___ _______ __________ ______ ___.
__________ ________ ___ ________ ______ _______ ____ __________ _______ ________ ________.
____ _________ _______ ____ _________ ______ ______ ______ ______ ________ _____.
_____ ____ _____ _____ ________.
____ _________ _____ _____ ________ ________ _________ _________.
__________ _________ ________ ______ _______ _______ _______ _____.
_________ _________ __________ _________ ________ __________ ______ __________ ________ ____ ________ __________.
______ _______ ____ ____ _____ ________ ________ __________ ________.
_________ ________ ____ ______ _____ ________ ______ __________ _________ ______.
____ ____ _________ __________ _______ ________ ________ _____ ______.
___ __________ ________ __________ _________ ____ __________ ___ _________ ____.
__________ ______ ________ __________ _____ ___ _______ _______ _________ ____.
_____ _______ ________ ________ ____ __________ ________ __________ ___ _____ __________.
__________ __________ ________ ____ __________ ___ _________.
_________ _____ _______ __________ _______ __________ _________ __________.
_____ _________ __________ ____ __________ ________ _________ _______ _______ _____.
______ ______ _____ _______ _________.
___ ____ ____ _____ ____ _________ ___ _______ _______.
_______ ________ ___ ________ _______ _______ _______ _________.
________ ______ _________ ______ __________ ________ __________ ________ ____.
___ __________ ______ ________ ____ _______ ___ ______ ________ ______ ____.
_________ ___ _____ _________ _________ __________ ___ __________ _______ ___ ______ ________.
____ _________ __________ _____ ___.
______ ___ _____ __________ _______ ____ __________.
___ ________ _______ _____ __________.
_______ ____ _____ _____ ____.
________ _______ _________ __________ ______ _______ _______ _______.
_______ ___ _______ ____ ____.
___ ___ _________ ________ __________ __________ _____ _____.
_________ ________ ________ _________ ____ ___ __________ ________ ___ ___ __________.
___ ______ ___ _________ ______.
Get Full Answer on WhatsApp