Writing complex classes in Java -- the trap of multiplication and output

Posted by philgh on Mon, 27 Sep 2021 10:32:27 +0200

Again, I need to write a complex class this time. I need to implement two construction methods: default and parametric, get and set methods, addition, subtraction and multiplication of complex and real numbers, addition, subtraction and multiplication of complex and complex numbers, and methods to output complex numbers. In fact, after mastering the basic knowledge, this assignment is not very difficult, but there are more methods to write. Another very important point is the consideration of details.
The complete code is at the end of the article. Before that, we will discuss some small problems that novices are prone to make mistakes.

Multiplication trap

Let's start with a code:

ComplexNumber complexMulti(ComplexNumber c){
		/**
		*Multiplication of current complex number and formal parameter complex number
		*@param c Received parameter, a complex number
		*@return Returns the result of a multiplication operation
		*/
		realPart = realPart * c.realPart - iPart * c.iPart;
		iPart = iPart * c.realPart + realPart * c.iPart;
		return this;
	}

Do you think there is something wrong with this code? Can it get the correct product?
If you can't see it, you can try running it.
Run it and you'll find an outrageous result. Why is that?
I was also confused at that time. Compared with manual calculation, the real part of the product is correct, but the imaginary part is wrong, so think carefully and find out what the problem is?
In the sentence "realPart = realPart * c.realPart - iPart * c.iPart", in this sentence, the original plural realPart has changed, while in the latter sentence, the changed realPart is used when calculating iPart.
Therefore, the solution is to create a new temporary variable and store the real and imaginary parts of the original complex number in this temporary variable. The code is as follows:

ComplexNumber complexMulti(ComplexNumber c){
		/**
		*Multiplication of current complex number and formal parameter complex number
		*@param c Received parameter, a complex number
		*@return Returns the result of a multiplication operation
		*/
		double r1 = realPart;
		double r2 = c.realPart;
		double i1 = iPart;
		double i2 = c.iPart;
		realPart = r1 * r2 - i1 * i2;
		iPart = i1 * r2 + r1 * i2;
		return this;
	}

Try running again, try a few more complex numbers, and you will find that the results are correct.

Output trap

After you finish some calculations, you may want the program to report this complex number to you. Therefore, you will use the toString method to form a string and print it on the screen. How simple toString is, you may think like this, that is:

public String toString(){
			/**
			*Output complex numbers as strings
			*@return Return string
			*/
		String s = "";
		s = this.realPart + "+" + this.iPart + "i";	
		return s;
	}

Tell me, do you think so?
Of course, this writing method is not wrong, but it is not rigorous enough. It is only applicable to the case where the real part and imaginary part are greater than zero. In other cases, this writing method will produce some results that are not in line with our usual habits: for example, "0 ± 1.5i", and normal people will write "- 1.5i".
Therefore, several situations should be considered:
For the real part, we only need to consider whether it is zero. If it is zero, it will be omitted, because there is nothing in front of it. Even if there is a minus sign, it is in line with the conventional writing method;
For the imaginary part, three cases of positive, negative and zero need to be considered, because it needs to be connected with the previous real part. If it is zero, it is omitted. If it is regular, no sign is required. If it is negative, the previous "+" sign needs to be removed.
There are six cases of real part multiplication and imaginary part multiplication. However, considering that "+" connection is not required when the real part is zero, the latter imaginary part can be expressed in the same way whether it is greater than zero or less than zero. Therefore, only five cases are required, and the code is as follows:

public String toString(){
			/**
			*Output complex numbers as strings
			*@return Return string
			*/
		String s = "";
		
		if(this.realPart == 0 && this.iPart != 0){
			s = iPart + "i";	
		}else if(this.realPart == 0 && this.iPart == 0){
			s = "" + 0.0;	
		}else if(this.realPart != 0 && this.iPart < 0){
			s = this.realPart + "" + this.iPart + "i";
		}else if(this.realPart != 0 && this.iPart == 0){
			s = this.realPart + "";	
		}else if(this.realPart != 0 && this.iPart > 0){
			s = this.realPart + "+" + this.iPart + "i";	
		}
		
		return s;
	}

