Java-复数矩阵

时间:2019-04-20 19:41:32

标签: java

我想创建一个类ComplexMatrix,该类具有字段ComplexNumber类型的array [NxN]。我已经制作了一个ComplexNumber类,如下所示:

public class ComplexNumber {

    private double real;
    private double img;

    // Getters and setters
    public double getReal() {
        return real;
    }

    public void setReal(double real) {
        this.real = real;
    }

    public double getImg() {
        return img;
    }

    public void setImg(double img) {
        this.img = img;
    }

    // Constructor
    public ComplexNumber(double real, double img) {
        this.real = real;
        this.img = img;
    }

    // Add
    public ComplexNumber addComp(ComplexNumber num) {

        ComplexNumber num1 = new ComplexNumber(real + num.real, img + num.img);

        return num1;
    }

    // Subtract
    public ComplexNumber subtractComp(ComplexNumber num) {

        ComplexNumber num1 = new ComplexNumber(real - num.real, img - num.img);

        return num1;
    }

    // Multiply
    public ComplexNumber multiplyComp(ComplexNumber num) {

        ComplexNumber num1 = new ComplexNumber(real*num.real-img*num.img,real*num.img+img*num.real);

        return num1;
    }

    // Is Equals
    boolean equals(ComplexNumber num) {

        ComplexNumber num1 = new ComplexNumber(real, img);
        if (num.real == num1.real && num.img == num1.img) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public String toString() {

        if (img > 0) {
            return getReal() + " + " + Math.abs(getImg()) + "i";
        }
        if (img < 0) {
            return getReal() + " - " + Math.abs(getImg()) + "i";
        }
        if (real==0) {
            return getImg() + "i";
        }
        if (img==0) {
            return getReal() + "";
        }
        return null;
    }

}

具有一些方法和自己的toString,其输出如下:

  3.51 + 1.87i
    2.35 - 5.61i
    -8.45 + 2.65i

已实现的ComplexMatrix具有一个数组[M] [N]作为字段,而cronstructor public ComplexMatrix(int rows, int cols)给出了该数组 使用方法computeRandom从一到十个随机数。然后一个 toString()函数(也从ComplexNumber类实现toSting())应打印随机数数组。所需的ComplexMatrix打印为:

[1.24 + 2.55i, -0.32 + 2.00i, 1.35 - 5.88i;
-5.71 - 5.91i, 0.29 – 9.14i, 0.00 + 3.51i;
6.44 + 0.00i, -3.51 – 0.67i, 2.10 + 4.20i;]

这是ComplexMatrix类:

         import java.util.Random;

public class ComplexMatrix {

    private ComplexNumber[][] complexArray;

    // Default Constructor
    public ComplexMatrix() {
        super();
        this.complexArray = null;
    }

    // Copy Constructor
    public ComplexMatrix(ComplexMatrix original) {
        super();
        original.complexArray = complexArray;
    }

    private Random rand = new Random();

    private double computeRandom() {
        int randomNum = (int) ((rand.nextDouble() - 0.5) * rand.nextInt(20) * 100);
        return randomNum / 100.0;
    }

    // Random Numbers Constructor
    public ComplexMatrix(int rows, int cols)  {

        double real = 0;
        double img = 0;

        ComplexNumber[][] complexArray= new ComplexNumber[rows][cols];

        for (int i = 0; i < rows; i++) {
            System.out.print("\n");
            for (int j = 0; j < cols; j++) {
                real = computeRandom();
                img = computeRandom();
                complexArray[i][j] = new ComplexNumber(real, img);
                //edw peiramatizomai..to print 8a ginetai sthn toString()
                System.out.print(complexArray[i][j].toString());
                System.out.print("\t");
            }
        }

    }

    @Override
    public String toString() {
        int rows = 0,cols = 0;
        //ComplexNumber[][] complexArray= new ComplexNumber[rows][cols];
        ComplexMatrix s = new ComplexMatrix(rows,cols);

        String out = "[";
        //????????????????????????????????????
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++) {
                //????????
                out = s.toString();

            }
        out += "]";
        return out;

    }
}

2 个答案:

答案 0 :(得分:0)

我将要给出伪代码(部分假设这是一个分配)。但是希望这可以使您有足够的机会进行实施,而不必仅仅给出解决方案。

@Override
public String toString() {
    out = "[";
    for (row : rows) {
        if (notFirstRow) {
            out += "\n"; // New line
        }
        for (column : columns) {
            if (notFirstColumn) {
                out += ", ";
            }
            out += matrix[row][column].toString();
        }
    }
    out += "]";
    return out;
}

警告,我没有断言这是最佳选择,或者没有更好的格式化矩阵(或复数)的方法...但是,如果您可以充实进行实际编译,我怀疑您会你在追求什么。

答案 1 :(得分:0)

我认为这里的主要问题是按照所需的格式打印。这是实现此目的的示例代码:

@Override
public String toString() {
    StringBuilder out = new StringBuilder();
    out = out.append("[");
    for (int row = 0; row < rows; row++) {
        for (int column = 0; column < columns; column++) {
            out.append(String.valueOf(matrix[row][column]));
            //Populate according to whether last column or not.
            out.append(column != columns - 1 ? ", " : ";");
        }
        out.append(row != rows - 1 ? "\n" : "");
    }
    out.append("]");
    return out.toString();
}

请记住,对于嵌套数组,您在标准库中有一个Arrays.deepToString方法。