Matrix

public class Matrix { // class Matrix with public modifier
int N ; // variable type integer columns
int M ; // variable type integer rows
int[][] MyArray ; // define 2D-Array

Matrix(int N,int M){ //constructor with 2 overloaded parameters
	MyArray = new int[N][M]; // set row,column to N,M
	this.N = N;
	this.M = M;
}
public void init(){ // void = empty Method with no return parameter
	// initialization for matrix
	for(int i = 0;i<N;i++) { // iterate through columns  index starts with 0
		for(int j = 0;j<M;j++) { // iteration through rows, index starts with 0
			MyArray[i][j] = 0;   zeros otherwise you will get error
		}
	}
}
public void setRandom(){ // void = empty Method with no return parameter
	// initialization for matrix
	for(int i = 0;i<N;i++) { // iterate through columns , index starts with 0
		for(int j = 0;j<M;j++) { // iteration through rows, index starts with 0
			MyArray[i][j] = (int) (Math.random()*3);  // you have to initialize all rows,columns with zeros otherwise you will get error
		}
	}
}
public void add(Matrix other) {  // Add Matrix to other Matrix
	// simple addition of 2 Matrixes with the same index
	// avoid errors, check if the other Matrix has same rows,columns
	
	if(other.N == N && other.M == M) {
		
	
	
	for(int i = 0;i<N;i++) { // iterate through columns , index starts with 0
		for(int j = 0;j<M;j++) { // iteration through rows, index starts with 0
			MyArray[i][j] = MyArray[i][j] + other.MyArray[i][j];   
		}
	}
	}
}
public void subtraction(Matrix other) {  // subtract Matrix to other Matrix
	// simple subtraction of 2 Matrixes with the same index
	// avoid errors, check if the other Matrix has same rows,columns
	
	if(other.N == M && N == M) {
		
	
	
	for(int i = 0;i<N;i++) { // iterate through columns , index starts with 0
		for(int j = 0;j<M;j++) { // iteration through rows, index starts with 0
			MyArray[i][j] = MyArray[i][j] - other.MyArray[i][j];  // you have to initialize all rows,columns with zeros otherwise you will get an error
		}
	}
	}
}
public void  multiplication(Matrix other) { 
	
	int Array[][] = new int[N][M];
	for(int i = 0;i<N;i++) { // iterate through columns , index starts with 0
		for(int j = 0;j<M;j++) { // iteration through rows, index starts with 0
			
				
			
			for(int s = 0;s<M;s++) {
				Array[i][j] += MyArray[i][s]*other.MyArray[s][j];
			}
			
		}
	}
	output(Array);
	
}
public void output() {
	for(int i = 0;i<N;i++) {
		System.out.println();
		for(int j = 0;j<M;j++) {
			System.out.printf(" %d",MyArray[i][j]);
		}
	}
}
public void output(int Array[][]) { // overloaded parameter type array as output 
	for(int i = 0;i<N;i++) {
		System.out.println();
		for(int j = 0;j<M;j++) {
			System.out.printf(" %d",Array[i][j]);
		}
	}
}


}
public class Main { // class main with public modifier

	public static void main(String[] args) {
	
Matrix m1 = new Matrix(4,4);

m1.init();
m1.setRandom();
System.out.println("Matrix 1");
m1.output();
System.out.println();
System.out.println("    +");
Matrix m2 = new Matrix(4,4);
System.out.println("Matrix 2");
m2.setRandom();
m2.output();
System.out.println();
System.out.println("    Solution of mult:");
m1.multiplication(m2);

System.out.println();
System.out.println();


Matrix m3 = new Matrix(4,4);
Matrix m4 = new Matrix(4,4);

m3.init();
m3.setRandom();
System.out.println("Matrix3:");
m3.output();
System.out.println();
System.out.println("Matrix4:");
m4.init();
m4.setRandom();
m4.output();
System.out.println();
m3.add(m4);
System.out.println();
System.out.println("Addition:");
m3.output();
	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *