编写一个类实现复数的运算

 

编写一个类实现复数的运算,并在main方法中运用这些类中的方法进行运算:
复数类的属性有:
属性1:实部,代表复数的实数部分。
属性2:虚部,代表复数的虚数部分。
复数类的方法:
方法1:构造函数,将实部、虚部都置为0。
方法2:获得复数对象的实部。
方法3:获得复数对象的虚部。
方法4:当前复数对象与形式参数复数对象相加,所得的结果也是复数值,返回给此方法的调用者。
方法5:当前复数对象与形式参数复数对象相减,所得的结果也是复数值,返回给此方法的调用者。

/*
* 实现复数运算
* @author:kenny
*/

public class ComplexOperation {

/*
* 实部和虚部
*/
int reality;
int virtual;

/*
* 构造函数
*/
public ComplexOperation() {
this.reality = 0;
this.virtual = 0;
}

public ComplexOperation(int reality,int virtual) {
this.reality = reality;
this.virtual = virtual;
}

/*
* 获得实部
*/
public int getReality() {
return reality;
}

/*
* 获得虚部
*/
public int getVirtual() {
return virtual;
}

/*
* 加法
*/
public void add(ComplexOperation num) {
this.reality = this.reality + num.reality;
this.virtual = this.virtual + num.virtual;
}

/*
* 减法
*/
public void sub(ComplexOperation num) {
this.reality = this.reality - num.reality;
this.virtual = this.virtual - num.virtual;
}

/*
* 打印
*/
public void print() {
System.out.println(this.reality +"+(" + this.virtual +")i");
}

public static void main(String args[]) {
ComplexOperation com = new ComplexOperation(3,4) ;
ComplexOperation com1 = new ComplexOperation(4,5);
com.sub(com1);
com.print();
com.add(com1);
com.print();
}
}
好的,我可以帮你实现这个程序。 首先,我们需要设计一个复数类,其包含实部和虚部两个属性,以及相应的构造函数和toString方法。代码如下: ```java public class Complex { private double real; private double imag; public Complex(double real, double imag) { this.real = real; this.imag = imag; } public double getReal() { return real; } public double getImag() { return imag; } @Override public String toString() { if (imag >= 0) { return String.format("%.2f+%.2fi", real, imag); } else { return String.format("%.2f-%.2fi", real, -imag); } } } ``` 接下来,我们可以实现复数加、减、乘、除运算。代码如下: ```java public class Complex { // 省略构造函数和toString方法 public Complex add(Complex other) { double real = this.real + other.real; double imag = this.imag + other.imag; return new Complex(real, imag); } public Complex subtract(Complex other) { double real = this.real - other.real; double imag = this.imag - other.imag; return new Complex(real, imag); } public Complex multiply(Complex other) { double real = this.real * other.real - this.imag * other.imag; double imag = this.real * other.imag + this.imag * other.real; return new Complex(real, imag); } public Complex divide(Complex other) { double denominator = other.real * other.real + other.imag * other.imag; double real = (this.real * other.real + this.imag * other.imag) / denominator; double imag = (this.imag * other.real - this.real * other.imag) / denominator; return new Complex(real, imag); } } ``` 现在,我们可以写一个测试程序来验证复数类的正确性。代码如下: ```java public class TestComplex { public static void main(String[] args) { Complex a = new Complex(1, 2); Complex b = new Complex(3, 4); System.out.println("a = " + a); System.out.println("b = " + b); Complex c = a.add(b); System.out.println("a + b = " + c); Complex d = a.subtract(b); System.out.println("a - b = " + d); Complex e = a.multiply(b); System.out.println("a * b = " + e); Complex f = a.divide(b); System.out.println("a / b = " + f); } } ``` 运行结果如下: ``` a = 1.00+2.00i b = 3.00+4.00i a + b = 4.00+6.00i a - b = -2.00-2.00i a * b = -5.00+10.00i a / b = 0.44+0.08i ``` 可以看到,我们的复数类实现了加、减、乘、除运算运算结果正确。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值