复数类Complex的定义如下:

public final class Complex {
    private final double real;
    private final double imag;

    public Complex(double real, double imag) {
        this.real = real;
        this.imag = imag;
    }

    public double getReal() {
        return real;
    }

    public double getImag() {
        return imag;
    }

    public Complex add(Complex other) {
        return new Complex(this.real + other.real, this.imag + other.imag);
    }

    public Complex subtract(Complex other) {
        return new Complex(this.real - other.real, this.imag - other.imag);
    }

    public Complex multiply(Complex other) {
        double real = this.real * other.real - this.imag * other.imag;
        double imag = this.real * other.imag + this.imag * other.real;
        return new Complex(real, imag);
    }

    public Complex divide(Complex other) {
        double denominator = other.real * other.real + other.imag * other.imag;
        double real = (this.real * other.real + this.imag * other.imag) / denominator;
        double imag = (this.imag * other.real - this.real * other.imag) / denominator;
        return new Complex(real, imag);
    }

    public Complex conjugate() {
        return new Complex(this.real, -this.imag);
    }

    public double abs() {
        return Math.sqrt(this.real * this.real + this.imag * this.imag);
    }

    public double phase() {
        return Math.atan2(this.imag, this.real);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Complex) {
            Complex other = (Complex) obj;
            return this.real == other.real && this.imag == other.imag;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(this.real, this.imag);
    }

    @Override
    public String toString() {
        if (this.imag == 0) {
            return String.format("%.2f", this.real);
        } else if (this.real == 0) {
            return String.format("%.2fi", this.imag);
        } else if (this.imag > 0) {
            return String.format("%.2f+%.2fi", this.real, this.imag);
        } else {
            return String.format("%.2f%.2fi", this.real, this.imag);
        }
    }
}

二次方程类QuadraticEquation的定义如下:

public class QuadraticEquation {
    private final double a;
    private final double b;
    private final double c;

    public QuadraticEquation(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public Complex getRoot1() {
        double discriminant = getDiscriminant();
        if (discriminant >= 0) {
            double sqrtDiscriminant = Math.sqrt(discriminant);
            double x1 = (-this.b + sqrtDiscriminant) / (2 * this.a);
            return new Complex(x1, 0);
        } else {
            double real = -this.b / (2 * this.a);
            double imag = Math.sqrt(-discriminant) / (2 * this.a);
            return new Complex(real, imag);
        }
    }

    public Complex getRoot2() {
        double discriminant = getDiscriminant();
        if (discriminant >= 0) {
            double sqrtDiscriminant = Math.sqrt(discriminant);
            double x2 = (-this.b - sqrtDiscriminant) / (2 * this.a);
            return new Complex(x2, 0);
        } else {
            double real = -this.b / (2 * this.a);
            double imag = -Math.sqrt(-discriminant) / (2 * this.a);
            return new Complex(real, imag);
        }
    }

    public double getDiscriminant() {
        return this.b * this.b - 4 * this.a * this.c;
    }
}

测试代码如下:

public class Main {
    public static void main(String[] args) {
        QuadraticEquation equation = new QuadraticEquation(2, 3, 1);
        Complex root1 = equation.getRoot1();
        Complex root2 = equation.getRoot2();
        System.out.println("Root 1: " + root1);
        System.out.println("Root 2: " + root2);
    }
}

运行输出如下:

Root 1: -0.50
Root 2: -1.00
使用java定义一个不可变类复数类Complex构造方法:对复数实部虚部赋值 定义一个二次方程式ax2+bx+c=0的类:QuadraticEquation该类的数据成员:abc 方法成员: 1、合适的构造方法 2、getRoot1getRoot2用于返回二次方程的2个根该方法的返回类型是复数类Complex二次方程的根的求法需自己查询资料 3、getDiscrimin

原文地址: http://www.cveoy.top/t/topic/biTM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录