For example, if the imaginary part is 1i, usually 1 can also be omitted.

Complete code

The following is the complete code, which needs to be taken by yourself.

/**
*@author Linda ZHENG
*/

public class ComplexNumber{
	public static void main(String[] args){
		ComplexNumber c = new ComplexNumber(4.0, 5.0);
		ComplexNumber cc = new ComplexNumber(3.0, -4.0);
		c.complexMulti(cc);
		System.out.println(c.toString());	
	}
	
	double realPart;
	double iPart;
	
	ComplexNumber(){
		/**
		*Nonparametric construction method
		*/
		realPart = 0.0;
		iPart = 0.0;	
	}
	
	ComplexNumber(double r, double i){
		/**
		*Nonparametric construction method
		*@param r First parameter received, real part
		*@param i Second parameter received, imaginary part
		*/
		realPart = r;
		iPart = i;	
	}
	
	double getRealPart(){
		/**
		*Get real part
		*@return real part
		*/
		return realPart;	
	}
	
	void setRealPart(double d){
		/**
		*Set real part
		*@param d Received parameter, real part
		*/
		realPart = d;	
	}

	double getIPart(){
		/**
		*Get real part
		*@return real part
		*/
		return iPart;	
	}
	
	void setIPart(double d){
		/**
		*Set imaginary part
		*@param d Received parameter, imaginary part
		*/
		iPart = d;	
	}
	
	ComplexNumber complexAdd(ComplexNumber c){
		/**
		*The current complex number is added to the formal parameter complex number
		*@param c Received parameter, a complex number
		*@return Returns the result of an addition operation
		*/
		realPart += c.realPart;
		iPart += c.iPart;
		return this;	
	}
	
	ComplexNumber complexAdd(double c){
		/**
		*The current complex number is added to the formal parameter real number
		*@param c Received parameter, a real number
		*@return Returns the result of an addition operation
		*/
		realPart += c;
		return this;	
	}
	
	ComplexNumber complexMinus(ComplexNumber c){
		/**
		*Subtraction of current complex number and formal parameter complex number
		*@param c Received parameter, a complex number
		*@return Returns the result of the subtraction operation
		*/
		realPart -= c.realPart;
		iPart -= c.iPart;
		return this;	
	}
	
	ComplexNumber complexMinus(double c){
		/**
		*Subtracting the current complex number from the formal parameter real number
		*@param c Received parameter, a real number
		*@return Returns the result of the subtraction operation
		*/
		realPart -= c;
		return this;	
	}
	
	ComplexNumber complexMulti(ComplexNumber c){
		/**
		*Multiplication of current complex number and formal parameter complex number
		*@param c Received parameter, a complex number
		*@return Returns the result of a multiplication operation
		*/
		double r1 = realPart;
		double r2 = c.realPart;
		double i1 = iPart;
		double i2 = c.iPart;
		realPart = r1 * r2 - i1 * i2;
		iPart = i1 * r2 + r1 * i2;
		return this;
	}
	
	ComplexNumber complexMulti(double c){
		/**
		*The current complex number is multiplied by the formal parameter real number
		*@param c Received parameter, a real number
		*@return Returns the result of a multiplication operation
		*/
		realPart *= c;
		iPart *= c;
		return this;
	}
	

	public String toString(){
			/**
			*Output complex numbers as strings
			*@return Return string
			*/
		String s = "";
		
		if(this.realPart == 0 && this.iPart != 0){
			s = iPart + "i";	
		}else if(this.realPart == 0 && this.iPart == 0){
			s = "" + 0.0;	
		}else if(this.realPart != 0 && this.iPart < 0){
			s = this.realPart + "" + this.iPart + "i";
		}else if(this.realPart != 0 && this.iPart == 0){
			s = this.realPart + "";	
		}else if(this.realPart != 0 && this.iPart > 0){
			s = this.realPart + "+" + this.iPart + "i";	
		}
		
		return s;
	}
}

Topics: Java