แสดงบทความที่มีป้ายกำกับ Assignment1 แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Assignment1 แสดงบทความทั้งหมด

วันเสาร์ที่ 21 กันยายน พ.ศ. 2556

Array:Easy

Question : Complete the methods that draw a bar chart from an array of integer data
                สร้างฟังก์ชันวาดแผนภูมิจาก array ของข้อมูลจำนวนเต็ม

Answer :
void setup() {
  size (450, 450);
  background(25);
}
void draw () {
  int x = 50;
  int i = 0;
  int y = 50;
  int [] B = {13,20, 14,10, 22,28, 32 } ;
                 
  while (i < B.length) {
    textSize(10);
    fill(#1B9AF7);
    rect (x, 380, 20, -B[i]*10);
    fill (#FC0AA8);
    text(B[i]+"", x,400);
    i = i + 1;
    x = x + 50;
  }
  textSize(19);
  fill (#FC0AA8);
  text("integer data ", 50, 50);
}
source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 253
ISBN : 0-13010922-3


Function : Easy

   Question : Code a method which draws a circle,giveen the coordinates of the centre and the radius.It header should be
    สร้างฟังก์ชันที่วาดวงกลมโดยมีการกำหนด centre  และ radius

         private void circle(Graphics g,int xCentre, int yCentre, int radius)

   Answer :

          void setup() {
             size(200, 200);
             background(0);
          }
          void draw() {
             fill(#F5076F);
             circle(100, 100, 100);
          }
          void circle(int xCentre, int yCentre, int radius) {
             ellipse(xCentre, yCentre, radius, radius);
          }

source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 253
ISBN : 0-13010922-3



วันพุธที่ 18 กันยายน พ.ศ. 2556

ARRAY : MEDIUM

Question  Graph drawer  Write a class that provides a method to draw a graph of a function given as an array of x-coordinates and an array of corresponding y-coordinates. It has the heading 

วาดกราฟ โดยเขียนฟังก์ชันวาดกราฟโดยให้ค่า x และ y ในรูปแบบของ array
              
              public void drawGraph(float[]x,float[]y)

              The method draws straight lines from one point to another.It also draws the axes.

ฟังก์ชั่นวาดเส้นตรงตามแกน

Answer

int i;
int j;
float[]x={5,20,50,75.5,105,130,150,180,195};
float[]y={150,50,130,80,100,60,80,10,50};

void setup(){
size(200,200);
strokeWeight(5);
background(250);
drawGraph();
}
void drawGraph(){
  while(i<x.length&&j<=x.length){
    j=i+1;
  line(x[i],y[i],x[j],y[j]);
  ellipse(x[i],y[i],5,5);
  i=i+1;
  j=j+2;
  }
}





source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 253
ISBN : 0-13010922-3

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

Array : Hard

Question... (Pie chart)Write a class that displays a pie chart of the data that is passed to its method as an array.
                เขียนโค้ดแสดงแผนภูมิวงกลมโดยการใช้ array

Answer...
                 void setup(){
                     size(300,300);
                     noStroke();
                     noLoop();
                }
              
                int[] angles={20,25,45,45,45,90,60,30};
                int[] cB={#FF0509,#FF05B9,#7D05FF,#056FFF,#00FFF9,#03FF00,#F6FF00,#F7B407};
                float start = 0;
                int i = 0;
                float diameter=200;

              void piechart(){  
                  while( i < angles.length){
                      fill(cB[i]);
                      arc(width/2, height/2, diameter, diameter, start, start+radians(angles[i]));
                      start+= radians(angles[i]);
                      i=i+1;
                  }
              }

              void draw(){
                  background(0);
                  piechart();
              }

             การแสดงผล:


.
source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 253
ISBN : 0-13010922-3

Function:Hard

Question...Write a method which returns the inch equivalent of its centimetre parameter.An example invocation is:
               เขียนฟังก์ชันที่มีการรีเทิร์นค่านิ้ว โดยกำหนด เซนติเมตรในฟังชั่นพารามิเตอร์
                float inches = inchEquivalent(2.5f);

               Multiply centimetres by 0.394 to calculate inches.
               หาเซนติเมตรโดย 0.394 ในการคำนวณนิ้ว

Answer...

void setup(){
  inchEquivalent(2.5);
  println("Equivalent "+inchEquivalent(2.5)+"inch");
}
float inchEquivalent(float x){
  float n;
  n = x*0.394;
  return n;
}



source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 61
ISBN : 0-13010922-3
การแสดงผล


loops.:hard

Question...  ( Wall )Write a program to draw a wall of bricks, as shown in figure8.10.  Use the libraly method drawRect to draw each brick.This is invoked like this:
                 
                 
                   g. drawRect(x,y,width,height);

                  All the parameters are integers,representing a pixel number.x,y are the coordinates of the top left of the rectangle. Hint: use nested loops.
               

Answer....

               void setup(){}
               void draw() {
                  int flats=5;
                  int floors=4;
                  int yCoord=10;
                  int xCoord;
               for (int floor=0;floor<floors;floor++) {
                  xCoord=0;
                  for (int flat=0;flat<flats;flat++) {
                     rect(xCoord, yCoord, 18, 10);
                     rect(xCoord+9, yCoord+10, 18, 10);
                     xCoord=xCoord+20;
                  }
                  yCoord=yCoord+20;
                }
              }



source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 136
ISBN : 0-13010922-3
การแสดงผล



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

Variable : Easy

Question... assign an initial value to a variable:
                  กำหนดตัวแปรเริ่มต้นคือ
         
                  int totalSeconds = 2549;

                  and convert this to hours, minutes and seconds. Do an example with pen and paper before you write the program.
                  และให้แปลงเป็นชั่วโมง,นาที,และวินาที โดยทำตัวอย่างกับกระดาษก่อนที่จะเขียนโปรแกรม

Answer...
                int totalSeconds = 2549;//ตัวแปรที่โจทย์กำหนดมาให้
                int hours=0;//กำหนดตัวแปรเพื่อหาชั่วโมง
                int minutes=0;//กำหนดตัวแปรเพื่อหาค่านาที
                int seconds=0;//กำหนดตัวแปรเพื่อหาค่าวินาที

                hours = totalSeconds/3600;//คำนวณเพื่อแปลงค่าหาชั่วโมง
                minutes = totalSeconds/60;//คำนวณเพื่อแปลงค่าหานาที
                seconds = totalSeconds%60;//คำนวณเพื่อแปลงค่าหาวินาที

                println("hours is "+hours);//แสดงค่าของชั่งโมง
                println("minutes is "+minutes);//แสดงค่าของนาที
                println("seconds is "+seconds);//แสดงค่าของวินาที

               แสดงผล  =  hours is 0
                                     minutes is 42
                                     seconds is 29

source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 45
ISBN : 0-13010922-3

Variable : Medium

Question...  Assume that two students take a java exam, and the results are assigned to two variables:
                  ให้นักเรียนแสดงตัวอย่างการทำและผลจากตัวแปรสองตัว

                  int mark1 = 44;
                  int mark2 = 51;
                 
                  Write a program which calculates and displays the average mark as a float value. Check your answer with a calculator.
                  เขียนโปรแกรมในการคำนวณและแสดงผลค่าเฉลี่ยของ mark เป็นค่าลอยๆเป็นจำนวนที่มีจุดทศนิยม เช็คคำตอบกับเครื่องคิดเลข

Answer...

                  int mark1 = 44;
                  int mark2 = 51;
                  int plusmark =0;//กำหนดตัวแปรผลบวก
                  float averagemark = 0;//กำหนดตัวแปร average

                 plusmark = mark1+mark2;//คำนวณผลบวกของค่าที่ให้มา
                 averagemark = plusmark/2;//การหาค่าเฉลี่ย average
             
                 println("averagemark is "+averagemark);//การแสดงผล

                 แสดงผล = averagemark is 47

source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 44
ISBN : 0-13010922-3



         

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

Function :Medium

Question...Create a method  drawFamily  which  draws two adults and two children  (assumed  to  be  half  as  tall as  the  adults).Make  use  of  your  drawPerson, and  decide  on  your  own  parameter.

สร้าง drawFamily  มีผู้ใหญ่สองคนและเด็กสองคน(ความสูงเป็นครึ่งหนึ่งของผู้ใหญ่) ทำโดยใช้ฟังก์ชั่น drawPerson ของคุณ และกำหนดตัวแปรของคุณเอาเอง

Answer...


.....
void setup(){
  size(200,200);
}
  int a=0;//กำหนดเพื่อให้ภาพขยับไปทางด้านซ้าย
  int b=0;//กำหนดเพื่อให้ภาพมีตำแหน่งเลื่อนลง
  int y= 160;//จุดต่ำสุดของภาพ
  int y1= 120;

 
void drawAdult(int a,int b, int r,int l){
  strokeWeight(8);//ขนาดของเส้นที่ใช้ในการวาด
  line(40+a,50+b,40+a,y1+l);//ลำตัวของคน
  line(40+a,y1+l,20+a,y);//ขาข้างซ้ายของคน
  line(40+a,y1+l,60+a,y);//ขาข้างขวาของคน
  line(20+a,80+l,60+a,80+b);//แขนของคน
  strokeWeight(2); //ขนาดของเส้นที่ใช้ในการวาด
  ellipse(40+a,50+b,r,r);//หัวของคน
}


void draw(){
  background(0,random(123),random(250));//พื้นหลังเป็นแบบสุ่ม
  drawAdult(0,0,50,0);//ผู้ใหญ่ด้านซ้ายสุด
  drawAdult(40,0,50,0);//ผู้ใหญ่ด้านตรงกลาง
  drawAdult(80,45,30,20);//เด็กตรงกลาง
  drawAdult(120,45,30,20);//เด็กด้านริมขวาสุด
 

}


source : Java for students
Douglas Bell and Mike Parr. -- 2nd ed. page 61
ISBN : 0-13010922-3

วันอังคารที่ 23 กรกฎาคม พ.ศ. 2556

Variable : Hard

Question...  Write the C code to calculate and print the next two numbers in the                            following series. You may use only one variable in each problem.

                ให้เขียนโค้ดคำนวณและแสดงผลค่าของตัวเลข 2 จำนวนถัดไป ของลำดับต่อไปนี้ โดย                         สามารถใช้ตัวแปรเพียงแค่ตัวเดียวในแต่ละปัญหา


                0,2,4,6,8,10,...,...

Answer...


       int n=0;
           while(n<=7){
           print(n*2);
           n=n+1;
        }

       จะแสดงผลหรือพิมพ์ออกมาเป็น : 02468101214(ซึ่งเราไม่สามารถแยกจำนวนออกได้)


        int n=0;
           while(n<=7){
           println(n*2);
           n=n+1;
        }

         จะแสดงผลหรือพิมพ์ออกมาเป็น :  0
                                                          2
                                                          4
                                                          6
                                                          8
                                                         10
                                                         12
                                                         14
         (ซึ่งทำให้เราสามารถแยกจำนวนออกได้จำนวนไหนเป็นจำนวนไหน)

วันจันทร์ที่ 22 กรกฎาคม พ.ศ. 2556

condition

Question...Rewrite the following sequence of if statements as a single if-else.
           
                ให้เขียนลำดับ statement ใหม่ โดยให้เปลี่ยนจาก if เป็น if-else เพียงอันเดียว

                if (year % 4==0)
                     System.out.print(year+"is a leap year.");
                if(year % 4 !=0)
                {
                     year = year +4 - year % 4;
                     System.out.print(year+"is the next leap year.");
                }

Answer...
                if (year % 4==0){
                     System.out.print(year+"is a leap year.");
                if(year % 4 !=0)
                }else{
                     year = year +4 - year % 4;
                     System.out.print(year+"is the next leap year.");
                }


source : Programming and problem solving with Java
Nell Dale and Chip Weems. -- 2nd ed. page 270
ISBN-13 : 978-0-7637-3402-2

Loop


1.       Question... What does the following loop print out?(number  is  of type  int.)

                  อะไรทำให้ loop พิมพ์ค่าออกมา (ประกาศตัวแปร number เป็นประเภท int)


                  number = 1;
                  while (number <11)
                  {
                      number ++;
                      out.println(number);
                   }

Answer...
               
               int number = 1;
                  while (number <11)
                  {
                      number ++;
                      System.out.println(number);
                   }

                เมื่อมีการประกาศตัวแปรด้วย int แล้ว ก็ยังไม่สามารถ run code ได้ เนื่องจากว่า
การที่เราจะตั้ง statement ให้พิมพ์ค่าใดออกมานั้น ต้องมีคำว่า System ไว้ข้างหน้า out เสมอหรือไม่ก็ไม่ต้องใส่ out ให้ตั้ง statement เป็น println ไปเลย

               int number = 1;
                  while (number <11)
                  {
                      number ++;
                      println(number);
                   }


source : Programming and problem solving with Java
Nell Dale and Chip Weems. -- 2nd ed. page 330
ISBN-13 : 978-0-7637-3402-2

Loop


Question...When the following code is executed,how many iterations of loop are performed?

เมื่อมีโค้ดถูกดำเนินการ  มีการวน loop เท่าไหร่

int number =2;
boolean done = false;
while(!done){
  number =number*2;
  if (number>64)
  done=true;
}

Answer...

int number =2;
boolean done = false;
while(!done){
  number =number*2;
  println(number);
  if (number>64)
  done=true;
}


เพื่อให้เราสามารถสังเกตจำนวนครั้งการวน loop ได้นั้นเราจึงใส่ statement "println(number);"ลงไป
หลังจากการ run code ทำให้เราทราบว่ามีการรวน loop ทั้งหมด 6 ครั้ง ตามบรรทัดของตัวเลขที่ output 



source : Programming and problem solving with Java
Nell Dale and Chip Weems. -- 2nd ed. page 330
ISBN-13 : 978-0-7637-3402-2

Condition


Question...Give this code fragment:

if (height>= minHeight){
  if(weight>=minWeight){
      System.out.print("Eligible to serve.");
  }else{
      System.out.print("Too light to serve.");
  }
}else{
   if (weight>=minWeight){
       System.out.print("Too short to serve.");
   }else{
   System.out.print("Too short and too light to serve.");
  }
}

a. What is the output when height exceeds minHeight and weight exceeds minWeight?
   สิ่งใดจะถูกส่งออกเมื่อ height มากกว่า minHeight และ weight มากกว่า minWeight

   Answer...เราสามารถหาคำตอบได้โดยการประกาศและกำหนดค่าดังนี้
   
   int height =12;//มีค่ามากกว่า minHeight เป็นไปตามที่โจทย์กำหนด
   int minHeight =10;//มีค่าน้อยกว่า height
   int weight =3;//มีค่ามากกว่า minWeight เป็นไปตามที่โจทย์กำหนด
   int minWeight = 0;//มีค่าน้อยกว่า weight

      เมื่อเราได้ประกาศและกำหนดค่าต่างๆเหล่านี้ไปแล้ว output ของ code นี้คือ Eligible to serve.



a. What is the output when height is less than minHeight and weight is less than  minWeight?
    สิ่งใดจะถูกส่งออกเมื่อheight น้อยกว่า minHeight และ weight น้อยกว่า minWeight

   Answer...เราสามารถหาคำตอบได้โดยการประกาศและกำหนดค่าดังนี้
   
   int height =12;//มีค่าน้อยกว่า minHeight เป็นไปตามที่โจทย์กำหนด
   int minHeight =14;//มีค่ามากกว่า height
   int weight =3;//มีค่าน้อยกว่า minWeight เป็นไปตามที่โจทย์กำหนด
   int minWeight = 5;//มีค่ามากกว่า weight

      เมื่อเราได้ประกาศและกำหนดค่าต่างๆเหล่านี้ไปแล้ว output ของ code นี้คือ Too short and too light to serve.



source : Programming and problem solving with Java
Nell Dale and Chip Weems. -- 2nd ed. page 267
ISBN-13 : 978-0-7637-3402-2

condition


Question....Given the float variables x1,x2,y1,y2,and m ,write a code segment to find the slope of a line through the two points (x1,y1)and(x2,y2). Use the formula

ให้ตัวแปรลอยๆ x1, x2, y1, y2, และ m ให้เขียนโค้ดหาความชันของเส้นผ่านจุดสองจุด (x1, y1) และ (x2, y2) 


ใช้สูตร


              m = y2-y1

                     x2-x1

to determine the slope of the line. If x1 equals x2,the line is vertical and the slope is undefined. The segment should display the slope with an appropriate label.If the slope is undefined, it should display the message “Slope undefined.”


กำหนดความชันของเส้น ถ้า x1 เท่ากับ x2 เส้นแนวตั้งและความชันจะหาค่าไม่ได้ ส่วนการแสดงผลความชันที่เหมาะสมควรเป็นป้ายประกาศ  ถ้าความชันหาค่าไม่ได้ก็ควรแสดงข้อความ "ความชันหาค่าไม่ได้"


Write a code :

 //ประกาศและกำหนดตัวแปรเพื่อให้โค้ดสามารถแสดงผลให้เราดูได้
int y1= 10;
int y2= 15;
int x1= 15;
int x2= 15;//หากกำหนดค่าของ x2 มากกว่า x1 จะทำให้เราสามารถหาค่าของความชั่นได้
int m=0;
if(x2-x1>0){//เงื่อนไขบอกว่าถ้า x2-x1 แล้วมากกกว่า 0 จะสามารถหาค่าความชันได้โดย statement ด้านล่าง
    m=y2-y1/(x2-x1);
    println(+m);
}else{//ถ้าไม่เป็นไปตามเงื่อนไขด้านบนจะทำตาม statement ด้านล่างนี้
    System.out.print("Slope undefined.");
}




source : Programming and problem solving with Java
Nell Dale and Chip Weems. -- 2nd ed. page 271
ISBN-13 : 978-0-7637-3402-2