Write a java 3x3 matrix program that does the following:
1) multiply
2)add
3)subtract
4)scalar product
5)does calculation for a^3 - b^3
6)does calculation for 2a^ + 3b^2
an example for the adding method will look something like this
public static double[][] multiplyMatrix(double [][] a, double [][] b)
Code -
import java.util.Scanner;
public class Main
{ public static int row = 3;
public static int cols = 3;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int[][] matrix1 = {{1,2,3},{4,5,6},{7,8,9}};
int[][] matrix2 = {{4,24,8},{3,4,8},{5,3,3}};
System.out.println("First Matrix = ");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
System.out.print(matrix1[i][j]+"\t");
}
System.out.println();
}
System.out.println("Second Matrix = ");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
System.out.print(matrix2[i][j]+"\t");
}
System.out.println();
}
int [][]sum = addMatrix(matrix1,matrix2);
System.out.println("Addition ");
displayOutput(sum);
int [][]sub = subMatrix(matrix1,matrix2);
System.out.println("Subtraction ");
displayOutput(sub);
int [][]mult =multiplyMatrix(matrix1,matrix2);
System.out.println("Multiplication ");
displayOutput(mult);
scalarProductMat(matrix1,5);
int [][]aSquare = multiplyMatrix(matrix1,matrix2);
int [][]aCube = multiplyMatrix(aSquare,matrix1);
int [][]bSquare = multiplyMatrix(matrix2,matrix2);
int [][]bCube = multiplyMatrix(bSquare,matrix2);
int [][]aCubeMinuBcube = subMatrix(aCube,bCube);
System.out.println("a^3 - b^3 = ");
displayOutput(aCubeMinuBcube);
}
public static void displayOutput(int a[][] ){
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
public static int [][] addMatrix(int [][] matrix1, int [][]
matrix2){
int[][] sum = new int[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
sum[i][j] = matrix1[i][j] + matrix2[i][j];
// System.out.print(sum[i][j]+"\t");
}
//System.out.println();
}
return sum;
}
public static int [][] subMatrix(int [][] matrix1, int [][]
matrix2){
int[][] sum = new int[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
sum[i][j] = matrix1[i][j] - matrix2[i][j];
// System.out.print(sum[i][j]+"\t");
}
//System.out.println();
}
return sum;
}
public static int [][] multiplyMatrix(int [][] a, int [][]
b){
int multi[][]=new int[3][3]; //3 rows and 3 columns
//multiplying and printing multiplication of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
multi[i][j]=0;
for(int k=0;k<3;k++)
{
multi[i][j]+=a[i][k]*b[k][j];
}//end of k loop
//System.out.print(multi[i][j]+"\t"); //printing matrix
element
}//end of j loop
//System.out.println();//new line
}
return multi;
}
static void scalarProductMat(int a[][],
int k)
{
int mat[][] = a;
System.out.println("Scaler product by "+k+" is ");
// scalar element is multiplied
// by the matrix
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
mat[i][j] = mat[i][j] * k;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print(mat[i][j]+"\t");
}
System.out.println();
}
}
}
Screenshots -
Get Answers For Free
Most questions answered within 1 hours.