JAva計算複素数(一)



/**
 *    
 */
public class ComplexNumber 
{
	/**     */
	private double realPart;
	/**     */
	private double imaginaryPart;
	
	/**
	 *       
	 */
	public ComplexNumber()
	{
		this.realPart = 0.0;
		this.imaginaryPart = 0.0;
	}
	
	/**
	 *     
	 * @param a   
	 * @param b   
	 */
	public ComplexNumber(double a,double b)
	{
		this.realPart = a;
		this.imaginaryPart = b;
	}
	
	/**
	 *        
	 * c = a + b      :
	 * c.   = a.   + b.  ;c.   = a.   + b.  
	 * @param aComNum   
	 * @return        ,       
	 */
	public ComplexNumber add(ComplexNumber aComNum)
	{
		if(aComNum==null)
		{
			System.err.println("      null!");
			return new ComplexNumber();
		}
		return new ComplexNumber(this.realPart + aComNum.getRealPart(),this.imaginaryPart + aComNum.getImaginaryPart());
	}
	
	/**
	 *        
	 * c = a - b      :
	 * c.   = a.   - b.  ;c.   = a.   - b.  
	 * @param aComNum   
	 * @return        ,       
	 */
	public ComplexNumber decrease(ComplexNumber aComNum)
	{
		if(aComNum==null)
		{
			System.err.println("      null!");
			return new ComplexNumber();
		}
		return new ComplexNumber(this.realPart - aComNum.getRealPart(),this.imaginaryPart - aComNum.getImaginaryPart());
	}
	
	/**
	 *        
	 * c = a * b      :
	 * c.   = a.   * b.   - a.   * b.  ;
	 * c.   = a.   * b.   + a.   * b.  ;
	 * @param aComNum   
	 * @return        ,       
	 */
	public ComplexNumber multiply(ComplexNumber aComNum)
	{
		if(aComNum==null)
		{
			System.err.println("      null!");
			return new ComplexNumber();
		}
		double newReal = this.realPart * aComNum.realPart - this.imaginaryPart * aComNum.imaginaryPart;
		double newImaginary = this.realPart * aComNum.imaginaryPart + this.imaginaryPart * aComNum.realPart;
		ComplexNumber result = new ComplexNumber(newReal,newImaginary);
		return result;
		
	}
	
	/**
	 *        
	 * c = a / b       :
	 * c.   = (a.   * b.   + a.   * b.  )/(b.   * b.   + b.   * b.  );
	 * c.   = (a.   * b.   - a.   * b.  )/(b.   * b.   + b.   * b.  );
	 * @param aComNum   
	 * @return        ,       
	 */
	public ComplexNumber divide(ComplexNumber aComNum)
	{
		if(aComNum==null)
		{
			System.err.println("      null!");
			return new ComplexNumber();
		}
		if(aComNum.getRealPart() == 0 && aComNum.getImaginaryPart() == 0)
		{
			System.err.println("      0!");
			return new ComplexNumber();
		}
		
		double temp = aComNum.getRealPart() * aComNum.getRealPart() +
		aComNum.getImaginaryPart() * aComNum.getImaginaryPart();
		
		double crealpart = (this.realPart * aComNum.getRealPart()+
				this.imaginaryPart * aComNum.getImaginaryPart())/temp;
		double cimaginarypart = (this.imaginaryPart * aComNum.getRealPart() -
				this.realPart * aComNum.getImaginaryPart())/temp;
		return new ComplexNumber(crealpart,cimaginarypart);
		
	}
	
	public String toString()
	{
		return this.realPart + " + " + this.imaginaryPart + "i";
	}
	
	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));
		System.out.println("(a - b) = " + a.decrease(b));
		System.out.println("(a * b) = " + a.multiply(b));
		System.out.println("(a / b) = " + a.divide(b));
	}

	public double getRealPart() {
		return realPart;
	}

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

	public double getImaginaryPart() {
		return imaginaryPart;
	}

	public void setImaginaryPart(double imaginaryPart) {
		this.imaginaryPart = imaginaryPart;
	}
}