声明复数类Complex,成员变量包括实部和虚部,类型均为double。要求该类具有 (1)实现多个构造方法(如无参构造方法、1个参数构造方法,2个参数构造方法,字符串参数的构造方法)。 (2)使用getter和setter完成属性的访问和修改 (3)成员方法包括实现复数加法add、减法minus、乘法multiply,计算模长length (4)重写equals()方法,只有当两个复数实部和虚部都相等时,才返回真 (5)重写toString(),输出复数信息 (6)在main方法中,使用实际数据测试成员方法
时间: 2024-02-12 19:08:00 浏览: 23
好的,这是一个关于复数类 Complex 的代码实现:
```java
public class Complex {
private double realPart; // 实部
private double imaginaryPart; // 虚部
// 无参构造方法
public Complex() {
this.realPart = 0.0;
this.imaginaryPart = 0.0;
}
// 1个参数构造方法
public Complex(double realPart) {
this.realPart = realPart;
this.imaginaryPart = 0.0;
}
// 2个参数构造方法
public Complex(double realPart, double imaginaryPart) {
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
}
// 字符串参数的构造方法
public Complex(String complexString) {
String[] parts = complexString.split("\\+|i");
this.realPart = Double.parseDouble(parts[0]);
this.imaginaryPart = Double.parseDouble(parts[1]);
}
// getter和setter
public double getRealPart() {
return realPart;
}
public void setRealPart(double realPart) {
this.realPart = realPart;
}
public double getImaginaryPart() {
return imaginaryPart;
}
public void setImaginaryPart(double imaginaryPart) {
this.imaginaryPart = imaginaryPart;
}
// 复数加法
public Complex add(Complex other) {
double realPart = this.realPart + other.realPart;
double imaginaryPart = this.imaginaryPart + other.imaginaryPart;
return new Complex(realPart, imaginaryPart);
}
// 复数减法
public Complex minus(Complex other) {
double realPart = this.realPart - other.realPart;
double imaginaryPart = this.imaginaryPart - other.imaginaryPart;
return new Complex(realPart, imaginaryPart);
}
// 复数乘法
public Complex multiply(Complex other) {
double realPart = this.realPart * other.realPart - this.imaginaryPart * other.imaginaryPart;
double imaginaryPart = this.realPart * other.imaginaryPart + this.imaginaryPart * other.realPart;
return new Complex(realPart, imaginaryPart);
}
// 计算模长
public double length() {
return Math.sqrt(realPart * realPart + imaginaryPart * imaginaryPart);
}
// 重写equals()方法
@Override
public boolean equals(Object obj) {
if (obj instanceof Complex) {
Complex other = (Complex) obj;
return this.realPart == other.realPart && this.imaginaryPart == other.imaginaryPart;
}
return false;
}
// 重写toString()方法
@Override
public String toString() {
if (imaginaryPart >= 0) {
return realPart + "+" + imaginaryPart + "i";
} else {
return realPart + "" + imaginaryPart + "i";
}
}
// 测试
public static void main(String[] args) {
Complex c1 = new Complex(1.0, 2.0);
Complex c2 = new Complex(3.0, 4.0);
Complex c3 = new Complex("5+6i");
System.out.println("c1 = " + c1.toString());
System.out.println("c2 = " + c2.toString());
System.out.println("c3 = " + c3.toString());
System.out.println("c1 + c2 = " + c1.add(c2).toString());
System.out.println("c1 - c2 = " + c1.minus(c2).toString());
System.out.println("c1 * c2 = " + c1.multiply(c2).toString());
System.out.println("c1.equals(c2) = " + c1.equals(c2));
System.out.println("c1.equals(new Complex(1.0, 2.0)) = " + c1.equals(new Complex(1.0, 2.0)));
System.out.println("|c1| = " + c1.length());
System.out.println("|c2| = " + c2.length());
System.out.println("|c3| = " + c3.length());
}
}
```
在 main 方法中,我们进行了一些测试,输出结果如下:
```
c1 = 1.0+2.0i
c2 = 3.0+4.0i
c3 = 5.0+6.0i
c1 + c2 = 4.0+6.0i
c1 - c2 = -2.0-2.0i
c1 * c2 = -5.0+10.0i
c1.equals(c2) = false
c1.equals(new Complex(1.0, 2.0)) = true
|c1| = 2.23606797749979
|c2| = 5.0
|c3| = 7.810249675906654
```