วันอาทิตย์ที่ 25 สิงหาคม พ.ศ. 2556

=if-else=(medium)

Programming Exercise
Proposition =>   Write a program to calculate the slope if x1=x2 (Slope undefined)
Translated into Thailand => เขียนโปรแกรมที่ใช้คำนวนหาค่าความชัน เมื่อ x1=x2 ให้แสดงค่า(Slope undefined)


Solution=> 
float m;
float x1=2, y1=5, x2=6, y2=8;   //ประกาศตัวแปลชนิดจำนวนทศนิยม
void setup() {
  if (x1==x2) {
    println("Slope undefined.");
  }
  else {
    m=(y2-y1)/(x2-x1);    //ใช้ในการคำนวนหาค่าความชัน
    println(m);            //แสดงข้อความออกมาทางหน้าจอ
  }
}

You can run code in http://processingjs.org/tools/processing-helper.html



text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
              ISBN 978-0-7637-3402-2

=if elae=(eazy)

Programming Exercise
Proposition => Given the declarations
   boolean leftPage;
   int pageNumber =.....;
 write a statement that sets leftPage to true is pageNumber is even.

Translated into Thailand => เขียนคำสั่งที่กำหนด leftPage เป็นจริงเมื่อ PageNumber เป็นเลขคู่

Solution=> 
 boolean leftPage()     //เป็นการส้รางฟังก์ชันโดยกำหนดให้มีการ return ค่ากลับเป็น boolean(true-false)
{
  int pageNumber=10;
  if (pageNumber%2==0) {
    return true;
  }
  else {
    return false;
  }
}
void setup() {
  println(leftPage());
}



Out put => true(ถ้า pageNumber เป็นเลขคู่)
                 false(ถ้า pageNumber เป็นเลขคคี่)

You can run code in http://processingjs.org/tools/processing-helper.html


text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
              ISBN 978-0-7637-3402-2

=if-else=(Hard)

Programming Exercise
Proposition => For employees who work more than 40 hours per week, calculate overtime pay and add it to their regular pay
  Regular pay = 20$/hours
  Overtime wages = 25$/hours

Translated into Thailand => สำหรับพนักงานที่ทำงานมากกว่า 40 ชั่วโมงต่อสัปดาห์ ให้คำนวณค่านอกเวลาและรวมกับค่าจ้างปกติของพวกเขา
 ค่าจ้างเวลาปกติ = 20$/ชั่วโมง
 ค่าจ้างนอกเวลา =  25$/ชั่วโมง
Solution=>  
  int work= 45;
  int regular=0;
  int over=0;
  int stan=40;
  int sum;
  if(work>stan){                     //เมื่อเงื่อนไขwork>stanเป็นจริงจะทำตามคำสั่งด้านใน
    over=(work-stan)*25;      //คำนวนหาค่าจ้างที่เกินเวลามา
    regular=stan*20;              //คำนวนหาเงินค้าจ้างในเวลาปกติ
  }
  else{
    regular=work*20;
  }
  sum=regular+over;
  println("Wage="+sum+"$");

Out put => Wage = 925 $

You can run code in http://processingjs.org/tools/processing-helper.html


text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
              ISBN 978-0-7637-3402-2

วันเสาร์ที่ 24 สิงหาคม พ.ศ. 2556

-Othello-




