What is a method? Method คือ คำสั่งที่เกิดจากกลุ่มของโค้ด คำสั่งที่รวมกันอยู่ภายใต้คำสั่งเดียว เพื่อให้คำสั่งใหม่ ที่ได้นั้นทำงานได้ตามวัตถุประสงค์ ประโยชน์ของ.

Slides:



Advertisements
งานนำเสนอที่คล้ายกัน
J2EE Architecture for Web Application & Enterprise Programming
Advertisements

Island Problem • Given a bitmap of aerial photographer of archipelago (chain of islands) – Bitmap is black & white • White means land • Black means sea.
Pointers. Why have pointers? / pointers ทำให้ฟังก์ชันหรือส่วนของ โปรแกรมสามารถใช้งานข้อมูลร่วมกันได้ / ใช้ pointers ในการสร้างโครงสร้างข้อมูลที่ ซับซ้อนได้
1 C Programming An Introduction. 2 Preprocessing Directives เขียนได้ 2 รูปแบบ #include คอมไพเลอร์จะทำ การค้นหาเฮดเดอร์ไฟล์ที่ระบุ จากไดเร็คทอรีที่ใช้
Component องค์ประกอบของ GUI.
Structure and Concept of Interactive lecture
Graphical User Interface charturong.ee.engr.tu.ac.th/CN208
In the Classroom Manoonsak Glinhom.
บทที่ 11 การเขียนโปรแกรมโดยใช้ข้อมูลชนิดพอยท์เตอร์
Chapter 5: Functions of Random Variables. สมมติว่าเรารู้ joint pdf ของ X 1, X 2, …, X n --> ให้หา pdf ของ Y = u (X 1, X 2, …, X n ) 3 วิธี 1. Distribution.
ระบบการจัดเก็บในคลังสินค้า
CS Assembly Language Programming Period 35.
LAB # 4 Computer Programming 1 1. พิจารณาโปรแกรมต่อไปนี้
LAB # 4.
การโปรแกรมเชิงวัตถุ (Object-Oriented Programming)
Principal Facts and Ideas Objectives 1. 1.Understand principal properties of central-force problem 2. 2.Solve problems : angular momentum of a single particle.
Object Oriented Programing
SCC : Suthida Chaichomchuen
CS Assembly Language Programming Period 28.
Create the Knowledge Management Blueprint
8/3/2014The Realities of software Testing1 Software testing Realities What is the realities of software testing Why does the software testing not complete.
OOP (Object-Oriented Programming)
อาร์เรย์ (Array).
บทที่ 6 เมธอด.
1 Javascript with Form. 2 Javascript - Get data from form post method formname.field.value get method formname.getElementById(“field")
Menu and Interactive with Powerpoint ให้นำเรื่อง Input /Output Technology มา จัดทำ การนำเสนอ โดยใช้หลักการ Menu and Interactive with powerpoint มาประยุกต์
ฟังก์ชัน ง30212 การเขียนโปรแกรมด้วยภาษาคอมพิวเตอร์ ศูนย์คอมพิวเตอร์
คำสั่ง for ง30212 การเขียนโปรแกรมด้วยภาษาคอมพิวเตอร์
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Java Programming Language.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved JTextArea Constructors 
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Java Programming Language.
Java Programming Language สาขาวิชาระบบสารสนเทศ คณะบริหารธุรกิจ มหาวิทยาลัยเทคโนโลยีราชมงคลกรุงเทพ.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Java Programming Language.
Thread Thread ส่วนของ process ที่ให้ CPU ประมวลผล.
Project Framework Risk & Issue Management Sponsor Management
Java Array and String Jarungjit Parnjai
คณะบริหารธุรกิจ วิทยาเขตกล้วยน้ำไท bu.ac.th
Polymorphism กนกวรรธน์ เซี่ยงเจ็น
Recursive Method.
Function ธนวัฒน์ แซ่ เอียบ. What is a function ฟังก์ชันในภาษา C เป็นโปรแกรมที่ถูกออกแบบมาเพื่อ ใช้แก้ปัญหางานใดงานหนึ่งโดยเฉพาะ ฟังก์ชันจะเปลี่ยน input.
รูปแบบของการใช้คำสั่ง for for( ตัวแปร = ค่าเริ่มต้น ; นิพจน์ตรรกะ ; ค่าเพิ่มหรือค่าลด ) { statement(s); } ตัวอย่าง กรณีกำหนดการวนซ้ำเป็นค่าคงที่ For(n=1;n
บทที่ 3 การสืบทอด (Inheritance)
ผลที่ได้จากการสอบสวน 1. เกิดเหตุการณ์อะไรขึ้น 2. ทำไมถึงเกิดเหตุการณ์นั้น 3. ใครจะเป็นผู้ที่เหมาะสมที่สุดในการแก้ไขป้องกันอุบัติเหตุในครั้งนี้
ผ้าห่มไม่หายแค่ใส่ตัวเลข ปัญหาและการปรับปรุงแก้ไข
การเขียนชื่อ “เรื่อง”
การเขียน Concept Paper & Full Proposal
บทที่ 10 การนำระบบไปใช้/การบำรุงรักษาระบบ
บทที่ 3 การวิเคราะห์ตลาดและคู่แข่งขัน
การทำงานเชิงวิเคราะห์
วิทยาศาสตร์ และ กระบวนการทางวิทยาศาสตร์
การเขียนหนังสือราชการและการโต้ตอบ นักจัดการงานทั่วไปชำนาญการ
SERVICE MARKETING พฤติกรรมลูกค้าในตลาดบริการ • กระบวนการตัดสินใจซื้อสำหรับผลิตภัณฑ์บริการ • บทบาทของลูกค้าที่เกี่ยวข้องในการบริการ.
คณะบริหารธุรกิจ วิทยาเขตกล้วยน้ำไท bu.ac.th
Java Translation Object and Class ในมุมมองคอมพิวเตอร์ Objects หรือ Instances หมายถึงวัตถุที่กำเนิดตัวตนจริงๆจากต้นแบบที่กำหนดโดยคลาส Object.
Contents Contents Introduction Objectives Conceptual frame work
ขั้นตอนในการพัฒนาบท หนังสั้น
นพ.ทวีทรัพย์ ศิรประภาศิริ สำนักงานคณะกรรมการผู้ทรงคุณวุฒิ
การใช้เครื่องมือในงานช่างยนต์
พลตำรวจตรี สุรัตน์ อรุณฤกษ์ถวิล อาจารย์ (สบ
Why’s KM ?.
ภาพรวมของการออกแบบสิ่งพิมพ์
วิทยาลัยเทคโนโลยีพณิชยการเชียงใหม่
สื่อการเรียนรู้ครูต้อม ประโยคควรรู้ในครอบครัว2 รายวิชาภาษาอังกฤษ ขั้นประถมศึกษาปีที่ 1-3 นางสาวลัดดาวัลย์ เขื่อนคำ ครูผู้สอน โรงเรียนสันมหาพนวิทยา อำเภอแม่แตง.
รายวิชา งานไฟฟ้าเบื้องต้นสำหรับครูอุตสาหกรรมศิลป์
อ.พรพนา ปัญญาสุข วิทยาลัยเทคโนโลยีพณิชยการเชียงใหม่ ผู้วิจัย
บทที่ 3 พฤติกรรมลูกค้าในตลาดบริการ • กระบวนการตัดสินใจซื้อสำหรับผลิตภัณฑ์บริการ • บทบาทของลูกค้าที่เกี่ยวข้องในการบริการ.
การเขียนโปรแกรมคอมพิวเตอร์ แบบภาษาเชิงวัตถุ
Class Diagram.
ECS คุณภาพ โรงพยาบาลยางสีสุราช ปี 2561 (ไตรมาสที่ 1)
นางสาวอรไท แซ่จิว สำนักงานป้องกันควบคุมโรคที่ 12 จังหวัดสงขลา
ใบสำเนางานนำเสนอ:

What is a method? Method คือ คำสั่งที่เกิดจากกลุ่มของโค้ด คำสั่งที่รวมกันอยู่ภายใต้คำสั่งเดียว เพื่อให้คำสั่งใหม่ ที่ได้นั้นทำงานได้ตามวัตถุประสงค์ ประโยชน์ของ method เมื่อ programmer ได้รับ method มา method นั้น จะเป็น service ให้กับ programmer ในการแก้ไขปัญหา โดยที่ programmer ไม่จำเป็นต้องทราบรายละเอียด ภายใน method

กลไกการทำงาน 1. รับค่ามาจากผู้ใช้งาน (Programmer) 2. ประมวลผล 3. ส่งค่ากลับมาในตำแหน่งที่เรียก แนวคิดการทำงานของ method Concept การใช้งาน method คือ ให้เรามอง method เป็น กล่องดำ (Black box) โดยให้เราสนใจ เพียงว่า สิ่งที่เราต้องส่งให้ method คืออะไร ? method ทำอะไร ? ค่าที่ method ส่งกลับมาคืออะไร ?

แนวคิดการทำงานของ Method( ต่อ ) { int x; for(int i=0;i<x;i++){ ………………….. } Method ค่าที่ส่งให้ method ค่าที่ Method ส่งกลับ

วิเคราะห์ Method readInt() สิ่งที่เราต้องส่งให้ method readInt() คืออะไร ? method readInt() ทำอะไร ? ค่าที่ method readInt() ส่งกลับมาคืออะไร ? readInt(…….){ …………………….. } ข้อความที่ต้องการ แสดง เลขจำนวนเต็ม จาก keyboard

ตัวอย่างโปรแกรมที่ 1 import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); }

ตัวอย่างโปรแกรมที่ 1 import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } x = ?

ตัวอย่างโปรแกรมที่ 1 import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } x = ? ? 5 5

