วันพฤหัสบดีที่ 19 กันยายน พ.ศ. 2556

Class Fraction




void setup() {
  size(200, 200);
  background(2);
  Fraction a=new Fraction(13, 20);
  Fraction f=new Fraction(2, 2);
  Fraction g=new Fraction(6, 3);
  Fraction b=new Fraction(8, 9);
  Fraction h=new Fraction(5, 6);
  f.plus(g);
  b.divide(2);
  h.mul(2);
  g.minus(5);
  a.add(3);
  textSize(20);
  text("Add = "+a.toString(), 20, 30);
  text("minus = "+g.toString(), 20, 60);
  text("mul = "+h.toString(), 20, 90);
  text("Divide = "+b.toString(), 20, 120);
  text("Plus = "+f.toString(), 20, 150);
}

class Fraction {
  int n;
  int d;
  Fraction(int a, int b) {
    this.n=a;
    this.d=b;
  }
  void add(int z ) {
    this.n=this.n+this.d*z;
  }
  void minus(int z) {
    this.n=this.n-this.d*z;
  }
  void mul(int s) {
    this.n=this.n*s;
  }
  void divide(int d) {
    this.d=this.d*d;
  }
  void plus(Fraction k) {
    this.n=this.n*k.d+this.d*k.n;
    this.d=this.d*k.d;
  }

  String toString() {
    String s=this.n+"/"+this.d;
    return s;
  }
}

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

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