Write a Java application program and use the complex number complex to verify that the addition of two complex numbers 1+2i and 3+4i produces a new complex number 4+6i. The complex requirements are as follows:

(1) The properties of the complex type Complex are:
RealPart: int type, representing the real part of the complex number,
ImaginPart; int type, representing the imaginary part of the complex number
(2) The methods of the complex type Complex are:
Complex(): Constructor function The real and imaginary parts of both are set to 0.
Complex(int ​​r.int i): Constructor, the formal parameter r is the initial value of the real part and the initial value of the imaginary part.
Complex complexAdd(Complex a): Add the current complex number object and the formal parameter complex number object, the result
is still a complex value, and return to the caller of this method.
String ToString(): Combine the real and imaginary parts of the current complex number object into a+bi string form, where a and b are the data of the real and imaginary parts respectively.
Calca

public class Calca {
    
    
	public static void main(String[] args) {
    
    
		Complex a=new Complex(1,2);
		Complex b=new Complex(3,4);
		System.out.println("a="+a.ToString());
		System.out.println("b="+b.ToString());
		Complex c=a.complexAdd(b);
		System.out.println("a+b="+c.ToString());
	}

}

Complex class

public class Complex {
    
    
	int RealPart;
	int ImaginPart;
	Complex(){
    
    
		RealPart=0;
		ImaginPart=0;
	}
	Complex(int r,int i){
    
    
		RealPart=r;
		ImaginPart=i;
	}
	Complex complexAdd(Complex a) {
    
    
		this.RealPart+=a.RealPart;
		this.ImaginPart+=a.ImaginPart;
		return this;
	}
	String ToString() {
    
    
		String str=RealPart+"+"+ImaginPart+"i";
		return str;
	}
}

Guess you like

Origin blog.csdn.net/The_Handsome_Sir/article/details/108888002