创建一个
omplexNumber
类做复数运算,复数具有如下格式:
real+imaginary*i。其中,i为-1的平方根。提供一个构造函数,以
便在此声明此类的对象时,对其进行初始化。为以下操作提供 public
方法:
●两个 Complex数相加:实部与实部相加,虚部与虚部相加
●两个 Complex数相减:实部与实部相减,虚部与虚部相减
关于#java#的问题:为以下操作提供 public方法:●两个 Complex数相加:实部与实部相加,虚部与虚部相加●两个 Complex数相减:实部与实部相减,虚部与虚部相减
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
技术专家团-Bamboo 2021-11-20 11:18关注class ComplexNumber{ private double real; private double imaginary; public ComplexNumber(){ } public ComplexNumber(double real,double imaginary){ this.real = real; this.imaginary = imaginary; } public ComplexNumber add(ComplexNumber b){ ComplexNumber c = new ComplexNumber(); c.real = this.real + b.real; c.imaginary = this.imaginary + b.imaginary; return c; } public ComplexNumber subtract(ComplexNumber b){ ComplexNumber c = new ComplexNumber(); c.real = this.real - b.real; c.imaginary = this.imaginary - b.imaginary; return c; } public void show(){ System.out.print(this.real); if(this.imaginary>=0) System.out.print("+"); System.out.print(this.imaginary+"i\n"); } } public class TestMain { public static void main(String[] args) { ComplexNumber a = new ComplexNumber(1.1,2.5); System.out.print("a="); a.show(); ComplexNumber b = new ComplexNumber(2.1,-1.5); System.out.print("b="); b.show(); System.out.print("相加:"); ComplexNumber c = a.add(b); c.show(); System.out.print("相减:"); ComplexNumber d = a.subtract(b); d.show(); } }本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用