【Java】【类和对象】复数

实现复数的加法、减法、乘法、和除法运算。

Complex:

package com.itheima1;
public class ComplexNumber {
     
    private double realPart,imageinaryPart;

    public ComplexNumber() {
     
        realPart = 0.0;
        imageinaryPart = 0.0;
    }

    public ComplexNumber(double realPart, double imageinaryPart) {
     
        this.realPart = realPart;
        this.imageinaryPart = imageinaryPart;
    }

    public double getRealPart() {
     
        return realPart;
    }

    public void setRealPart(double realPart) {
     
        this.realPart = realPart;
    }

    public double getImageinaryPart() {
     
        return imageinaryPart;
    }

    public void setImageinaryPart(double imageinaryPart) {
     
        this.imageinaryPart = imageinaryPart;
    }
    public ComplexNumber add(ComplexNumber aComNum){
     
        if(aComNum == null){
     
            System.out.println("对象不能够为null");
            return new ComplexNumber();
        }
        return new ComplexNumber(this.realPart + aComNum.getRealPart(),
                this.imageinaryPart + aComNum.getImageinaryPart());
    }
    public ComplexNumber decrease(ComplexNumber aComNum){
     
        if(aComNum == null){
     
            System.out.println("对象不能够为null");
            return new ComplexNumber();
        }
        return new ComplexNumber(this.realPart - aComNum.getRealPart(),
                this.imageinaryPart - aComNum.getImageinaryPart());
    }
    public ComplexNumber multiply(ComplexNumber aComNum){
     
        if(aComNum == null){
     
            System.out.println("对象不能够为null");
            return new ComplexNumber();
        }
        double r = this.realPart * aComNum.getRealPart() -
                this.imageinaryPart * aComNum.getImageinaryPart();
        double i = this.imageinaryPart * aComNum.getRealPart() +
                this.realPart * aComNum.getImageinaryPart();
        return new ComplexNumber(r,i);
    }
    public ComplexNumber divide(ComplexNumber aComNum){
     
        if(aComNum == null){
     
            System.out.println("对象不能够为null");
            return new ComplexNumber();
        }
        if((aComNum.imageinaryPart == 0) && (aComNum.realPart == 0)){
     
            System.out.println("除数不能为零");
            return new ComplexNumber();
        }
        double temp = aComNum.getRealPart() * aComNum.getRealPart() +
                aComNum.getImageinaryPart() * aComNum.getImageinaryPart();
        double crealpart = (this.realPart * aComNum.getRealPart()
        + this.imageinaryPart * aComNum.imageinaryPart) / temp;
        double cimaginary = (this.imageinaryPart * aComNum.getRealPart() -
                this.realPart * aComNum.getImageinaryPart()) / temp;
        return new ComplexNumber(crealpart,cimaginary);

    }

    @Override
    public String toString() {
     
        return this.realPart + " + " + this.imageinaryPart + "i";
    }
}

Main:

package com.itheima1;

public class Main {
     
    public static void main(String[] args) {
     
       ComplexNumber a = new ComplexNumber(2,4);
       ComplexNumber b = new ComplexNumber(1,2);
        System.out.println("ComplexNumber a: " + a.toString());
        System.out.println("ComplexNumber b: " + b.toString());
        System.out.println("(a + b ) = " + a.add(b).toString());
        System.out.println("(a - b ) = " + a.decrease(b).toString());
        System.out.println("(a * b ) = " + a.multiply(b).toString());
        System.out.println("(a / b ) = " + a.divide(b).toString());
    }
}

【Java】【类和对象】复数_第1张图片

你可能感兴趣的:(#,Java题解)