วันพุธที่ 26 พฤศจิกายน พ.ศ. 2557

Lab 5 - Queue

void setup(){
  Patient[] P = {new Patient("Aruk Pupatee",80,false),new Patient("Tanaphat Kalaya",100,true),
  new Patient("Anuparp Chewakumnuan",20,true),new Patient("Apichit Saisuttivong",100,false)};
  PatientQueue PQ = new PatientQueue(P);
  PQ.showAllPatient();
  Patient P1 = new Patient("Anothai Arunthong",90,false);
  Patient P2 = new Patient("Warunatid Thongsalee",100,false);
  Patient P3 = new Patient("Chayanin Khawsanit",60,true);
  PQ.addPatient(P1);
  PQ.addPatient(P2);
  PQ.addPatient(P3);
  PQ.showAllPatient();
  PQ.removePatient();
  PQ.showAllPatient();
}
class PatientQueue{
  Patient[] HospitalPatient;
  PatientQueue(Patient p){
    this.HospitalPatient = new Patient[1];
    this.HospitalPatient[0] = p;
  }
  PatientQueue(Patient[] p){
    this.HospitalPatient = p;
    this.sortQueue();
  }
  void sortQueue(){
    Patient[] p = new Patient[this.HospitalPatient.length-this.getTotalEmer()];
    int pcount = 0;
    for(int i=0;i<this.HospitalPatient.length;i++){
      if(!this.HospitalPatient[i].getEmer()){
        p[pcount] = this.HospitalPatient[i];
        pcount++;
      }
    }
    for(int i=0;i<this.HospitalPatient.length;i++){
      for(int j=i;j<this.HospitalPatient.length;j++){
        if(!this.HospitalPatient[i].getEmer() && this.HospitalPatient[j].getEmer()){
          Patient temp = this.HospitalPatient[i];
          this.HospitalPatient[i] = this.HospitalPatient[j];
          this.HospitalPatient[j] = temp;
        }
        else {
          if(this.HospitalPatient[i].getEmer() && this.HospitalPatient[j].getEmer()){
            if(this.HospitalPatient[i].getBlood() > this.HospitalPatient[j].getBlood()){
              Patient temp = this.HospitalPatient[i];
              this.HospitalPatient[i] = this.HospitalPatient[j];
              this.HospitalPatient[j] = temp;
            }
          }
        }
      }
    }
    for(int i=(this.HospitalPatient.length-1);i>(this.getTotalEmer()-1);i--){
      HospitalPatient[i] = p[pcount-1];
      pcount--;
    }
  }
  void addPatient(Patient p){
    Patient[] temp = new Patient[this.HospitalPatient.length+1];
    for(int i=0;i<this.HospitalPatient.length;i++){
      temp[i] = this.HospitalPatient[i];
    }
    temp[temp.length-1] = p;
    this.HospitalPatient = temp;
    this.sortQueue();
  }
  void removePatient(){
    Patient[] temp = new Patient[this.HospitalPatient.length-1];
    for(int i=0;i<(this.HospitalPatient.length-1);i++){
      temp[i] = this.HospitalPatient[i+1];
    }
    this.HospitalPatient = temp;
  }
  void showAllPatient(){
    for(int i=0;i<this.HospitalPatient.length;i++){
      print((i+1)+")."+this.HospitalPatient[i].getName());
      print(" Blood : "+this.HospitalPatient[i].getBlood()+"%");
      if(this.HospitalPatient[i].getEmer()){
        println(" <<< EMERGENCY !!");
      }
      else println("");
    }
    println("----------------------------------------------------");
  }
  int getTotalEmer(){
    int count=0;
    for(int i=0;i<this.HospitalPatient.length;i++){
      if(this.HospitalPatient[i].getEmer()){
        count++;
      }
    }
    return count;
  }
}
class Patient{
  String name;
  int blood;
  boolean isEmergency;
  Patient(String n,int b,boolean e){
    this.name = n;
    if(b>100){
      this.blood = 100;
    }
    else this.blood = b;
    this.isEmergency = e;
  }
  String getName(){
    return this.name;
  }
  int getBlood(){
    return this.blood;
  }
  boolean getEmer(){
    return this.isEmergency;
  }
}

วันอาทิตย์ที่ 23 พฤศจิกายน พ.ศ. 2557

Lab 5 - class House

