= Easy =
Proposition => Write the object definition functions for an object called car with four properties: model, make, year and price and define value respectively 67, 83, 12 and 25. (กำหนดฟังก์ชั่นชื่อ car สำหรับโจทย์ต่อไปนี้ model, make, year, price และใส่ค่าที่เป็นจำนวนเต็มตามลำดับ 67, 83, 12, 25)Solution =>
void car () {
int model = 67;
int make = 83;
int year = 12;
int price = 25;
}
Explain => จากโจทย์ให้กำหนดฟังชั่นที่ชื่อ car สำหรับเรียกชื่อ model, make, year, price ซึ่งทำได้ดังนี้
void car () { //เป็นการประกาศฟังก์ชั่นชื่อ car และ void ข้างหน้าคือการไม่เรียกค่ากลับ
int model = 67; //เป็นการประกาศตัวแปรชื่อmodelทางซ้าย โดยรับค่าจำนวนเต็มคือ 67 ทางด้านขวา
int make = 83;
int year = 12;
int price = 25;
}
From : Teach yourself java script in a week => ISBN : 1-57521-073-8
= Medium=
Proposition => For each of the following problem, decide which is more appropriate, an if-else or an if in function Explain your answers. Students who are candidates for admission to a college submit their SAT scores. If a student's score is equal to or greater than a certain value, print a letter of acceptance for the student. Otherwise, print a rejection notice. (ลองใช้ if-else หรือ if ในฟังก์ชั่นช่วยในการอธิบายโจทย์ต่อไปนี้ นักเรียนที่สมัครเข้าศึกษาต่อที่วิทยาลัยโดยใช้คะแนนSATถ้าคะแนนของนักเรียนมากกว่าหรือเท่ากับค่าคงที่ที่เรากำหนด ให้พิมพ์คำว่า acceptance แต่ถ้าไม่พิมพ์คำว่า rejection)Solution =>
void setup () {
//variable
int SAT = 50;
int x = 30;
if (SAT>=x) {
println ("acceptance");
}
else {
println ("rejection");
}
}
Explain =>
void setup () { //เป็นการประกาศฟังก์ชั่นชื่อ setup โดยที่ไม่มีการเรียกค่ากลับเนื่องจากมี void
//variable
int SAT = 50; //เป็นการประกาศตัวแปร โดยที่ตัวเลขทางด้านขวามือ คือคะแนนสอบSATของนักเรียนแต่ละคนนี่สมมุติให้เป็น 50 จะนำค่าไปใส่ให้กับตัวแปรชื่อ SAT ทางซ้ายมือของเครื่องหมายเท่ากับ int x = 30; // ตัวแปร x คือค่าคงตัวที่เรากำหนดใช้เป็นเกณฑ์ในการเปรียบเทียบ
if (SAT>=x) { //เป็นเงื่อนไข คือถ้า SAT มากกว่าหรือเท่ากับ x จะพิมพ์คำว่า acceptance ซึ่งแปลว่ายอมรับ ถ้าไม่จะพิมพ์คำว่า rejection แปลว่าปฏิเสท นั่นเอง println ("acceptance");
}
else {
println ("rejection");
}
}
From : Programming and Problem solving with Java => ISBN_13 : 978-0-7637-3402-2
= Hard =
Proposition => Create User define functions for draw a picture multiple circles.(สร้างฟังก์ชั่นที่กำหนดเอง สำหรับวาดภาพวงกลมหลายวง)
Solution =>
void MulCircle (int x, int r) {
ellipse (x, 50, r, r);
}
void setup () {
size (100, 100);
MulCircle (10, 10);
MulCircle (20, 10);
MulCircle (30, 10);
MulCircle (40, 10);
MulCircle (50, 10);
MulCircle (60, 10);
MulCircle (70, 10);
MulCircle (80, 10);
MulCircle (90, 10);
}
Explain =>
void MulCircle (int x, int r) {
ellipse (x, 50, r, r);
} //เป็นการสร้างฟังก์ชั่นใหม่ โดยชื่อ MulCircle ภายในประกอบด้วยตัวแปร x, r และคำสั่งที่ใช้วาดวงกลม
void setup () {
size (100, 100);
MulCircle (10, 10); //เป็นการเรียกใช้ฟังก์ชั่น โดยวิธีเรียกใช้คือ ใช้แค่ชื่อ และใส่ค่าของพารามิเตอร์ ถ้ามี ดังตัวอย่าง ชื่อฟีงก์ชั่นคือ MulCircle มีพารามืเตอร์คือ (10, 10) นั่นเอง
MulCircle (20, 10);
MulCircle (30, 10);
MulCircle (40, 10);
MulCircle (50, 10);
MulCircle (60, 10);
MulCircle (70, 10);
MulCircle (80, 10);
MulCircle (90, 10);
} //เป็นฟังก์ชั่นชื่อ setup ซึ่งจะถูกเรียกใช้ก่อนเสมอ โดยในที่นี้สร้างขึ้นมาเพื่อเรียกใช้ฟังก์ชั่น MulCircle เป็นการวากวงกลมนั่นเอง
From : Programming and Problem solving with Java => ISBN_13 : 978-0-7637-3402-2
ไม่มีความคิดเห็น:
แสดงความคิดเห็น