当前位置:首页 > Java

java复数类java程序如何编写

2026-03-20 17:20:42Java

java复数类java程序如何编写

复数类实现方法

在Java中实现复数类需要定义复数的基本属性和运算方法。复数通常由实部和虚部组成,支持加减乘除等运算。

java复数类java程序如何编写

public class Complex {
    private double real;  // 实部
    private double imaginary;  // 虚部

    // 构造函数
    public Complex(double real, double imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }

    // 获取实部
    public double getReal() {
        return real;
    }

    // 获取虚部
    public double getImaginary() {
        return imaginary;
    }

    // 复数加法
    public Complex add(Complex other) {
        return new Complex(this.real + other.real, this.imaginary + other.imaginary);
    }

    // 复数减法
    public Complex subtract(Complex other) {
        return new Complex(this.real - other.real, this.imaginary - other.imaginary);
    }

    // 复数乘法
    public Complex multiply(Complex other) {
        double newReal = this.real * other.real - this.imaginary * other.imaginary;
        double newImaginary = this.real * other.imaginary + this.imaginary * other.real;
        return new Complex(newReal, newImaginary);
    }

    // 复数除法
    public Complex divide(Complex other) {
        double denominator = other.real * other.real + other.imaginary * other.imaginary;
        double newReal = (this.real * other.real + this.imaginary * other.imaginary) / denominator;
        double newImaginary = (this.imaginary * other.real - this.real * other.imaginary) / denominator;
        return new Complex(newReal, newImaginary);
    }

    // 复数的模
    public double modulus() {
        return Math.sqrt(real * real + imaginary * imaginary);
    }

    // 复数的共轭
    public Complex conjugate() {
        return new Complex(real, -imaginary);
    }

    // 重写toString方法
    @Override
    public String toString() {
        if (imaginary >= 0) {
            return real + " + " + imaginary + "i";
        } else {
            return real + " - " + (-imaginary) + "i";
        }
    }
}

使用示例

public class Main {
    public static void main(String[] args) {
        Complex c1 = new Complex(3, 4);
        Complex c2 = new Complex(1, -2);

        System.out.println("c1 = " + c1);
        System.out.println("c2 = " + c2);

        System.out.println("c1 + c2 = " + c1.add(c2));
        System.out.println("c1 - c2 = " + c1.subtract(c2));
        System.out.println("c1 * c2 = " + c1.multiply(c2));
        System.out.println("c1 / c2 = " + c1.divide(c2));
        System.out.println("|c1| = " + c1.modulus());
        System.out.println("c1的共轭复数 = " + c1.conjugate());
    }
}

扩展功能

复数类可以根据需要扩展更多功能,例如极坐标表示、指数形式、对数运算等。对于科学计算,还可以实现更复杂的数学函数支持。

// 极坐标表示
public double getArgument() {
    return Math.atan2(imaginary, real);
}

// 指数形式
public String toExponentialForm() {
    return modulus() + " * e^(" + getArgument() + "i)";
}

标签: 复数程序
分享给朋友:

相关文章

java程序如何打包

java程序如何打包

打包Java程序的方法 使用JAR打包 Java程序可以通过JAR(Java Archive)格式打包,这是一种常见的压缩格式,适用于包含多个类文件和资源的项目。以下是使用JAR打包的步骤: 确保项…

国产操作系统如何部署react程序

国产操作系统如何部署react程序

国产操作系统部署React程序的步骤 环境准备 确保国产操作系统(如统信UOS、麒麟OS等)已安装Node.js环境。可通过官方文档或包管理器(如apt、yum)安装Node.js和npm/yarn。…

js实现程序

js实现程序

JavaScript 实现程序的基本步骤 JavaScript 是一种广泛应用于网页开发的脚本语言,能够实现动态交互功能。以下是实现一个简单 JavaScript 程序的基本方法。 创建 HTML…

java程序如何运行

java程序如何运行

运行Java程序的基本步骤 确保已安装Java开发工具包(JDK) 检查是否安装JDK,在命令行输入java -version和javac -version。若无输出或报错,需从Oracle官网下载并…

如何运行java程序

如何运行java程序

安装Java开发环境 确保系统已安装Java Development Kit (JDK)。可通过命令行输入java -version和javac -version验证是否安装成功。若未安装,需从Ora…

如何java程序

如何java程序

编写Java程序的基本步骤 安装Java开发工具包(JDK):从Oracle官网下载并安装适合操作系统的JDK版本。配置环境变量,确保java和javac命令可在终端或命令行中运行。 选择开发工…