float[][] Color = new float[8][8]; //สร้างตัวแปรอาเรย์ 2 มิติ ชนิดจำนวนเต็ม 
และกำหนดจำนวนช่องของindex int[][] Xpos = new int[8][8]; void setup() { size(480, 480); background(0, 148, 22); int x = 30, i = 0, c=0; while (c<Xpos.length) { //เป็นลูปที่ใช้ในการรันอะเรย์ในมิติที่1 while (i<Xpos.length) {  //เป็นลูปที่ใช้ในการรันอะเรย์ในมิติที่2 
เมื่อออกจากลูปนี้ก็จะกลับไปวนลูปแรกใหม่  
เป็นการเขียนแบบ ลูปซ้อนลูป Xpos[c][i] = x; //เป็นการเก็บค่าในพิกัดแกนxของแต่ละตัว Color[c][i] = random(0, 2); //เป็นการเก็บตัวเลขของแต่ละตัวซึ่งจะนำไปใช้แบ่งเป็นสีต่อไป x=x+60; i++; } x=30; c++; i=0; } } void draw() { drawTable(); //เรียกใช้ฟังก์ชันdrawTable() int j = 0, y = 30, c=0; while (j<Xpos.length) { while (c<Xpos.length) { drawCircle(Xpos[j][c], y,Color[j][c]); //เรียกใช้ฟังก์ชันdrawCircle()โดยมีการส่งค่าไป3ค่า c++; } j++; y=y+60; c=0; } } void drawTable() { int x=60; while (x<=420) { line(x, 0, x, 480); line(0, x, 480, x); x=x+60; } } void drawCircle(int X, int y,float Color) { //ใช้วาดตัวหมากโดยมีparameter X,y Color int x=30; if (Color<1) { //ใช้กำหนดสีของตัวหมาก fill(255); } else if (Color>1) { fill(0); } smooth(); noStroke(); ellipse(X, y, 45, 45); }





//อ้างอิงคำสั่งพื้นฐานต่างๆจาก lab1 http://com5630043.blogspot.com/search/label/lab1
//อ้างอิงอธิบายฟังก์ชันจาก lab2 http://com5630043.blogspot.com/search/label/lab2

//อ้างอิงอธิบายเรื่องif-elseจาก lab3 http://com5630043.blogspot.com/search/label/lab3%20%3A%20Condition



วันอังคารที่ 20 สิงหาคม พ.ศ. 2556

=Variable= (Hard)

Programming Exercise
Proposition => Write an application to calculate the diameter,circumference and area of a circle with a radius input by user.Assign the radius to a float variable.Declare a named constant PI with the value 3.14159. The application should output the diameter,circumference and area,each on a separate line

Translated into Thailand =>
เขียนโปรแกรมเพื่อคำนวณขนาดเส้นผ่าศูนย์กลาง,เส้นรอบวงและพื้นที่ของวงกลมที่มีรัศมีการป้อนข้อมูลโดยผู้ใช้ โดยรัศมีเป็นตัวแปรทศนิยม  และประกาศ PI มีค่าเท่ากับ 3.14159 โปรแกรมควรจะแสดงผลเส้นผ่าศูนย์กลางมเส้นรอบวงและพื้นที่ให้อยู๋ในแต่ละบรรทัดที่แยกต่างหาก


Solution=> 
               float radius=4;
               float PI=3.14159;
               float d=radius*2;
               float c=2*PI*radius;
               float a=PI*radius*radius;
               println("Diameter is "+d);
               println("Circumference is "+c);

               println("Area is "+a);

Out put =>  
Diameter is 8
Circumference is 25.13272
Area is 50.26544

You can run code in  http://processingjs.org/tools/processing-helper.html

อธิบายการทำงานของโปรแกรม =>
     เป็นโปรแกรมที่ใช้หาเส้นผ่านศูนย์กลาง เส้นรอบวง และพื้นที่ของวงกลม โดยเราจะประกาศตัวแปลชื่อ radius มาเก็บค่าที่เราจะป้อนเข้าไปเมื่อกดรันค่า radius ก็จะถูกส่งไปคำนวนในสมการต่างๆ
และทำการปริ้นค่าออกมาเพื่อแสดงผลโดย1คำสั่งprintln จะได้1แถว ถ้าต้องการให้แยกแถวกันจิงต้องใช้คำสั่ง้ println 3ครั้ง



text book name :Programming and problem solving with Java – 2nd edition
Author : Nell Dale and Chip Weems
              ISBN 978-0-7637-3402-2

-XO-




void setup() {
  size(300, 300);
  background(0);
}
int x=100;           
int[][] ox= {{1, 0, 1},{1, 0, 1}, {0, 1, 0}};    //สร้างตัวแปรอาเรย์ 2 มิติ ชนิดจำนวนเต็ม และกำหนดค่า
void draw() {
  drawTa();                                 //เรียกใช้ฟังก์ชัน drawTa( )
  drawOX();           
  
}
void drawTa() {               //ฟังก์ชันนี้ใช้ในการวาดตาราง
  while (x<=200) {           //เป็นคำสั่งที่ใช้ในการวนลูป(ทำงานซ้ำ)เมื่อเงื่อนไขเป็นจริง
    stroke(250);
    strokeWeight(10);
    line(x, 20, x, 280);
    line(20, x, 280, x);
    x=x+100;                   //ให้เพิ่มค่าx 100ในทุกครั้งที่มีการวนลูป
  }
}
int i=0; 
int s;
int y;
void drawOX() {         //เป็นฟังก์ชันที่ใช้ในการวาด O และ X
  int c=0;
  strokeWeight(5);
  if (i<ox.length) {              
    while (c<ox[i].length) {
      if (ox[i][c]==1) {            //ถ้าเงื่อนไขเป็นจริง จะทำตามคำสั่งด้านใน
        drawX();                      //เรียกใช้ฟังก์ชัน drawX
      }
      else{
       drawO();                     //เรียกใช้ฟังก์ชัน drawO
      }
      c=c+1;
      s=s+100;
    }
    if (i<ox.length) {
      if (c==3) {
        i=i+1;
        s=0;
        y=y+100;
      }
    }
  }
}
void drawX() {        //ฟังก์ขันที่ใช้ในการสร้างรูป X
  stroke(0,255,0);
  line(20+s, 20+y, 80+s, 80+y); 
  line(80+s, 20+y, 20+s, 80+y);
}
void drawO() {        //ฟังก์ขันที่ใช้ในการสร้างรูป O
  noFill();
  stroke(0,0,255);
  ellipse(50+s, 50+y, 70, 70);
}

///////////Option 2//////////


void setup() {
 size(300, 300);
 background(0);
}
int x=100;
int[][] ox= {{1, 0, 1},{1, 0, 1}, {0, 1, 0}}; //สร้างตัวแปรอาเรย์ 2 มิติ ชนิดจำนวนเต็ม และกำหนดค่า
void draw() {
 drawTa();  //เรียกใช้ฟังก์ชัน drawTa( )
 drawOX();
}
void drawTa() { //ฟังก์ชันนี้ใช้ในการวาดตาราง
 while (x<=200) {
   stroke(255, 0, 0);
   strokeWeight(10);
   line(x, 20, x, 280);
   line(20, x, 280, x);
   x=x+100;
 }
}
int c=0;
int i=0;
int s=0;
int y=0;
void drawOX() {  //เป็นฟังก์ชันที่ใช้ในการวาด O และ X
 while (i<ox.length) { //ลูปนี้จะใช้เช็คเงื่อนไขของindexมิติที่1
โดยมีติที่ 1 เก็บค่าของจำนวนแถว
  while(c<ox[i].length){ //ลูปนี้จะใช้เช็คเงื่อนไขของindexมิติที่1 
โดยมีติที่ 2 เก็บค่าว่าตัวไหนเป็นXหรือO
เป็นการเขียนแบบ ลูปซ้อนลูป
    if(ox[i][c]==1){ //ถ้าเงื่อนไขเป้นจริงวาด X
     drawX();
    }
    else if(ox[i][c]==0){ //ถ้าเงื่อนไขเป็นจริงวาด O
     drawO();
    }
    c=c+1; //เพิ่มค่าCเพื่อให้ออกจากลูป
    s=s+100; //เพิ่มค่าSเพื่อให้รูปไม่ทับกัน
   }
   s=0; //เป็นการรีเซ็ตค่าใหม่เพิ่มเตรียมวาดในแถวที่2
   c=0;
   y=y+100; //เพิ่มค่าyเพื่อเป็นพิกัดในแกนy
   i=i+1; //เพิ่มค่าiทุกๆครั้งที่มีการวนลูป
   
 }
}


void drawX() {
 stroke(0,255,0);
 strokeWeight(5);
 line(20+s, 20+y, 80+s, 80+y);
 line(80+s, 20+y, 20+s, 80+y);
}
void drawO() {
 noFill();
 stroke(0,0,255);
 ellipse(50+s, 50+y, 70, 70);
}

//อ้างอิงคำสั่งพื้นฐานต่างๆจาก lab1 http://com5630043.blogspot.com/search/label/lab1
//อ้างอิงอธิบายฟังก์ชันจาก lab2 http://com5630043.blogspot.com/search/label/lab2

//อ้างอิงอธิบายเรื่องif-elseจาก lab3 http://com5630043.blogspot.com/search/label/lab3%20%3A%20Condition

วันพุธที่ 7 สิงหาคม พ.ศ. 2556

-Barchart-




จังหวัดที่มีรถมอเตอร์ไซค์น้อยที่สุด 3 ลำดับ พ.ศ. 2552 - 2554

void setup() {
  size(210, 240);
  background(0);
  Barchart();
  maxmin();
}
float[][]stat= {           //สร้างตัวแปรอาเรย์2มิติ ชนิดจำนวนทศนิยม และกำหนดค่า
  {
    28.93, 34.84, 36.23
  }
  , {
    29.78, 34.37, 35.85
  }
  , {
    7.92, 43.7, 48.38
  }
};
String[][] j= {               //สร้างตัวแปรอาเรย์2มิติ ชนิดข้อความ และกำหนดค่า
  {
    "MaeHongSon", "SamutSongkhram", "Ranong"
  }
  , {
    "MaeHongSon", "SamutSongkhram", "Nonthaburi"
  }
  , {
    "Kna", "MaeHongSon", "SamutSongkhram"
  }
};
String[] year= {
  "2552", "2553", "2554"
};
int i=0;
int count=0;
void Barchart() {     //เป็นฟังก์ชันที่ใช้ในกาวาดBarchart
  int y=30;
  float avg=0;             //ตัวแปลที่ใช้ในการเก็บค่าเฉลี่ย
  while (count<=2) {         //กำหนดเงื่อนไขในกาวนลูป ใช้ในกาวาดรูปBarchart
    avg=(avg+stat[i][count]/stat[i].length);       //ใช้ในการคำนวนหาค่าเฉลี่ย โดยค่าที่นำมาคิดจะถูกเปลี่ยน                                                                 ไปทุกๆครั้งที่มีการวนลูป สังเกตได้จากตัวแปล i ที่อยู๋ใน[ ]
    rect(0, y, stat[i][count]*2, 10);
    text(j[i][count], 100, y+10);        
    y=y+10;
    count=count+1;                        
    if (count==3) {                              //โดยภายในเงื่อนไขนี้จะมีคำสั่งที่ใช้ในการวาด
                                                         Barchartขอค่าเฉลี่ย
      text("Year "+year[i], 10, y-30);         //แสดงข้อความออกมาที่Canvas
      fill(255, 0, 0);
      rect(0, y, (avg/3)*2, 5);
      fill(255);
      y=y+20;
      i=i+1;
      count=0;
      fill(255, 0, 0);
      text("average= "+avg+"%", 5, 190);  
      fill(255);
      avg=0;                                  //รีเซ็ตค่าให้เป็น0เพื่อนำกลับไปใช้คำนวนของปีต่อไปได้
      if (i==3) {
        break;
      }
    }
  }
}
void maxmin() {           //เป็นฟังก์ชันที่ใช้ในการหาค่า Max Min
  i=0;
  float max=stat[0][0];          //ตัวแปลที่ใช้ในการเก็บค่าMax
  float min=stat[0][0];            //ตัวแปลที่ใช้ในการเก็บค่าMax
  while (count<=2) {             //ลูปที่ใช้ในการวนหาค่ามาที่สุดและน้อยที่สุด
    if (max<stat[i][count]) {        //ใช้ในการเปลี่ยบเทียบค่าว่าค่าไหนมีค่ามากกว่ากัน
                                                ถ้าค่าstatมีค่ามากกว่าค่า max 
      max=stat[i][count];             ค่า maxก็จะเท่ากับค่าstatทันทีตามคำสั่งที่เขียนไว้
    }
    if (min>stat[i][count]) {          //ใช้ในการเปลี่ยบเทียบค่าว่าค่าในน้อยกว่ากัน
      min=stat[i][count];
    }
    count=count+1;
    if (count==3) {
      i=i+1;
      count=0;
      if (i==3) {
        break;
      }
    }
  }
  fill(0, 255, 0);
  text("max is SamutSongkhram= "+max+"%", 5, 210); //แสดงข้อความออกมาทางCanvas
  text("min is Kan="+min+"%", 10, 230);
}
//อ้างอิงคำสั่งพื้นฐานต่างๆจาก lab1 http://com5630043.blogspot.com/search/label/lab1
//อ้างอิงอธิบายฟังก์ชันจาก lab2 http://com5630043.blogspot.com/search/label/lab2

//อ้างอิงอธิบายเรื่องif-elseจาก lab3 http://com5630043.blogspot.com/search/label/lab3%20%3A%20Condition

วันอังคารที่ 6 สิงหาคม พ.ศ. 2556

-Solar System-



void setup(){
size(500,300);
background(0);
}
int y=150;
int x=0;
int r=1;
int[][] elements={{-140,70,90,120,160,210,270,330,380},{400,5,10,15,13,35,35,25,25}};
                           //สร้างตัวแปรอาเรย์2มิติ ชนิดจำนวนเต็ม และกำหนดค่า
String[] planet={"Sun","Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune"};
int[] moon={0,0,0,1,2,66,22,27,13};
void draw(){
 smooth();
 while(x<planet.length){      //วนลูปเพื่อใช้ในการวาดดาว
   drawP(x,r);               //เรียกใช้ฟังชัน drawP โดยมีการส่งค่าไปยังฟังก์ชัน drawP
  if(x%2==0){            //เงื่อนไขที่ใช้ในการ เขียนข้อความกำกับในดาวแต่ละดวง
  text(planet[x],elements[0][x],y-50);
  }
  else{
  text(planet[x],elements[0][x]-5,y+50);
  }
  drawmoon();           //เรียกใช้ฟังก์ชัน drawmoon()
  x=x+1;
 }


}
void drawP(int x,int r){    //ฟังก์ชันที่ใช้ในการวาดดาว โดยมีx และ y เป็นparameter
  if(x==0){
   fill(255,0,0);
  }
  else if(x==6){
  ellipse(elements[0][x],y,70,10);  //ตำแน่งของวงกลมในพิกัดxจะเปลี่ยนไปตาม
                                                     ค่าที่เราเก็บไว้ในตัวแปลelement[][] ข้างต้น
                                                     สังเกตได้จากตัวแปลxที่อยู๋ใน[]
                                                     เพราะทุกๆครั้งที่มีการวนลูปค่าxจะเพิ่มขึ้น
  }
  else{
   fill(255);
  }
  ellipse(elements[0][x],y,elements[r][x],elements[r][x]);
}
void drawmoon(){         //ฟังก์ชันที่ใช้ในการวาดพระจันทร์
  int c=0;
  while(c<moon[x]){
    fill(250,250,100);
   ellipse(elements[0][x]+20*cos(c),y+20*sin(c),3,3);
   c=c+1;
   fill(255);
  }

}
//อ้างอิงคำสั่งพื้นฐานต่างๆจาก lab1 http://com5630043.blogspot.com/search/label/lab1
//อ้างอิงอธิบายฟังก์ชันจาก lab2 http://com5630043.blogspot.com/search/label/lab2

//อ้างอิงอธิบายเรื่องif-elseจาก lab3 http://com5630043.blogspot.com/search/label/lab3%20%3A%20Condition