ตัวอย่างโปรแกรมที่ 1 import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } x = 17 ? 5

ตัวอย่างโปรแกรมที่ 1 import acm.program.*; public class Program001 extends ConsoleProgram { public void run() { int x; x=2+readInt(“?”)*3; println(“x = ”+x); } x = 17 ? 5 x = 17

ประเภทของ method 1. Medthod สำเร็จรูป Medthod สำเร็จรูป คือ method ที่มีใน Compiler หรือ Package สำเร็จรูป ที่โปรแกรมดึงมา ใช้ ( ผ่านการ import) 2. Method ที่ผู้ใช้นิยามขึ้นเอง การสร้าง method ขึ้นมาใช้งานเองเป็นเทคนิค การเขียนโปรแกรมขนาดใหญ่ โดยแบ่งงานขนาด ใหญ่ออกเป็นงานย่อยที่มีขนาดเล็กลงแล้วจัดการงาน ย่อยเหล่านั้นให้อยู่ในรูปของ method

1. Method สำเร็จรูป Method สำเร็จรูปในบางครั้ง Method เหล่านี้ จะถูกจัดรวมกันเป็นกลุ่มในรูปแบบของ class ลักษณะการเรียกใช้ Method ที่ถูกเขียนอยู่ใน class class_name. method_name (argument_list)

method ใน class Math Math.abs(x)Returns the absolute value of x Math.sqrt(x)Returns the correctly rounded positive square root of x Math.pow(x,y)Returns the value of x raised to the power of y. Math.log(x)Returns the natural logarithm (base e) of x Math.log10(x)Returns the base 10 logarithm of x. Math.sin(x)Returns the trigonometric sine of an angle. Math.cos(x)Returns the trigonometric cosine of an angle. Math.tan(x)Returns the trigonometric tangent of an angle.

ตัวอย่างโปรแกรมที่ 2 import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); }

