void setup(){
size(400,250);
background(111);
Fraction f = new Fraction(2,7);
Fraction g = new Fraction(4,9);
f.add(3);
text("="+f.toString(),195,140);
}
class Fraction{
int n;
int d;
Fraction(int a,int b){
n = a; //this.n = a
d = b;
}
void add(int z){
text(this.n+"/"+this.d,195,100);
text(z,195,120);
this.n = this.d*z+this.n;
}
String toString(){
String s = this.n+"/"+this.d;
return s;
}
void reciprocal(){
int temp;
temp = n;
n = d;
d = temp;
}
}
20 กันยายน 2556
Palindrome
.. Palindrome ..
~ ~ hu hu hu :')) ~ ~
Palindrome Processing.js code
17 กันยายน 2556
Assignment 2
void Snoopy(float x, float y) {
noStroke();
ellipse(x+50, y, 50, 50);
ellipse(x+40, y+10, 60, 40);
ellipse(x+45, y+45, 25, 30);
ellipse(x+55, y+55, 20, 5);
fill(0, 0, 0);
ellipse(x+30, y+10, 10, 7);
ellipse(x+40, y-5, 3, 7);
ellipse(x+45, y-5, 3, 7);
ellipse(x+70, y+10, 20, 35);
stroke(0, 0, 0);
fill(255);
ellipse(x+30, y+50, 15, 25);
ellipse(x+40, y+50, 15, 25);
ellipse(x+55, y+50, 10, 10);
}
int sno = 300;
void setup() {
size(600, 400);
}
void draw(){
int i = 0;
int j = 0;
int h = 45;
int l = 40;
int x = 100;
int y = height-140;
int r = 20;
background(111);
Snoopy(width-190, sno);
while(j<3){
if(j==0){
while(i<8){
ellipse(x,y,r,2*r/3);
x = x+l;
y = y-h;
h = h-7;
i = i+1;
}
}
else if(j==1){
while(i<7){
ellipse(x,y,r,2*r/3);
x = x+l+10;
y = y-h;
h = h-13;
i = i+1;
}
}else if(j==2){
while(i<6){
ellipse(x,y,r,2*r/3);
x = x+l+17;
y = y-h+10;
h = h-20;
i = i+1;
}
}
h = 45;
j = j+1;
i = 0;
x = 100;
y = height-140;
}
}
void keyPressed() {
if (keyCode == UP) {
if (sno>100) {
sno=sno-100;
}
}
if (keyCode ==DOWN) {
if (sno<300) {
sno=sno+100;
}
}
}
void tennis(float x, float y) {
ellipse(x, y, 20, 20);
}
noStroke();
ellipse(x+50, y, 50, 50);
ellipse(x+40, y+10, 60, 40);
ellipse(x+45, y+45, 25, 30);
ellipse(x+55, y+55, 20, 5);
fill(0, 0, 0);
ellipse(x+30, y+10, 10, 7);
ellipse(x+40, y-5, 3, 7);
ellipse(x+45, y-5, 3, 7);
ellipse(x+70, y+10, 20, 35);
stroke(0, 0, 0);
fill(255);
ellipse(x+30, y+50, 15, 25);
ellipse(x+40, y+50, 15, 25);
ellipse(x+55, y+50, 10, 10);
}
int sno = 300;
void setup() {
size(600, 400);
}
void draw(){
int i = 0;
int j = 0;
int h = 45;
int l = 40;
int x = 100;
int y = height-140;
int r = 20;
background(111);
Snoopy(width-190, sno);
while(j<3){
if(j==0){
while(i<8){
ellipse(x,y,r,2*r/3);
x = x+l;
y = y-h;
h = h-7;
i = i+1;
}
}
else if(j==1){
while(i<7){
ellipse(x,y,r,2*r/3);
x = x+l+10;
y = y-h;
h = h-13;
i = i+1;
}
}else if(j==2){
while(i<6){
ellipse(x,y,r,2*r/3);
x = x+l+17;
y = y-h+10;
h = h-20;
i = i+1;
}
}
h = 45;
j = j+1;
i = 0;
x = 100;
y = height-140;
}
}
void keyPressed() {
if (keyCode == UP) {
if (sno>100) {
sno=sno-100;
}
}
if (keyCode ==DOWN) {
if (sno<300) {
sno=sno+100;
}
}
}
void tennis(float x, float y) {
ellipse(x, y, 20, 20);
}
Background --> Assignment 2
.. Assignment 2..
~ ~ Background :')) ~ ~
Background Processing.js code
15 กันยายน 2556
..# Array #..
..# Array #..
Easy
P.524 /3
Write the C++ statement to declare a
one-dimensional double array named rates that has five elements.
Then write the code to assign the following numbers to the array: 6.5, 8.3,
4,2,and 10.5.
Answer double[] rate = new int[5];
double[] rate[0] = 6.5;
double[] rate[1] = 8.3;
double[] rate[2] = 4;
double[] rate[3] = 2;
double[] rate[4] = 10.5;
// or double[] rate = {6.5, 8.3, 4, 2, 10.5};
Answer double[] rate = new int[5];
double[] rate[0] = 6.5;
double[] rate[1] = 8.3;
double[] rate[2] = 4;
double[] rate[3] = 2;
double[] rate[4] = 10.5;
// or double[] rate = {6.5, 8.3, 4, 2, 10.5};
Write the C++ code to add together the numbers
stored in the first and second elements inclouded in a one-dimensional int
array named num. Display the sum on the screen.
Answer int[] num = {20, 30};
println(num[0]+num[1]);
// add together the numbers stored in the first and second elements
// output 50
Answer int[] num = {20, 30};
println(num[0]+num[1]);
// add together the numbers stored in the first and second elements
// output 50
Hard
P.524 /20
Write the C++ cord to
add together the number stored in the first row, first column of the num
array, and the number stored in the second row, first column of the num
array. Display the sum on the screen.
Answer void setup(){
int[][] num = {{1,2},{4,3}}; // แระกาศและกำหนดค่าอะเรย์สองมิติ num
int[] N = new int[num[1].length]; /* ประกาศและกำหนดให้ค่าอะเรย์หนึ่งมิติ N ให้รับค่าผลบวกของ column มีความยาวเท่ากับ จำนวน column ในแถวที่ 1 */
int i = 0; // ประกาศและกำหนดค่าเริ่มต้น i = 0
while (i < num[1].length){
N[i] = num[0][i]+num[1][i];
/* add together the number stored in the first row, first column of the num array, and the number stored in the second row, first column */
i = i + 1; // เพิ่มค่า i จะขึ้นตำแหน่งใหม่
}
println(N);
}
Answer void setup(){
int[][] num = {{1,2},{4,3}}; // แระกาศและกำหนดค่าอะเรย์สองมิติ num
int[] N = new int[num[1].length]; /* ประกาศและกำหนดให้ค่าอะเรย์หนึ่งมิติ N ให้รับค่าผลบวกของ column มีความยาวเท่ากับ จำนวน column ในแถวที่ 1 */
int i = 0; // ประกาศและกำหนดค่าเริ่มต้น i = 0
while (i < num[1].length){
N[i] = num[0][i]+num[1][i];
/* add together the number stored in the first row, first column of the num array, and the number stored in the second row, first column */
i = i + 1; // เพิ่มค่า i จะขึ้นตำแหน่งใหม่
}
println(N);
}
..# Loop #..
..# Loop #..
Easy
P.351 /1
Assume a program declares an int named evenNum and initializes it to 2.
Write a C++ while loop that uses the evenNum variable to display
the even integers between 1 and 9
Answer int evenNum = 2; // ประกาศและกำหนดค่าตัวแปร evenNum เริ่มที่ 2
while (evenNum < 9) { // สร้าง Loop ที่ evenNum น้อยกว่า 9
println (evenNum); // ปริ๊นค่า evenNum ในบรรทัดใหม่
evenNum = evenNum + 2; // เพิ่มค่า evenNum 2 หน่วย เพื่อที่จะเป็นจำนวนคู่
} // end while
Answer int evenNum = 2; // ประกาศและกำหนดค่าตัวแปร evenNum เริ่มที่ 2
while (evenNum < 9) { // สร้าง Loop ที่ evenNum น้อยกว่า 9
println (evenNum); // ปริ๊นค่า evenNum ในบรรทัดใหม่
evenNum = evenNum + 2; // เพิ่มค่า evenNum 2 หน่วย เพื่อที่จะเป็นจำนวนคู่
} // end while
Middle
P.390 /2
Assume a program declares two int variable
named firstLoop and secondLoop. Both variable are initialized to
the number 1. Write the C++ code that uses these variables to display the
number 1 and 3 on four lines, as follows.
1 2 3
1 2 3
1 2 3
1 2 3
Answer int firstLoop = 1; // ประกาศและกำหนดค่าตัวแปร firstLoop เท่ากับ 1
int secondLoop = 1; // ประกาศและกำหนดค่าตัวแปร secondLoop เท่ากับ 1
while (firstLoop <=4) { // สร้าง Loop ที่ค่า firstLoop น้อยกว่าหรือเท่ากับ 4
while (secondLoop <= 3) { // สร้าง Loop ที่ค่า secondLoop น้อยกว่าหรือเท่ากับ 3
print(secondLoop); // ปริ๊นค่า secondLoop ในบรรทัดเดียวเดิม
print(" "); // ปริ๊น ช่องว่าง ในบรรทัดเดียวกัน
secondLoop = secondLoop + 1;
// เพิ่มค่า secondLoop 1 หน่วย แล้วกลับไปพิจารณาเงื่อนไขการวน loop
} // end while secondLoop
println(); // ปริ๊นเริ่มบรรทัดใหม่
firstLoop = firstLoop + 1;
// ให้ firstLoop เพิ่ม 1 หน่วย กลับไปพิจารณาเงื่อนไขการวนค่า loop
secondLoop = 1; // รีเซ็ตค่า secondLoop เพื่อเริ่มการวน loop ที่สองใหม่
} // end while firstLoop
1 2 3
1 2 3
1 2 3
1 2 3
Answer int firstLoop = 1; // ประกาศและกำหนดค่าตัวแปร firstLoop เท่ากับ 1
int secondLoop = 1; // ประกาศและกำหนดค่าตัวแปร secondLoop เท่ากับ 1
while (firstLoop <=4) { // สร้าง Loop ที่ค่า firstLoop น้อยกว่าหรือเท่ากับ 4
while (secondLoop <= 3) { // สร้าง Loop ที่ค่า secondLoop น้อยกว่าหรือเท่ากับ 3
print(secondLoop); // ปริ๊นค่า secondLoop ในบรรทัดเดียวเดิม
print(" "); // ปริ๊น ช่องว่าง ในบรรทัดเดียวกัน
secondLoop = secondLoop + 1;
// เพิ่มค่า secondLoop 1 หน่วย แล้วกลับไปพิจารณาเงื่อนไขการวน loop
} // end while secondLoop
println(); // ปริ๊นเริ่มบรรทัดใหม่
firstLoop = firstLoop + 1;
// ให้ firstLoop เพิ่ม 1 หน่วย กลับไปพิจารณาเงื่อนไขการวนค่า loop
secondLoop = 1; // รีเซ็ตค่า secondLoop เพื่อเริ่มการวน loop ที่สองใหม่
} // end while firstLoop
Hard P.394
/6:B
Complete the program by entering the instructions
to display the following pattern of asterisks ( 9 asterisks, 8 asterisks, 7
asterisks, 6 asterisks, 5 asterisks, 4 asterisks, 3 asterisks, 2 asterisks, 1
asterisks ).
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Answer void setup() {
int i = 9;
while (i > 0) {
printV(i); // เรียกใช้ฟังก์ชัน printV และใส่ค่า parameter ให้เท่ากับค่า i
println(); // ปริ๊นบรรทัดใหม่
i = i - 1; // ให้ค่า I ลดลง 1 หน่วย
} // end loop
} // end function
void printV(int n) { // สร้างฟังก์ชัน printV มี parameter n* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Answer void setup() {
int i = 9;
while (i > 0) {
printV(i); // เรียกใช้ฟังก์ชัน printV และใส่ค่า parameter ให้เท่ากับค่า i
println(); // ปริ๊นบรรทัดใหม่
i = i - 1; // ให้ค่า I ลดลง 1 หน่วย
} // end loop
} // end function
int count = 0; // ประกาศและกำหนดค่าตัวแปร count = 0
while (count < n) { // สร้าง loop ให้ค่า count น้อยกว่าค่า ตัวแปร n
print("* "); // ปริ๊น “* ” ในบรรทัดเดียวกัน
count = count+1; // เพิ่มค่า count 1 หน่วย แล้วกลับไปดูเงื่อนไขการวน loop
} // end loop
} // end function printV
..# condition #..
..# condition #..
Easy
P.292 /1
Assume you want to create a program that displays
the message “Highest honors” when a student’s test score is 90 or above. When
the test score is 70 through 89, the program should displays the message “Good
job”. For all other test score, the program should displays the message “Retake
the test”. Write the pseudocode for this program’s selection structure.
Answer if (score >= 90)
display “Highest honors”
else
if (score >=70)
display “Good job”
else
display “Retake the test”
end if
end if
Answer if (score >= 90)
display “Highest honors”
else
if (score >=70)
display “Good job”
else
display “Retake the test”
end if
end if
Middle
P.292 /2
Write the C++ code that corresponds to the
pseudocode you wrote in Question 1. The student’s score is stored in an int variable
named score.
Answer int score = 80; // ประกาศและกำหนดค่าตัวแปร score
if (score >= 90) { // เงื่อนไขที่ถ้า score มากกว่าหรือเท่ากับ 90
println("Highest honors"); // ปริ๊น Highest honors
}
else
if (score >= 70) { // เงื่อนไขที่ถ้าไม่ใช่แล้ว score มากกว่าหรือเท่ากับ 70
println ("Good job"); // ปริ๊น Good job
}
else { // เงื่อนไขที่ถ้า score น้อยกว่า 70
println ("Retake the test"); // ปริ๊น Retake the test
} // end if
Answer int score = 80; // ประกาศและกำหนดค่าตัวแปร score
if (score >= 90) { // เงื่อนไขที่ถ้า score มากกว่าหรือเท่ากับ 90
println("Highest honors"); // ปริ๊น Highest honors
}
else
if (score >= 70) { // เงื่อนไขที่ถ้าไม่ใช่แล้ว score มากกว่าหรือเท่ากับ 70
println ("Good job"); // ปริ๊น Good job
}
else { // เงื่อนไขที่ถ้า score น้อยกว่า 70
println ("Retake the test"); // ปริ๊น Retake the test
} // end if
Hard
P.309 /1
Write the C++ if statement that compares
the contents of the quantiry variable to the number 10. If the quantity variable contains a number that
id equal to 10, display the string “Equal”. If the quantity variable
contains a number that is greater than 10, display string “Over 10”. If the quantity
variable contains a number that is less than 10, display the string “Not over
10”.
..# Function #..
..# Function #..
Easy P.217 /2
Write the C++ code for a void function that
prompts the user to enter the name of an item, and than stores the user’s
response in the String variable whose address is passed to the function.
Name the function getItemName().
Answer void getItemName(){
}
Answer void getItemName(){
}
Middle
P.166 /1
Write the C++ code for a function that receives
an integer passed to it. The function, name halveNumber() , should
divide the integer by 2, and than return the result (which may contain a
decimal place.)
Answer void setup(){ //สร้างฟังก์ชัน เพื่อทดลองการปริ๊นค่า
println(halveNumber(20)); // ปริ๊นค่า ฟังก์ชัน halveNumber เมื่อให้ค่า x = 20;
}
float halveNumber( int x ){ // สร้างฟังก์ชันที่ส่งค่ากลับได้ชื่อ halveNumber
return x/2; // divide the integer by 2 and than return the result
}
Answer void setup(){ //สร้างฟังก์ชัน เพื่อทดลองการปริ๊นค่า
println(halveNumber(20)); // ปริ๊นค่า ฟังก์ชัน halveNumber เมื่อให้ค่า x = 20;
}
float halveNumber( int x ){ // สร้างฟังก์ชันที่ส่งค่ากลับได้ชื่อ halveNumber
return x/2; // divide the integer by 2 and than return the result
}
Hard
P.217 /4
Write the C++ cord for a void function that
receives three double variable: the first two by value and the
last one by reference. The function should divide the first variable by
the second variable, and than store the result in the third variable. Name the
function quotient().
9 กันยายน 2556
T^T Binary display T^T
T^T Binary display T^T
~ ~ ฮือๆๆ ~ ~
Binary Processing.js code
8 กันยายน 2556
Factorial
.. Factorial ..
~ ~ hu hu hu :')) ~ ~
Factorial Processing.js code
reverseWord
.. reverseWord ..
~ ~ hu hu hu :')) ~ ~
My reverseWord Processing.js code
3 กันยายน 2556
GCD
int gcd(int x, int y){
if(x == 0){
return y;
}
if(y == 0){
return x;
}
if(x>y){
return gcd(y,x%y);
}else{
return gcd(x,y%x);
}
}
void setup(){
println(gcd(30,3));
}
if(x == 0){
return y;
}
if(y == 0){
return x;
}
if(x>y){
return gcd(y,x%y);
}else{
return gcd(x,y%x);
}
}
void setup(){
println(gcd(30,3));
}