用JAVA定义个复数类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用JAVA定义个复数类相关的知识,希望对你有一定的参考价值。

定义个复数类,可以通过构造函数给复数对象赋值,实部和虚部都是该类的的私有属性,必须有获取和修改属性的方法,并定义它与复数、实数相加及复数间乘、除的方法。

public class Complex

private double x;//实部
private double y;//虚部

public Complex()

/**构造函数
* @param x 实数部分
* @param y 虚数部分
*/
public Complex(double x,double y)
super();
this.x = x;
this.y = y;


/**求模
* @return 该复数的模
*/
public double mod()
return x * x + y * y;


/**复数间加法
* @param complex 加数
* @return 计算结果
*/
public Complex add(Complex complex)
double x = this.x + complex.x;
double y = this.y + complex.y;
return new Complex(x,y);


/**复数与实数的加法
* @param a 加数
* @return 计算结果
*/
public Complex add(double a)
return this.add(new Complex(a,0));


/**复数间减法
* @param complex 减数
* @return 计算结果
*/
public Complex subtract(Complex complex)
double x = this.x - complex.x;
double y = this.y - complex.y;
return new Complex(x,y);


/**复数与实数的减法
* @param a 减数
* @return 计算结果
*/
public Complex subtract(double a)
return subtract(new Complex(a,0));


/**复数间乘法
* @param complex 乘数
* @return 计算结果
*/
public Complex multiply(Complex complex)
double x = this.x * complex.x - this.y * complex.y;
double y = this.y * complex.x + this.x * complex.y;
return new Complex(x,y);


/**复数间除法
* @param complex 除数
* @return 计算结果
*/
public Complex divide(Complex complex)
double x = (this.x * complex.x + this.y * complex.y) / (complex.mod());
double y = (this.y * complex.x - this.x * complex.y) / (complex.mod());
return new Complex(x,y);


public String toString()
StringBuffer sb = new StringBuffer();
if(x != 0)
sb.append(x);
if(y > 0)
sb.append("+" + y + "i");
else if(y < 0)
sb.append(y + "i");

else
if(y != 0)
sb.append(y + "i");


if(x == 0 && y == 0)
return "0";

return sb.toString();


public double getX()
return x;


public void setX(double x)
this.x = x;


public double getY()
return y;


public void setY(double y)
this.y = y;


public static void main(String[] args)
Complex a = new Complex(2,0.5);
Complex b = new Complex(0.5,2);
System.out.println("(" + a + ")+(" + b + ")=" + a.add(b));
System.out.println("(" + a + ")+" + 2 + "=" + a.add(2));
System.out.println("(" + a + ")-(" + b + ")=" + a.subtract(b));
System.out.println("(" + a + ")-" + 2 + "=" + a.subtract(2));
System.out.println("(" + a + ")*(" + b + ")=" + a.multiply(b));
System.out.println("(" + a + ")/(" + b + ")=" + a.divide(b));


参考技术A public class Test

private int shi = 0; //实数部分
private int xu = 0; //虚数部分

public Test()
public Test(int shi,int xu)
this.shi = shi;
this.xu = xu;


//加法
public Test jia(Test t)
Test test = new Test();
test.setShi(this.shi + t.getShi());
test.setXu(this.xu + t.getXu());
return test;

//减法
public Test jian(Test t)
Test test = new Test();
test.setShi(this.shi-t.getShi());
test.setXu(this.xu - t.getXu());
return test;

//乘法
public Test cheng(Test t)
Test test = new Test();
test.setShi(this.shi*t.getShi()-this.xu*t.getXu());
test.setXu(this.xu*t.getShi()+this.shi*t.xu);
return test;

//除法
public Test chu(Test t)
Test test = new Test();
test.setShi( (this.shi*t.getShi()+this.xu*t.getXu()) / (t.getShi()*t.getShi()+t.getXu()*t.getXu()) );
test.setXu((this.xu*t.getShi() - this.shi*t.getXu()) / (t.getShi()*t.getShi()+t.getXu()*t.getXu()) );
return test;


public int getShi()
return this.shi;

public void setShi(int shi)
this.shi = shi;

public int getXu()
return this.xu;

public void setXu(int xu)
this.xu = xu;

public String getValue()
return shi+"+"+xu+"i";

public void setValue(int shi,int xu)
this.shi = shi;
this.xu = xu;


public static void main(String args[])
Test t1 = new Test(3,4);
Test t2 = new Test();
t2.setShi(6);
t2.setShi(8);
Test t3 = t1.cheng(t2);
System.out.println(t3.getValue());

以上是关于用JAVA定义个复数类的主要内容,如果未能解决你的问题,请参考以下文章

用java定义一个复数类Complex,能够创建复数对象,并且实现复数之间的加、减运算

用java 编写一个复数类

java构造一个复数类

C++编程,定义一个复数类

定义一个复数类Complex,复数的实部real与虚步image定义为私有数据成员,

复数类的设计问题