วันอังคารที่ 24 กันยายน พ.ศ. 2556

-Limited UFO-




void setup ()
{
  size(300, 300);
}

Ufo u = new Ufo (50, 50);
int check=0;

void draw()
{
  background(0);  
  u.display();
}

class Ufo
{
  int x, y, r;

  Ufo(int a, int b)
  {

    this.x=a;
    this.y=b;
    this.r=0;
  }

  void display() {
    if(r<3){
    drawUFO();
    move();
    }
  }
  void move(){
 
    if(this.x<270&&this.y==50){
      this.x=this.x+5;
   
    }
    if(this.x==270&&this.y<270){
     this.y=this.y+5;
   
    }
    if(this.x>30&&this.y==270){
     this.x=this.x-5;
    }
    if(this.x==30&&this.y>50){
      this.y=this.y-5;
    }
    if(this.x==30&&this.y==50){
      r++;
    }
 
 
  }
  void drawUFO() {
   fill(250,255,0);
   triangle(x,y,x-15,y+30,x+15,y+30);
   fill(150);
   ellipse(x,y,30,30);
   fill(30, 144 ,255);
   ellipse(x,y+5,50,15);
 
  }
}

-Class UFO-



void setup ()
{
  size(300, 300);
}

Ufo u = new Ufo (50, 50);
int check=0;

void draw()
{
  background(0);  
  u.display();
}

class Ufo
{
  int x, y, z;

  Ufo(int a, int b)
  {

    this.x=a;
    this.y=b;
    this.z=0;
  }

  void display() {
    drawUFO();
    move();
  }
  void move(){
    if(this.x<270&&this.y==50){
      this.x=this.x+1;
   
    }
    if(this.x==270&&this.y<270){
     this.y=this.y+1;
   
    }
    if(this.x>30&&this.y==270){
     this.x=this.x-1;
    }
    if(this.x==30&&this.y>50){
      this.y=this.y-1;
    }
 
  }
  void drawUFO() {
   fill(250,255,0);
   triangle(x,y,x-15,y+30,x+15,y+30);
   fill(150);
   ellipse(x,y,30,30);
   fill(30, 144 ,255);
   ellipse(x,y+5,50,15);
 
  }
}

=Array=(Medium)

Programming Exercise
Proposition => Write a Function that returns the lowest subscript in an array. Also find a function that returns the highest subscript in an array.


Translated into Thailand => เขียนฟังก์ชั่นที่ รีเทิร์นค่า ที่น้อยที่สุดและค่าที่มากที่สุด ใน Array


Solution=> 
void setup(){
 println("Max :"+Max(x));   //แสดงข้อความบนหน้าจอ
 println("Min :"+Min(x));
}
int[] x={4,2,3,4,5,7,6};   //ประกาศตัวแปลเป็นอะเรย์ชนิดจำนวนเต็ม 

int Max(int[] y){  //พังก์ชันที่มีการreturnค่าเป็นจำนวนเต็ม และมีparameter 
  int max=0;       //ประกาศและกำหนดค่า Maxในตอนแรก
  int i;
  for(i=0;i<y.length;i++){   //ใช้รันหาว่ามีตัวไหนในArrayที่มีค่ามากกว่าค่าMax 
   if(y[i]>max){
    max=y[i];         //เก็บค่าที่มีค่ามากกว่าค่าMaxคอนเริ่มต้นให้เป็นค่าMaxแทน
   }
  }
  return max;   //return max ไปที่ฟังก์ชัน
}

int Min(int[] m){      //พังก์ชันที่มีการreturnค่าเป็นจำนวนเต็ม และมีparameter 
 int min=m[0];      //ประกาศและกำหนดค่า Min ในตอนแรก
 int i;
 for(i=0;i<m.length;i++){  //ใช้รันหาว่ามีตัวไหนในArrayที่มีค่ามากกว่าค่าMin
   if(m[i]<min){
    min=m[i];         //เก็บค่าที่มีค่ามากกว่าค่าMinคอนเริ่มต้นให้เป็นค่าMinแทน
   }
  }
  return min;  //return min ไปที่ฟังก์ชัน

}



Out put => Max : 7
                  Min : 2

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

วันศุกร์ที่ 20 กันยายน พ.ศ. 2556

=Array=(Eazy)

Programming Exercise
Proposition => Write the code to calculate the average of the element included in a one-dimensional double array named rates. The array has five element.

Translated into Thailand => เขียนโค้ดเพื่อคำนวนหาค่าเฉลี่ยของค่าองค์ประกอบที่อยู่ในอะเรย์1มิติ ข้อมูลภายในอะเรย์มี5ข้อมูล


