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