我想创建一个类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;
}
}
最佳答案
我只是要给出伪代码(部分假设这是一个分配)。但是希望这可以让您有足够的机会进行实施,而不必仅仅给出解决方案。
@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;
}
警告,我没有断言这是最佳选择,或者没有更好的格式化矩阵(或复数)的方法...但是,如果您可以充实进行实际编译,我怀疑您会拥有所需的再之后。