Solution=> 
int[] rate={16,9,8,11,7}; //ประกาศตัวแปลเป็นอะเรย์ชนิดจำนวนเต็ม 
int i;
float sum=0;
for(i=0;i<rate.length;i++){ //ลูปfor()ใช้ในการวนลูปเพื่อให้โปรแกรมทำงานซ้ำๆเมื่อเงื่อนไขเป็นจริง
  sum=sum+rate[i];              //ใช้ในการรวมผลบวกของทุกจำนวนในarray
}
println(sum/rate.length);   //แสดงค่าออกมาที่หน้าจอ

Out put => 10.2

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

=Fuction=(Hard)

Programming Exercise
Proposition => Write the code for a function that receives four integers. The function should calculate the average of the four integer, and the return result .name the function calcAverage()

Translated into Thailand =>
เขียนโค้ดสำหรับ ฟังก์ชั่นที่รับจำนวนเต็ม 4 จำนวน,เป็นฟังก์ชั่นที่ใช้คำนวนหาค่าเฉลี่ย ของสี่จำนวนและรีเทิร์นค่า โดยใช้ชื่อฟังก์ชั่นcalcAverage()

Solution=> 

void setup() {
  println(calcAverage(7,18,15,29));  //แสดงค่าออกมาทางหน้าจอโดยค่าที่แสดง
                                                         ได้จากการเรียกใช้ฟังก์ชันcalcAverage( )
}

float calcAverage(int a,int b,int c,int d) { //สร้างฟังก์ชันโดยกำหนดให้มีการ return ค่ากลับ และมีparameter
  return (a+b+c+d)/4;
}


Out put =>  17.0

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

=While=(Eazy)

Programming Exercise
Proposition =>Write a while clause that stops a posttest loop when the value in the quantity variable in less than the number 0.
Translated into Thailand =>  ให้เขียน while loop แล้วให้หยุดเมื่อค่า ในตัวแปลน้อยกว่า0

Solution=> 
int c=10;
while(c>=0){

 c--;

}





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

อธิบายการทำงานของโปรแกรม => ทุกๆครั้งที่มีการวนลูป ค่าcจะลดลงทุกๆครั้งก่อนออกจากลูป
และจะจบการทำงานเมื่อเงื่อนไข c>=0 ไม่เป็นจริง


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

-Class Button-




void setup() {
  size(200, 200);
  background(0);
}
Button a =new Button();     //ประกาศ object f =new Button( )ขึ้นมาเป็นButton
                                           (ซึ่งเป็นคลาสที่สร้างไว้)เป็นตัวแปรแบบ local variable
Button b =new Button("book");
void draw() {
  a.display();                        //เป็นการเรียกใช้ method ภายในฟังชันโดยกำหนดobject
                                             ที่ต้างการใช้ไว้ข้างหน้าmethod
  b.display();
}

class Button {       //เป็นการสร้างClass ขึ้นมาเพื่อเก็บData & Method
                                 ทำให้สะดวกในการใช้งานต่อไปในอนาคต
  String name;   //ประกาศ attribute name ขึ้นมาเป็นข้อความ ซึ่งเป็นตัวแปรแบบlocal variable
                                  แต่ตัวแปรนี้ method ภายในคลาสจะสามารถมองเห็นได้ด้วย
  int x, y, z;    //ประกาศ attribute x,y,z ขึ้นมาเป็นจำนวนเต้ม ซึ่งเป็นตัวแปรแบบlocal variable
                                  แต่ตัวแปรนี้ method ภายในคลาสจะสามารถมองเห็นได้ด้วย
  Button(String n) {     //constructor ใช้ในการกำหนด object
    this.name=n;
    x=100;
    y=100;
    z=55;
  }
  Button() {             //constructor defult ไม่มี parameter
    this.name="No";
    x=50;
    y=20;
    z=30;
  }
  void clicked() {   //method ที่ใช้ในการตรวจสอบพิกัดของปุ่มกับตำแหน่งที่กดเมาส์ว่าตรงกันไหม
    if (mousePressed==true&&mouseX<=x+z&&mouseX>=x&&mouseY>=y&&mouseY<=y+z) {
      println(" "+this.name);              //แสดงข้อคว่ามของปุ่มที่กด
      mousePressed=false;
    }
  }
  void display() {          //method ที่ใช้ในการแสดงภาพขึ้นCanvas
    rect(x, y, z, z);
    if (x==100) {
      fill(0);
      text("My name", x+2, y+30);
      fill(255);
    }
    a.clicked();
    b.clicked();
  }
}


//อ้างอิงคำสั่งพื้นฐานต่างๆจาก 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