วันอังคารที่ 25 พฤศจิกายน พ.ศ. 2557

Lab.5 Class complex

class Complex {
  int a;
  int b;
  Complex(int a, int b) {
    this.a= a;
    this.b= b;
  }
  Complex(int a) {
    this.a= a;
  }
  Complex add(Complex d) {
    int r; // result
    int i;
    r= this.a+d.a;
    i= this.b+d.b ;
    Complex z= new Complex(r, i);
    return z;
  }
  Complex reduce(Complex d) {
    int r;
    int i;
    r= this.a-d.a;
    i=this.b-d.b;
    Complex z=new Complex(r, i);
    return z;
  }
  String toString() {
    String s;
    if (this.b == 0) {
      s = this.a + "";
    } else if (this.b > 0) {
      s = this.a + "+" + this.b +"i";
    } else {
      s = this.a + "" + this.b +"i";
    }
    return s;
  }
}

void setup() {
  Complex z, y, x, w, v;
  z=new Complex(5, 3);
  y=new Complex(7);
  w=new Complex(6, 7);
  x=z.add(y);
  println(x);
  v=x.reduce(w);
  println(v);
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น