ตัวอย่างโปรแกรมที่ 2 import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } a=3 b=4

ตัวอย่างโปรแกรมที่ 2 import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } a=3 b=4 c=?

ตัวอย่างโปรแกรมที่ 2 import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } a=3 b=4 c=? 25

ตัวอย่างโปรแกรมที่ 2 import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } a=3 b=4 c=? 5.0

ตัวอย่างโปรแกรมที่ 2 import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } a=3 b=4 c=5.0

ตัวอย่างโปรแกรมที่ 2 import acm.program.*; public class Program002 extends ConsoleProgram { public void run() { int a=3,b=4; double c; c=Math.sqrt(a*a+b*b); println(“c = ”+c); } a=3 b=4 c=5.0

2. Method ที่ผู้ใช้นิยามขึ้นเอง Method ที่ผู้ใช้นิยามขึ้นเอง จำแนกได้ 3 แบบ 2.1 Method ที่ไม่มีการรับค่าและส่งค่ากลับ 2.2 Method ที่มีการรับค่าแต่ไม่ส่งค่ากลับ 2.3 Method ที่มีการรับค่าและส่งค่ากลับ

2.1 Method ที่ไม่มีการรับค่าและ ส่งค่ากลับ รูปแบบทั่วไปของการนิยาม method ที่ไม่มีการส่งค่าและรับค่ากลับ scope void method_name () { statements …………… } * Scopepublic

ตัวอย่างโปรแกรมที่ 3 import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); }

ตัวอย่างโปรแกรมที่ 3 import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); }

ตัวอย่างโปรแกรมที่ 3 import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } i=0

ตัวอย่างโปรแกรมที่ 3 import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } i=0 *

ตัวอย่างโปรแกรมที่ 3 import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } i=1 **

ตัวอย่างโปรแกรมที่ 3 import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } i=20 ********************

ตัวอย่างโปรแกรมที่ 3 import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } ********************

ตัวอย่างโปรแกรมที่ 3 import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } ******************** Wisuwat Sunhem

ตัวอย่างโปรแกรมที่ 3 import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } ******************** Wisuwat Sunhem KMITL

