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

fraction

class Fraction {
  int frac;//fraction
  int sec;//section
  Fraction( int fraction, int section) {// fraction/section
    this.frac = fraction;
    this.sec = section;
  }
  Fraction( int fraction) {// fraction/1
    this.frac = fraction;
    this.sec = 0;
  }
  void display() {
    println( this.frac + "/" + this.sec);
  }

   void plus (Fraction b) {
     int frac;
     int sec;
    if (this.sec == b.sec) {
      frac = this.frac + b.frac;
      sec = this.sec;
    } else {
      top = (this.frac * b.sec) + (b.frac * this.sec);
      sec = this.sec * b.sec;
    }
  }

  void minus(Fraction b) {
    int frac;
    int sec;
    if (this.sec == b.sec) {
      frac = this.frac - b.frac)
      sec = this.sec ;
    } else {
      frac = (this.frac * b.sec) - (b.frac * this.sec);
      sec = this.sec * b.sec;
    }
  }

  void multiply(Fraction b) {
    int frac;
    int sec;
    top = this.frac * b.frac
    sec = this.sec * b.sec
  }

  void divide(Fraction b) {
    println(this.frac * b.sec+ "/" + this.sec * b.frac);
  }
}


void setup() {

  Fraction a; // Create object
  Fraction b;

  a = new Fraction(1, 2); // 1/2
  b = new Fraction(2, 3);
  a.plus(b); // a + b
  a.minus(b); // a - b
  a.multiply(b); // a * b
  a.divide(b); // a / b
}

Lab.5 Class employee

class Employee {
  String name;
  int age;
  String position;
  float salary;
  String address;
  Employee(String name) {
    this.name = name;
  }
  void age(int Age) {
    this.age =  Age;
  }
  void position(String Position) {
    this.position = Position;
  }
  void salary(float Salary) {
    this.salary = Salary;
  }
  void address(String Address) {
    this.address = Address;
  }
  void printEmp_data() {
    println("Name     : " + this.name );
    println("Age      : " + this.age );
    println("Position : " + this.position);
    println("Address  : " + this.address);
    println("Salary   : " + this.salary + " Baht");
  }
 
}

void setup() {

  Employee number1 = new Employee ("B");
  number1.age(25);
  number1.position("Engineer");
  number1.address("");
  number1.salary(20000);
  number1.printEmp_data();
}

Lab.5 Class balloon

int R ;
int G ;
int B ;
class Balloon {
  float x;
  float y;
  int r_x;
  int r_y;
  float moveX;
  float moveY;

  Balloon(float x, float y, int r_x, int r_y, float moveX, float moveY) {
    this.x = x;
    this.y = y;
    this.r_x = r_x;
    this.r_y = r_y;
    this.moveX = moveX;
    this.moveY = moveY;
  }
  void display () {
    ellipse(this.x, this.y, this.r_x, this.r_y);
    triangle(this.x - (this.r_x/5), this.y + (2*(this.r_y/3)), this.x, this.y + (this.r_y/2), this.x + (this.r_x/5), this.y + (2*(this.r_y/3)));
    line(this.x, this.y + (this.r_y/2), this.x, this.y + 1.5*this.r_y);
 
}
  void move () {
    this.y = this.y - this.moveY;
    //this.x = this.x + this.moveX;
  }
}
Balloon []a = new Balloon[3];
void setup () {
  size(400, 300);
  a[0] = new Balloon(100, 250, 50, 70, 0.5, 1);
  a[1] = new Balloon(150, 300, 40, 60, -0.5, 1);
  a[2] = new Balloon(200, 200, 60, 80, 1, 2);
}
void draw () {
  background(255);
  for (Balloon i : a) {
    i.display();
    i.move();
  }
}

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);
}