📅  最后修改于: 2023-12-03 15:28:08.600000             🧑  作者: Mango
本篇文章将介绍如何用Java语言设计一个具有实部和虚部数据成员的复杂数类,并提供默认和参数化的构造函数,最后编写一个程序来演示如何进行复数的算术运算。
复杂数是存在实部和虚部的数,因此,我们需要设计一个类来表示复数。这个类需要有以下几个属性:
根据这两个属性,我们可以构造一个复数。 为了方便使用,默认构造函数将实部和虚部都初始化为0。 可以使用参数化构造函数来创建一个具有指定实部和虚部的复数。
public class ComplexNumber {
private double realPart;
private double imaginaryPart;
public ComplexNumber() {
this.realPart = 0;
this.imaginaryPart = 0;
}
public ComplexNumber(double realPart, double imaginaryPart) {
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
}
// Getters and setters ommitted for brevity
}
现在,我们已经定义了复杂数类的数据成员。接下来,我们需要实现类的方法来支持基本的算术运算。
我们需要至少实现以下方法:
它们需要遵循复数的算术规则。下面是一个示例:
public class ComplexNumber {
//...
public ComplexNumber add(ComplexNumber other) {
double realPart = this.realPart + other.getRealPart();
double imaginaryPart = this.imaginaryPart + other.getImaginaryPart();
return new ComplexNumber(realPart, imaginaryPart);
}
public ComplexNumber subtract(ComplexNumber other) {
double realPart = this.realPart - other.getRealPart();
double imaginaryPart = this.imaginaryPart - other.getImaginaryPart();
return new ComplexNumber(realPart, imaginaryPart);
}
public ComplexNumber multiply(ComplexNumber other) {
double realPart = this.realPart * other.getRealPart() - this.imaginaryPart * other.getImaginaryPart();
double imaginaryPart = this.realPart * other.getImaginaryPart() + this.imaginaryPart * other.getRealPart();
return new ComplexNumber(realPart, imaginaryPart);
}
public ComplexNumber divide(ComplexNumber other) {
double denominator = Math.pow(other.getRealPart(), 2) + Math.pow(other.getImaginaryPart(), 2);
double realPart = (this.realPart * other.getRealPart() + this.imaginaryPart * other.getImaginaryPart()) / denominator;
double imaginaryPart = (this.imaginaryPart * other.getRealPart() - this.realPart * other.getImaginaryPart()) / denominator;
return new ComplexNumber(realPart, imaginaryPart);
}
public double getAbsoluteValue() {
return Math.sqrt(Math.pow(this.realPart, 2) + Math.pow(this.imaginaryPart, 2));
}
}
现在,我们可以使用这个复杂数类进行算术运算。
下面的演示程序将创建两个复杂数,同时演示了如何使用类中的方法进行算术运算。
public class ComplexNumberDemo {
public static void main(String[] args) {
ComplexNumber a = new ComplexNumber(1, 2);
ComplexNumber b = new ComplexNumber(2, -1);
ComplexNumber sum = a.add(b);
System.out.println(a + " + " + b + " = " + sum);
ComplexNumber difference = a.subtract(b);
System.out.println(a + " - " + b + " = " + difference);
ComplexNumber product = a.multiply(b);
System.out.println(a + " * " + b + " = " + product);
ComplexNumber quotient = a.divide(b);
System.out.println(a + " / " + b + " = " + quotient);
double absoluteValue = a.getAbsoluteValue();
System.out.println("|" + a + "| = " + absoluteValue);
}
}
运行此程序将输出以下结果:
1.0 + 2.0i + 2.0 - 1.0i = 3.0 + 1.0i
1.0 + 2.0i - 2.0 + 1.0i = -1.0 + 3.0i
1.0 + 2.0i * 2.0 - 1.0i = 4.0 + 3.0i
(1.0 + 2.0i) / (2.0 - 1.0i) = 0.0 + 1.0i
|1.0 + 2.0i| = 2.23606797749979
本文介绍了如何用Java语言设计一个复杂数类,同时演示了如何用这个类来进行算术运算。这是Java中一个非常实用和基础的类。希望读者通过这篇文章,能够对Java中对类的创建和运用有更深刻的了解。