去宇宙摘星星℡ 2021-11-20 10:57 采纳率: 100%
浏览 72
已结题

关于#java#的问题:为以下操作提供 public方法:●两个 Complex数相加:实部与实部相加,虚部与虚部相加●两个 Complex数相减:实部与实部相减,虚部与虚部相减

创建一个
omplexNumber
类做复数运算,复数具有如下格式:
real+imaginary*i。其中,i为-1的平方根。提供一个构造函数,以
便在此声明此类的对象时,对其进行初始化。为以下操作提供 public
方法:
●两个 Complex数相加:实部与实部相加,虚部与虚部相加
●两个 Complex数相减:实部与实部相减,虚部与虚部相减

  • 写回答

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();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月20日
  • 已采纳回答 12月12日
  • 创建了问题 11月20日

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?