当前位置:首页 > Java

java复数类java程序如何编写

2026-03-20 17:20:42Java

复数类实现方法

在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());
    }
}

扩展功能

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

java复数类java程序如何编写

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

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

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

相关文章

vue实现手机小程序

vue实现手机小程序

Vue 实现手机小程序的方案 Vue 本身是一个前端框架,主要用于构建 Web 应用。若需开发手机小程序,需结合其他工具或框架。以下是常见的实现方案: 使用 Uni-app 框架 Uni-app 是…

vue怎么实现小程序

vue怎么实现小程序

Vue 实现小程序的方案 Vue 可以通过多种方式实现小程序开发,以下是常见的几种方案: 使用 uni-app 框架 uni-app 是一个基于 Vue.js 的开发框架,允许开发者编写一次代码,同…

如何运行一个react程序

如何运行一个react程序

安装Node.js和npm 确保系统已安装Node.js(包含npm)。可通过终端输入node -v和npm -v验证版本。若未安装,从Node.js官网下载并安装最新LTS版本。 创建React项…

react native实现小程序

react native实现小程序

React Native 实现小程序的方案 React Native 本身是用于开发跨平台移动应用的框架,但通过一些工具和平台,可以将其转换为小程序格式(如微信小程序、支付宝小程序等)。以下是几种常见…

js程序实现

js程序实现

JavaScript 程序实现的基本方法 JavaScript 是一种广泛使用的编程语言,主要用于网页开发。以下是实现 JavaScript 程序的一些基本方法。 变量声明与使用 JavaScrip…

如何运行java程序

如何运行java程序

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