Home > Software engineering >  How can I print out the result matrix in my java code?
How can I print out the result matrix in my java code?

Time:03-19

Create a basics.Matrix class (using a two-dimensional array of real numbers as a matrix) that has the following operations: construct an M × N zero matrix, construct an M × N matrix using an M × N array, create an N × N dimensional unit matrix ( the result matrix should be a return value), the matrix transposed resp. calculating the sum and difference of two matrices, representing the matrix as a string (use java.lang.StringBuilder to generate the text).

Also create a main program (Main.java) that tests these operations!

My problem is in my basicsMatrixMain.java code, that I do not know how can I print out thre results I get from difference or transpone. Can anybody help me to solve it ?

public class basicsMatrix {
    private final int N;             
    private final int M;             
    private final double[][] matrix;  

    public basicsMatrix(int M, int N) {
        this.N = N;
        this.M = M;
        matrix = new double[M][N]; 
    }

    public basicsMatrix(double[][] matrix) {
        M = matrix.length;
        N = matrix[0].length;
        this.matrix = new double[N][M];
        for (int i = 0; i < M; i  )
            for (int j = 0; j < N; j  )
                    this.matrix[i][j] = matrix[i][j];
    }

    public void transzponalas(double[][] matrix1){
        double[][] transpose = new double[M][N];
        for(int i = 0; i < N; i  ) {
            for (int j = 0; j < M; j  ) {
                transpose[j][i] = matrix1[i][j];
            }
        }
    }
    public void add(double[][] matrix1,double[][] matrix2){
        double[][] osszeadas = new double[N][M];
        for(int i = 0; i < N; i  ) {
            for (int j = 0; j < M; j  ) {
                osszeadas[i][j] = (matrix1[i][j]   matrix2[i][j]);
            }
        }
    }
    public void difference(int matrix1[][], int matrix2[][]){
        double[][] kivonas = new double[N][M];
        for(int i = 0; i <= N; i  ){
            for(int j = 0; j <= M; j  ){
                kivonas[i][j] = matrix1[i][j] - matrix2[i][j];
            }
        }
    }
    public String matrixtostring(double[][] matrix1){
 
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < N; i  ) {
            for (int j = 0; j < M; j  ) {
                sb.append(matrix1);
            }
        }
        return sb.toString();
    }
}
public class basicsMatrixMain {
    public static void main(String[] args) {
        int N = 2;
        int M = 3;
        double[][] matrix1 = { {2, 3, 4}, {5, 2, 3} };
        double[][] matrix2 = { {-4, 5, 3}, {5, 6, 3} };


    System.out.println("\n");
    System.out.print("Difference:");
    for(int i = 0; i <= N; i  ){
        for(int j = 0; j <= M; j  ){
            System.out.println();
        }
    }
}
}

CodePudding user response:

You have defined a lot of functions in basicsMatrix that you can use here.

However there are some changes that you need to make. In your add, difference and transpose methods, you define something but you never save the result. I would recommend something like this:

public double[][] transzponalas(double[][] matrix1){
    double[][] transpose = new double[M][N];
    for(int i = 0; i < N; i  ) {
        for (int j = 0; j < M; j  ) {
            transpose[j][i] = matrix1[i][j];
        }
    }
    return transpose;
}
public double[][] add(double[][] matrix1,double[][] matrix2){
    double[][] osszeadas = new double[N][M];
    for(int i = 0; i < N; i  ) {
        for (int j = 0; j < M; j  ) {
            osszeadas[i][j] = (matrix1[i][j]   matrix2[i][j]);
        }
    }
    return osszeadas;
}
public double[][] difference(int matrix1[][], int matrix2[][]){
    double[][] kivonas = new double[N][M];
    for(int i = 0; i <= N; i  ){
        for(int j = 0; j <= M; j  ){
            kivonas[i][j] = matrix1[i][j] - matrix2[i][j];
        }
    }
    return kivonas;
}

All these functions now return a matrix that you can print out.

Now you just print you matrices. Something like this should work.

System.out.println(matrixtostring(transzponalas(matrix1)));
System.out.println(matrixtostring(add(matrix1,matrix2)));
System.out.println(matrixtostring(difference(matrix1,matrix2)));

CodePudding user response:

Looking at the question description.

"Create a basics.Matrix class (using a two-dimensional array of real numbers as a matrix) that has the following operations: construct an M × N zero matrix, construct an M × N matrix using an M × N array, create an N × N dimensional unit matrix ( the result matrix should be a return value), the matrix transposed resp. calculating the sum and difference of two matrices, representing the matrix as a string (use java.lang.StringBuilder to generate the text).

Also create a main program (Main.java) that tests these operations!"

I suspect that you are supposed to create a class that calls functions on itself. In other words the basicsMatrix will become your matrix.

For example, the transzponalas method would become

public void transzponalas(){
    double[][] transpose = new double[M][N];
    for(int i = 0; i < N; i  ) {
        for (int j = 0; j < M; j  ) {
            transpose[j][i] = this.matrix[i][j];
        }
    }
    int tmp = this.M
    this.M = this.N
    this.N = tmp
    this.matrix = transpose;
}

This way, you change the matrix that is inside your basicsMatrix. You should make sure that you understand the assignment correctly.

  • Related