《Java实现复数运算》由会员分享,可在线阅读,更多相关《Java实现复数运算(2页珍藏版)》请在人人文库网上搜索。
1、定义一个复数类complex,它的内部具有两个实例变量:realPart和imagPart,分别代表复数的实部和虚部,编程实现要求的数学运算。(1)实现两个复数相加。复数加运算的原则是:复数的实部和虚部分别相加。(2)实现两个复数相减。复数减运算的原则是:复数的实部和虚部分别相减。(3)输出运算结果,判断是否正确。*/ 程序清单/* * Complex.java*/public class Complex / real partprivate double realPart;/ imag partprivate double imagePart;/ default initpublic Com。
2、plex() this.realPart = 0.0;this.imagePart = 0.0;/ init with argumentspublic Complex(double real, double image) this.realPart = real;this.imagePart = image;/ show the complexpublic void show() if (this.realPart = 0.0) & (this.imagePart = 0.0)System.out.println(0);else if (this.realPart = 0.0)System.o。
3、ut.println(this.imagePart + i);else if (this.imagePart = 0.0)System.out.println(this.realPart);elseSystem.out.println(this.realPart + + + this.imagePart + i);/ add two complexespublic void add(Complex x, Complex y) this.realPart = x.realPart + y.realPart;this.imagePart = x.imagePart + y.imagePart;/s。
4、ub two complexespublic void sub(Complex x, Complex y) this.realPart = x.realPart - y.realPart;this.imagePart = x.imagePart - y.imagePart;public static void main(String arg) Complex c1 = new Complex(1, 2);Complex c2 = new Complex(2, 2);Complex c3 = new Complex();c1.show();c2.show();c3.add(c1, c2);System.out.print(add : );c3.show();c3.sub(c1, c2);System.out.print(sub : );c3.show(。