void setup(){
  size(500,500);
  background(255);
  House h = new House(100,100,150,150);
  h.display();
}
class House{
  int x,y,w,h;
  Door door;
  Window left_window;
  Window right_window;
  Roof roof;
  House(int x,int y,int w,int h){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.door = new Door(this.x+((this.w*2)/5),this.y+(this.h/2),this.w/5,this.h/2);
    this.left_window = new Window(this.x+(this.w/7),this.y+(this.h/2),this.w/7,this.h/5);
    this.right_window = new Window(this.x+((this.w*5)/7),this.y+(this.h/2),this.w/7,this.h/5);
    this.roof = new Roof(this.x,this.y,this.w,this.h/2);
  }
  void display(){
    rect(this.x,this.y,this.w,this.h);
    this.door.display();
    this.left_window.display();
    this.right_window.display();
    this.roof.display();
  }
}
class Door{
  int x,y,w,h;
  Door(int x,int y,int w,int h){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }
  void display(){
    rect(this.x,this.y,this.w,this.h);
  }
}
class Window{
  int x,y,w,h;
  Window(int x,int y,int w,int h){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }
  void display(){
    rect(this.x,this.y,this.w,this.h);
    line(this.x+(this.w/2),this.y,this.x+(this.w/2),this.y+this.h);
    line(this.x,this.y+(this.h/2),this.x+this.w,this.y+(this.h/2));
  }
}
class Roof{
  int x,y,w,h;
  Roof(int x,int y,int w,int h){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }
  void display(){
    triangle(this.x,this.y,this.x+(this.w/2),this.y-this.h,this.x+this.w,this.y);
  }
}

วันอาทิตย์ที่ 2 พฤศจิกายน พ.ศ. 2557

Lab 5 - class Matrix

void setup(){
  int[][] a = {{-1,2,1},
               {3,-2,4},
               {2,-3,1}};
  int[][] b = {{7,4,-2},
               {-1,-5,1},
               {-3,4,6}};
  Matrix m = new Matrix(a);
  Matrix n = new Matrix(b);
  m.ChangeValueAt(4,2,2);
  m.ShowMatrix();
  m.AddMatrix(n);
  m.ShowMatrix();
  n.SubMatrix(m);
  n.ShowMatrix();
}

class Matrix{
  int[][] matrix;
  int max_row;
  int max_column;
  Matrix(int r,int c){
    matrix = new int[r][c];
    this.max_row = this.matrix.length;
    this.max_column = this.matrix[0].length;
  }
  Matrix(int[][] a){
    this.matrix = a;
    this.max_row = this.matrix.length;
    this.max_column = this.matrix[0].length;
  }
  void ChangeMatrix(int[][] a){
    this.matrix = a;
    this.max_row = this.matrix.length;
    this.max_column = this.matrix[0].length;
  }
  void ChangeValueAt(int value,int r,int c){
    this.matrix[r-1][c-1] = value;
  }
  void ShowMatrix(){
    for(int r=0;r<this.max_row;r++){
      print("|");
      for(int c=0;c<this.max_column;c++){
        if(this.matrix[r][c]>=0){
          print("  "+this.matrix[r][c]+" ");
        }
        else print(" "+this.matrix[r][c]+" ");
      }
      println("|");
    }
    println("");
  }
  void AddMatrix(Matrix x){
    if((this.max_row==x.max_row)&&(this.max_column==x.max_column)){
      for(int r=0;r<this.max_row;r++){
        for(int c=0;c<this.max_column;c++){
          this.matrix[r][c] = this.matrix[r][c] + x.matrix[r][c];
        }
      }
    }
    else println("Can't add matrix");
  }
  void SubMatrix(Matrix x){
    if((this.max_row==x.max_row)&&(this.max_column==x.max_column)){
      for(int r=0;r<this.max_row;r++){
        for(int c=0;c<this.max_column;c++){
          this.matrix[r][c] = this.matrix[r][c] - x.matrix[r][c];
        }
      }
    }
    else println("Can't subtract matrix");
  }
  /*
  int FindDet(){
    int det=0;
    if((this.max_row==this.max_column) ){
      if(this.max_row==2){
        det = (this.matrix[0][0]*this.matrix[1][1])-(this.matrix[1][0]*this.matrix[0][1]);
      }
      else {
        if(this.max_row==3){
          for(int c=0;c<this.matrix[0].length;c++){
            for(int r=c
          }
        }
        else {
          println("Can't find determinant");
        }
      }
    }
    else println("Can't find determinant");
    return det;
  }
  */
}