复数运算的JAVA类

mac2022-06-30  80

//这个类表示复数,定义了执行复数运算的方法.public class ComplexNumber {    private double x, y;    //初始化X和Y的构造函数    public ComplexNumber(double real, double imaginary) {        this.x = real;        this.y = imaginary;

    }//返回复数实部的存取器方法    public double real() {        return x;    }    //返回复数虚部的存取器方法    public double imaginary() {        return y;    }//计算复数的幅值    public double magnitude() {        return Math.sqrt(x * x + y * y);    }//将其转换为一个字符串.这个方法很常用.    public String toString() {        return "(" + x + "," + y + ")";    }//静态类方法,接受两个复数,相加,返回它们的和.    public static ComplexNumber add(ComplexNumber a, ComplexNumber b) {        return new ComplexNumber(a.x + b.x, a.y + b.y);    }//相加的实例方法    public ComplexNumber add(ComplexNumber a) {        return new ComplexNumber(this.x + a.x, this.y + a.y);    }//复数相乘的静态类方法    public static ComplexNumber multiply(ComplexNumber a, ComplexNumber b) {        return new ComplexNumber(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);    }//复数相乘的实例方法    public ComplexNumber multiply(ComplexNumber a) {        return new ComplexNumber(x * a.x - y * a.y, x * a.y + y * a.x);    }

转载于:https://www.cnblogs.com/J2EEPLUS/archive/2007/11/28/2487840.html

相关资源:JAVA复数的加减法程序
最新回复(0)