作业2 OO基础2-3、利用二维数组(double[])实现一个矩阵类:Matrix

作业2 OO基础2-3、利用二维数组(double[])实现一个矩阵类:Matrix

要求提供以下方法:
(1)set(int row, int col, double value):将第row行第col列的元素赋值为value;
(2)get(int row,int col):取第row行第col列的元素;
(3)width():返回矩阵的列数;
(4)height():返回矩阵的行数;
(5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的矩阵;
(6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的矩阵。
(7)Matrix transpose():返回当前矩阵的转置矩阵;
(8)getMax():返回矩阵中的最大值及其所在行和列;
(9)print():以行和列的形式打印出当前矩阵。(30.0分)

Matrix.java

//Matrix.java
import java.util.Arrays;
import java.util.Scanner;

public class Matrix {
    public double[][] arr;
    public int row;
    public int col;


    public Matrix(int row, int col) {
        double[][] arr = new double[row][col];

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                arr[i][j] = 0;
            }

        }
        this.arr = arr;
    }

    public Matrix(double[][] arr) {
        this.arr = arr;
    }

    public double[][] getArr() {
        return arr;
    }

    public void setArr(double[][] arr) {
        this.arr = arr;
    }

    public void set(int row, int col, double value) {
        if (row > height() || col > width() || row < 0 || col < 0) {
            System.out.println("row or col exceeds the matrix size");
        } else {
            arr[row][col] = value;
            System.out.println("The assignment is successful.\nThe value of the element in the " + row + " row and the " + col + " column is " + value);
        }
    }

    public double get(int row, int col) {
        if (row > height() || col > width() || row < 0 || col < 0) {
            System.out.println("row or col exceeds the matrix size");
            return 0;
        } else {
            System.out.println("The value of the element in the " + row + " row and the " + col + " column is " + arr[row][col]);
            return arr[row][col];
        }

    }

    public int height() {
        /*
        返回矩阵的行数
         */
        return arr.length;
    }

    public int width() {
        /*
        返回矩阵的列数
         */
        return arr[0].length;
    }

    public Matrix add(Matrix b) {
        if (this.height() == b.height() && this.width() == b.width()) {
            Matrix mx = new Matrix(this.height(), this.width());
            for (int i = 0; i < this.height(); i++) {
                for (int j = 0; j < this.width(); j++) {
                    mx.arr[i][j] = arr[i][j] + b.arr[i][j];
                }
            }
            return mx;
        } else {
            System.out.println("Matrix addition failed.");
            return null;
        }

    }

    public Matrix multiply(Matrix b) {
        if (this.width() == b.height()) {

            Matrix mx = new Matrix(this.height(), b.width());
            for (int i = 0; i < this.height(); i++) {
                for (int j = 0; j < b.width(); j++) {

                    for (int k = 0; k < this.width(); k++) {
                        mx.arr[i][j] += arr[i][k] * b.arr[k][j];
                    }
                }
            }
            return mx;
        } else {
            System.out.println("Matrix multiplication failed.");
            return null;
        }
    }

    public Matrix transpose() {
        Matrix mx = new Matrix(this.width(), this.height());
        for (int i = 0; i < this.height(); i++) {
            for (int j = 0; j < this.width(); j++) {
                mx.arr[j][i] = arr[i][j];
            }
        }
        return mx;
    }

    public double[] getMax() {
        double[] darr = new double[]{0, 0, 0};
        for (int i = 0; i < this.height(); i++) {
            for (int j = 0; j < this.width(); j++) {
                if (arr[i][j] > darr[0]) {
                    darr[0] = arr[i][j];
                    darr[1] = i;
                    darr[2] = j;
                }
            }
        }
        return darr;
    }

    public void print() {
        for (int i = 0; i < this.height(); i++) {
            for (int j = 0; j < this.width(); j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        double[][] a = new double[][]{{1, 2, 3}, {4, 5, 6}};
        double[][] b = new double[][]{{6, 5, 4}, {3, 2, 1}};
        double[][] c = new double[][]{{1, 2}, {3, 4}, {5, 6}};

        Matrix mxa = new Matrix(a);
        Matrix mxb = new Matrix(b);
        Matrix mxc = new Matrix(c);

        System.out.println("Test the print method:");
        mxa.print();
        mxb.print();
        mxc.print();


        System.out.println("Test set method:");
        mxa.set(1, 2, 10);
        mxa.print();

        System.out.println("Test get method:");
        mxa.get(1, 2);
        System.out.println();

        System.out.println("Test height method:");
        System.out.println("Number of rows of matrix a is " + mxa.height());
        System.out.println();

        System.out.println("Test width method:");
        System.out.println("Number of columns of matrix a is " + mxa.width());
        System.out.println();

        System.out.println("Test the Matrix add method:");
        mxa.add(mxb).print();

        System.out.println("Test the Matrix multiply method:");
        mxa.multiply(mxc).print();

        System.out.println("Test the Matrix transpose method:");
        mxa.transpose().print();

        System.out.println("Test the getMax method:");
        System.out.println(Arrays.toString(mxa.getMax()));
        System.out.println("The maximum value is " + mxa.getMax()[0] + ", and the position is (" + mxa.getMax()[1] + ", " + mxa.getMax()[2] + ")");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值