Java实现复数Complex的加减乘除运算、取模、求幅角角度

this.realPart = realPart;

this.imaginaryPart = imaginaryPart;

}

//属性的get、set方法

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;

}

//加法运算

public Complex add(Complex otherComplex){

if (otherComplex != null) {

return new Complex(this.getRealPart() + otherComplex.getRealPart(),this.getImaginaryPart() + otherComplex.getImaginaryPart());

}else throw new RuntimeException(“参与运算的对象为空!”);

}

//减法运算

public Complex decrease(Complex otherComplex){

if (otherComplex != null) {

return new Complex(this.getRealPart() - otherComplex.getRealPart(),this.getImaginaryPart() - otherComplex.getImaginaryPart());

}else throw new RuntimeException(“参与运算的对象为空!”);

}

//乘法运算

public Complex multiply(Complex otherComplex){

if (otherComplex != null) {

double newReal = this.getRealPart() * otherComplex.getRealPart() - this.getImaginaryPart() * otherComplex.getImaginaryPart();

double newImaginary = this.getImaginaryPart() * otherComplex.getRealPart() + this.getRealPart() * otherComplex.getImaginaryPart();

return new Complex(newReal,newImaginary);

}else throw new RuntimeException(“参与运算的对象为空!”);

}

//除法运算

public Complex divide(Complex otherComplex){

if (otherComplex != null) {

if (otherComplex.getRealPart() != 0 && otherComplex.getImaginaryPart() != 0){

double newReal = (this.getRealPart() * otherComplex.getRealPart() + this.getImaginaryPart()

必看视频!获取2024年最新Java开发全套学习资料 备注Java

  • otherComplex.getImaginaryPart()) / (otherComplex.getRealPart() * otherComplex.getRealPart() + otherComplex.getImaginaryPart() * otherComplex.getImaginaryPart());

double newImaginary = (this.getImaginaryPart() * otherComplex.getRealPart() - this.getRealPart() * otherComplex.getImaginaryPart()) / (otherComplex.getRealPart() * otherComplex.getRealPart() + otherComplex.getImaginaryPart() * otherComplex.getImaginaryPart());

return new Complex(newReal,newImaginary);

}else throw new RuntimeException(“除数不能为0!”);

}else throw new RuntimeException(“参与运算的对象为空!”);

}

//取模

public double delivery(){

return Math.sqrt(this.getRealPart() * this.getRealPart() + this.getImaginaryPart() * this.getImaginaryPart());

}

//幅度值(角度)

public double angle(){

double atan;

if (this.getRealPart() != 0) { //注意,该处double型变量若有进行其他操作,则不能以此方式判断其等于0,应该是其绝对值小于某个很小的数;而这当前情景下,其实精度问题并不影响,因此可以这样写

atan = Math.atan(this.getImaginaryPart() / this.getRealPart());

}else {

if (this.getImaginaryPart() > 0) {

atan = Math.PI / 2;

}else if (this.getImaginaryPart() < 0){

atan = -Math.PI / 2;

}else atan = Math.atan(0);

}

return atan;

}

}

//测试主类

public class ComplexTest {

public static void main(String[] args) {

Complex complex1 = new Complex(0, 5);

Complex complex2 = new Complex(3, -3);

//取模测试

double delivery = complex1.delivery();

System.out.println(“(” + complex1.getRealPart() + “+” + complex1.getImaginaryPart() + “i” + “)” + “的模为:” + delivery);

写在最后

学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!

最后再分享的一些BATJ等大厂20、21年的面试题,把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,上面只是以图片的形式给大家展示一部分。

蚂蚁金服三面直击面试官的Redis三连,Redis面试复习大纲在手,不慌

Mybatis面试专题

蚂蚁金服三面直击面试官的Redis三连,Redis面试复习大纲在手,不慌

MySQL面试专题

蚂蚁金服三面直击面试官的Redis三连,Redis面试复习大纲在手,不慌

并发编程面试专题

有限,上面只是以图片的形式给大家展示一部分。

[外链图片转存中…(img-67d59yyA-1716459301943)]

Mybatis面试专题

[外链图片转存中…(img-fXRTbLqs-1716459301944)]

MySQL面试专题

[外链图片转存中…(img-EQtwfucE-1716459301944)]

并发编程面试专题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值