/**
* 复数类
*/
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;
}
}
java计算复数(一)
最新推荐文章于 2024-09-22 12:14:24 发布