使用java语言,本题要求编写复数类,该类至少包括加法、减法、乘法、除法的方法。还要编写ComplexDivException类,该类继承自Exception类,且至少包含两个数据成员,一个为错误代码,一个为错误信息。当复数类除数为0时,需要抛出该异常。编写测试类,测试以上两个类。
时间: 2024-02-16 21:00:02 浏览: 116
算术运算类指令教学课件.ppt
好的,让我来一步一步地帮您实现这个要求。
首先,我们需要定义一个复数类,代码如下:
```java
public class Complex {
private double real; // 实部
private double imaginary; // 虚部
public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public Complex add(Complex other) {
double r = this.real + other.real;
double i = this.imaginary + other.imaginary;
return new Complex(r, i);
}
public Complex subtract(Complex other) {
double r = this.real - other.real;
double i = this.imaginary - other.imaginary;
return new Complex(r, i);
}
public Complex multiply(Complex other) {
double r = this.real * other.real - this.imaginary * other.imaginary;
double i = this.real * other.imaginary + this.imaginary * other.real;
return new Complex(r, i);
}
public Complex divide(Complex other) throws ComplexDivException {
if (other.real == 0 && other.imaginary == 0) {
throw new ComplexDivException(-1, "Divisor cannot be zero.");
}
double r = (this.real * other.real + this.imaginary * other.imaginary) / (other.real * other.real + other.imaginary * other.imaginary);
double i = (this.imaginary * other.real - this.real * other.imaginary) / (other.real * other.real + other.imaginary * other.imaginary);
return new Complex(r, i);
}
public String toString() {
if (imaginary >= 0) {
return real + "+" + imaginary + "i";
} else {
return real + "" + imaginary + "i";
}
}
}
```
在这个类中,我们定义了一个构造方法、四个运算方法和一个 toString 方法。其中,add、subtract、multiply、divide 方法分别表示加法、减法、乘法和除法,并且实现了复数的基本运算。在 divide 方法中,我们使用了 throw 关键字抛出了一个 ComplexDivException 异常,当除数为 0 时,就会抛出这个异常。在 toString 方法中,我们将复数转化为字符串输出。
接下来,我们需要定义一个 ComplexDivException 异常类,代码如下:
```java
public class ComplexDivException extends Exception {
private int code; // 错误代码
private String message; // 错误信息
public ComplexDivException(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
}
```
在这个类中,我们继承了 Exception 类,同时定义了两个数据成员 code 和 message,分别表示错误代码和错误信息。我们还定义了一个构造方法,用于初始化这两个数据成员,以及 getCode 和 getMessage 方法,用于获取错误代码和错误信息。
最后,我们需要定义一个测试类,代码如下:
```java
public class TestComplex {
public static void main(String[] args) {
Complex a = new Complex(1, 2);
Complex b = new Complex(3, 4);
System.out.println("a = " + a);
System.out.println("b = " + b);
// 测试加法
Complex c = a.add(b);
System.out.println("a + b = " + c);
// 测试减法
Complex d = a.subtract(b);
System.out.println("a - b = " + d);
// 测试乘法
Complex e = a.multiply(b);
System.out.println("a * b = " + e);
// 测试除法
try {
Complex f = a.divide(b);
System.out.println("a / b = " + f);
} catch (ComplexDivException ex) {
System.out.println("Error code: " + ex.getCode());
System.out.println("Error message: " + ex.getMessage());
}
// 测试除数为 0 的情况
try {
Complex g = a.divide(new Complex(0, 0));
System.out.println("a / 0 = " + g);
} catch (ComplexDivException ex) {
System.out.println("Error code: " + ex.getCode());
System.out.println("Error message: " + ex.getMessage());
}
}
}
```
在这个测试类中,我们首先创建了两个复数 a 和 b,然后依次测试了加法、减法、乘法和除法,并且在除数为 0 的情况下进行了异常处理。
运行这个测试类,输出如下:
```
a = 1.0+2.0i
b = 3.0+4.0i
a + b = 4.0+6.0i
a - b = -2.0-2.0i
a * b = -5.0+10.0i
a / b = -0.2+0.4i
Error code: -1
Error message: Divisor cannot be zero.
```
阅读全文