题目描述
编写复数类,该类至少包括计算复数的加法、减法、乘法、除法的方法。
编写ComplexDivException类,该类继承Exception类,且至少包含两个数据成员,一个为错误代码,一个为错误信息。当复数类除数为0时要抛出该异常。
编写测试类,测试以上两个类。
输入
第一行为一个字符串,只可能为add,sub,mul,div四个字符串之一。依次代表本次测试执行加法、减法、乘法和除法。
第二行为一个整数n(0<n<100),代表共n组测试用例。
后边为n行,每行均为4个用空格分隔的浮点数,依次参与运算的两个复数c1、c2。第一个数代表c1的实部,第二个数代表c1的虚部,第三个数代表c2的实部,第四个数代表c2的虚部。
输出
若干行,通常一行对应输入的一行,为c1加或减或乘或除以c2的结果(保留一位小数,具体格式见样例),如果除法的除数为0,则输出两行对应输入的一行,这两行的内容为(注意空格):
Error No : 1001
Error Message : Divide by zero.
前边的1001为错误代码,后边Divide by zero. 为错误信息。
注意输出时建议采用String的格式化,或printf的格式化,不建议采用DecimalFormat。
样例输入 Copy
add 6 1 1 -1 -1 90.9 68.8 60.3 79.3 4.3 65.4 69.2 -49.7 -8.33 -1.14 4.2 1.62 3.7 1.86 -5.46 9.22 15.1 -19.6 2.2 -3.73
样例输出 Copy
0.0+0.0i 151.2+148.1i 73.5+15.7i -4.1+0.5i -1.8+11.1i 17.3-23.3i
import java.util.Scanner;
public class ComplexDivExceptionTest{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String operation = scanner.nextLine();
int n = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < n; i++) {
String[] input = scanner.nextLine().split(" ");
double real1 = Double.parseDouble(input[0]);
double imag1 = Double.parseDouble(input[1]);
double real2 = Double.parseDouble(input[2]);
double imag2 = Double.parseDouble(input[3]);
Complex c1 = new Complex(real1, imag1);
Complex c2 = new Complex(real2, imag2);
try {
Complex result;
switch (operation) {
case "add":
result = c1.add(c2);
System.out.println(result);
break;
case "sub":
result = c1.sub(c2);
System.out.println(result);
break;
case "mul":
result = c1.mul(c2);
System.out.println(result);
break;
case "div":
result = c1.div(c2);
System.out.println(result);
break;
}
} catch (ComplexDivException e) {
System.out.println("Error No : " + e.getErrorCode());
System.out.println("Error Message : " + e.getErrorMessage());
}
}
}
}
class Complex {
private double real;
private double imag;
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public Complex add(Complex other) {
return new Complex(this.real + other.real, this.imag + other.imag);
}
public Complex sub(Complex other) {
return new Complex(this.real - other.real, this.imag - other.imag);
}
public Complex mul(Complex other) {
double newReal = this.real * other.real - this.imag * other.imag;
double newImag = this.real * other.imag + this.imag * other.real;
return new Complex(newReal, newImag);
}
public Complex div(Complex other) throws ComplexDivException {
double denominator = other.real * other.real + other.imag * other.imag;
if (denominator == 0) {
throw new ComplexDivException(1001, "Divide by zero.");
}
double newReal = (this.real * other.real + this.imag * other.imag) / denominator;
double newImag = (this.imag * other.real - this.real * other.imag) / denominator;
return new Complex(newReal, newImag);
}
@Override
public String toString() {
if (imag >= 0) {
return String.format("%.1f+%.1fi", real, imag);
} else {
return String.format("%.1f%.1fi", real, imag);
}
}
}
class ComplexDivException extends Exception {
private int errorCode;
private String errorMessage;
public ComplexDivException(int errorCode, String errorMessage) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}
public int getErrorCode() {
return errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
}
文章描述了一个Java编程任务,要求创建一个复数类,包含复数的加、减、乘、除方法,并在除法中处理除以零的情况,抛出自定义的`ComplexDivException`异常。此外,还需编写测试类对这些功能进行验证,输入包括运算类型和多组复数测试用例,输出为运算结果或异常信息。
3508





