[go: up one dir, main page]

0% found this document useful (0 votes)
4 views2 pages

Experiment-6: Aim:-Create Java Program With The Use of Java Packages

The document outlines a Java program that creates a package named 'Mathematics' containing a class 'Matrix' for performing addition and subtraction of 2x2 matrices. It includes methods for matrix operations and a main method demonstrating the functionality with example matrices. The program ensures that only 2x2 matrices are accepted and prints the results of the operations.

Uploaded by

shreycoder1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Experiment-6: Aim:-Create Java Program With The Use of Java Packages

The document outlines a Java program that creates a package named 'Mathematics' containing a class 'Matrix' for performing addition and subtraction of 2x2 matrices. It includes methods for matrix operations and a main method demonstrating the functionality with example matrices. The program ensures that only 2x2 matrices are accepted and prints the results of the operations.

Uploaded by

shreycoder1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment-6

Aim:- Create java program with the use of java packages


Create a package named “Mathematics” and add a class “Matrix” with methods to add and subtract
matrices (2x2). Write a Java program importing the Mathematics package and use the classes defined
in it.

Code:
package package1;

public class Matrix {


private int[][] matrix;

public Matrix(int[][] matrix) {


if(matrix.length!=2 || matrix[0].length!=2 || matrix[1].length!=2) {
throw new IllegalArgumentException("Only 2*2 matrices are allowed");
}
this.matrix=matrix;
}

public int[][] add(Matrix other){


int[][] result=new int[2][2];
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
result[i][j]=this.matrix[i][j]+other.matrix[i][j];
}
}
return result;
}

public int[][] subtract(Matrix other){


int[][] result=new int[2][2];
for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
result[i][j]=this.matrix[i][j]-other.matrix[i][j];
}
}
return result;
}

public void printMatrix() {


for(int i=0;i<2;i++) {
for(int j=0;j<2;j++) {
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}

public static void main(String[] args) {


int[][] matrix1= {{1,2},{3,4}};
int[][] matrix2= {{5,6},{7,8}};
Matrix m1=new Matrix(matrix1);
Matrix m2=new Matrix(matrix2);
System.out.println("Matrix 1:");
m1.printMatrix();
System.out.println("Matrix 2:");
m2.printMatrix();
int[][] resultAdd=m1.add(m2);
Matrix resultAddMatrix=new Matrix(resultAdd);
System.out.println("Result of Addition:");
resultAddMatrix.printMatrix();
int[][] resultSubtract=m1.subtract(m2);
Matrix resultSubtractMatrix=new Matrix(resultSubtract);
System.out.println("Result of Subtraction:");
resultSubtractMatrix.printMatrix();
}
}

Output:

You might also like