ตัวอย่างโปรแกรมที่ 3 import acm.program.*; public class Program003 extends ConsoleProgram { public void run() { print20Star(); println(“Wisuwat Sunhem”); println(“KMITL); print20Star(); } public void print20Star(){ for(int i=0;i<20;i++) print(“*”); println(); } ******************** Wisuwat Sunhem KMITL ********************

2.2 Method ที่มีการรับค่าแต่ไม่ส่ง ค่ากลับ รูปแบบทั่วไปของการนิยาม method ที่มีการรับค่าแต่ไม่ส่งค่ากลับ scope void method_name (parameter_list) { statements …………… }

ตัวอย่าง Parameter_list หากมีการนิยาม method ในลักษณะ public void method_name(int v1,int v2,char v3) { ………………………………….. } ___________________________________________ หากมีการเรียกใช้ method ในลักษณะ ดังนี้ method_name(24, -2, ‘A’); ค่าของตัวแปร parameter จะมีค่าดังนี้ v1=24, v2=-2, v3=‘A’

Do you know it? * ** *** **** *****

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); }

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=1

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=1 #n=1 #ch=‘*’

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=1 #n=1 #ch=‘*’ $i=0->1 *

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=1 #n=1 #ch=‘*’ *

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=2 *

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=2 #n=2 #ch=‘*’ *

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=2 #n=2 #ch=‘*’ $i=0->2 * **

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=2 #n=2 #ch=‘*’ * **

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=3 * **

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=3 #n=3 #ch=‘*’ * **

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=3 #n=3 #ch=‘*’ $i=0->3 * ** ***

ตัวอย่างโปรแกรมที่ 4 import acm.program.*; public class Program004 extends ConsoleProgram { public void run() { for(int i =1;i<=3;i++){ printStar(i, ‘*’); } public void printStar(int n,char ch){ for(int i=0;i<n;i++) print(ch); println(); } i=3 #n=3 #ch=‘*’ * ** ***

2.3 Method ที่มีการรับค่าและส่ง ค่ากลับ รูปแบบทั่วไปของการนิยาม method ที่มีการรับค่าและส่งค่ากลับ scope method_type method_name (parameter_list) { statements …………… return value; } * method_type Data_type (int, double, …)

return value การ return ค่ากลับของ method สามารถอยู่ใน ลักษณะของ Expression ที่มี data_type -> method_type เช่น method_type เป็น boolean สามารถ return เป็น true,false,(4+4)==(3/2) int สามารถ return เป็น 5, x(int type), 5*2+4-4

what is GCD? (Greatest common divisor)

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; }

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=78 #y=33

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=78 #y=33 #r=12

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=78 #y=33 #r=12

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=33 #y=33 #r=12

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=33 #y=12 #r=12

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=33 #y=12 #r=9

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=33 #y=12 #r=9

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=12 #y=12 #r=9

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=12 #y=9 #r=9

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=12 #y=9 #r=3

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=12 #y=9 #r=3

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=9 #y=9 #r=3

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=9 #y=3 #r=3

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=9 #y=3 #r=0

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=9 #y=3 #r=0

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 #x=9 #y=3 #r=0

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 3

ตัวอย่างโปรแกรมที่ 5 import acm.program.*; public class Program005 extends ConsoleProgram { public void run() { int x = 78, b = 33; pritnln(gcd(x,b)); } public int gcd(int x,int y){ int r = x % y; while(r!=0) { x = y; y = r; r = x % y; } return y; } x=78 b=33 3 3

How to program efficiently

Predicate Method return (a*b == a+b) if(a*b == a+b) return true; else return false; 1. การคืนค่ากลับของพรีดิเคทเมธอดสามารถทำ ได้ ดังนี้

if(isPrime(n)){ statement1; statement2; ………………. } if(isPrime(n)==true){ statement1; statement2; ………………. } 2. การเรียกใช้งานพรีดิเคทเมธอด ภายใต้คำสั่ง if สามารถ ทำได้ ดังนี้

การตรวจสอบว่าเลขมีกี่หลัก ? int n=0; while(true){ if(num==0) break; num/=10; n++; } การตรวจสอบจำนวนหลักของตัวเลขทำได้ดังนี้ กำหนดให้ num เป็นตัวแปรชนิดจำนวนเต็มที่มีค่า มากกว่า 0 int n=0; if(num!=0) n=(int)Math.log10(num)+1;

การตัดเลขโดดด้านหลังออกจาก จำนวนเต็ม num/100.0 กำหนดให้ num = 2632 เป็นตัวแปรชนิดจำนวนเต็ม num/ int temp=num/(int)Math.pow(10,n); //n: เลขชี้กำลัง