java 设计一个复数类

来源:百度知道 编辑:UC知道 时间:2024/06/14 08:24:08
定义一个表示复数的类,要求
1.具有实部、虚部等属性
2.类中有一个构造方法(参数为复数的实部和虚部)
3.类中有成员方法,完成负数加 、减以及显示等功能。

哪位大虾 帮帮小弟我呀

不知道是不是 ~
  //复数类。

  public class Complex
  {
  private double real,im; //实部,虚部

  public Complex(double real, double im) //构造方法
  {
  this.real = real;
  this.im = im;
  }

  public Complex(double real) //构造方法重载
  {
  this(real,0);
  }

  public Complex()
  {
  this(0,0);
  }

  public Complex(Complex c) //拷贝构造方法
  {
  this(c.real,c.im);
  }

  public boolean equals(Complex c) //比较两个对象是否相等
  {
  return this.real==c.real && this.im==c.im;
  }

  public String toString()
  {
  return "("+this.real+"+"+this.im+"i)";
  }

  public void add(Complex c) //两个对象相加
  { //改变当前对象,没有返回新对象
  this.real += c.real;
  this.im += c.im;
  }

  public Complex plus(