Method

76
Method

description

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

Transcript of Method

Page 1: Method

Method

Page 2: Method

What is a method?Method คื�อ คื��สั่��งที่��เกิ�ดจ�กิกิลุ่��มของโคื�ดคื��สั่��งที่��

รวมกิ�นอยู่��ภ�ยู่ใต้�คื��สั่��งเด�ยู่ว เพื่��อให้�คื��สั่��งให้ม�ที่��ได�น� #นที่��ง�นได�ต้�มว�ต้ถุ�ประสั่งคื'

ประโยู่ชน'ของ method เม��อ programmer ได�ร�บ method ม� method

น�#นจะเป*น service ให้�กิ�บ programmer ในกิ�รแกิ�ไข ป,ญห้� โดยู่ที่�� programmer ไม�จ��เป*นต้�องที่ร�บร�ยู่

ลุ่ะเอ�ยู่ดภ�ยู่ใน method

Page 3: Method

กิลุ่ไกิกิ�รที่��ง�น1. ร�บคื��ม�จ�กิผู้��ใช�ง�น(Programmer)2. ประมวลุ่ผู้ลุ่3. สั่�งคื��กิลุ่�บม�ในต้��แห้น�งที่��เร�ยู่กิ

แนวคื�ดกิ�รที่��ง�นของ methodConcept กิ�รใช�ง�น method คื�อ ให้�เร�มอง

method เป*น กิลุ่�องด�� (Black box) โดยู่ให้�เร�สั่นใจ เพื่�ยู่งว��

สั่��งที่��เร�ต้�องสั่�งให้�method คื�ออะไร? method ที่��อะไร? คื��ที่�� method สั่�งกิลุ่�บม�คื�ออะไร?

Page 4: Method

แนวคื�ดกิ�รที่��ง�นของ Method(ต้�อ)

{ int x; for(int i=0;i<x;i++){ …………………..}

Method

คื��ที่��สั่�งให้�method

คื��ที่�� Method สั่�งกิลุ่�บ

Page 5: Method

ว�เคืร�ะห้'Method readInt() สั่��งที่��เร�ต้�องสั่�งให้�method readInt() คื�ออะไร? method readInt()ที่��อะไร? คื��ที่�� method readInt() สั่�งกิลุ่�บม�คื�ออะไร?

readInt(…….){ …………………….. …………………….. ……………………..}

ข�อคืว�มที่��ต้�องกิ�รแสั่ดง

เลุ่ขจ��นวนเต้/ม จ�กิkeyboard

Page 6: Method

ต้�วอยู่��งโปรแกิรมที่�� 1import acm.program.*; public class Program001 extends

ConsoleProgram { public void run() {

int x;x=2+readInt(“?”)*3;println(“x = ”+x);

} }

Page 7: Method

ต้�วอยู่��งโปรแกิรมที่�� 1import acm.program.*; public class Program001 extends

ConsoleProgram { public void run() {

int x;x=2+readInt(“?”)*3;println(“x = ”+x);

} }

x = ?

Page 8: Method

ต้�วอยู่��งโปรแกิรมที่�� 1import acm.program.*; public class Program001 extends

ConsoleProgram { public void run() {

int x;x=2+readInt(“?”)*3;println(“x = ”+x);

} }

x = ?

? 5

5

Page 9: Method

ต้�วอยู่��งโปรแกิรมที่�� 1import acm.program.*; public class Program001 extends

ConsoleProgram { public void run() {

int x;x=2+readInt(“?”)*3;println(“x = ”+x);

} }

x = 17

? 5

Page 10: Method

ต้�วอยู่��งโปรแกิรมที่�� 1import acm.program.*; public class Program001 extends

ConsoleProgram { public void run() {

int x;x=2+readInt(“?”)*3;println(“x = ”+x);

} }

x = 17

? 5x = 17

Page 11: Method

ประเภที่ของ method1 . Medthod สั่��เร/จร�ป

Medthod สั่��เร/จร�ป คื�อ method ที่��ม�ในCompiler ห้ร�อ Package สั่��เร/จร�ป ที่��โปรแกิรมด0ง

ม�ใช� ( ผู้��นกิ�ร import)

2. Method ที่��ผู้��ใช�น�ยู่�มข0#นเอง กิ�รสั่ร��ง method ข0#นม�ใช�ง�นเองเป*นเที่คืน�คื

กิ�รเข�ยู่นโปรแกิรมขน�ดให้ญ� โดยู่แบ�งง�นขน�ดให้ญ�ออกิเป*นง�นยู่�อยู่ที่��ม�ขน�ดเลุ่/กิลุ่งแลุ่�วจ�ดกิ�รง�นยู่�อยู่

เห้ลุ่��น�#นให้�อยู่��ในร�ปของ method

Page 12: Method

1. Method สั่��เร/จร�ปMethod สั่��เร/จร�ปในบ�งคืร�#ง Method เห้ลุ่��น�#

จะถุ�กิจ�ดรวมกิ�นเป*นกิลุ่��มในร�ปแบบของ class

ลุ่�กิษณะกิ�รเร�ยู่กิใช�Method ที่��ถุ�กิเข�ยู่นอยู่��ในclass

class_name. method_name (argument_list)

Page 13: Method

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.

Page 14: Method

ต้�วอยู่��งโปรแกิรมที่�� 2import 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);

} }

Page 15: Method

ต้�วอยู่��งโปรแกิรมที่�� 2import 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=3b=4

Page 16: Method

ต้�วอยู่��งโปรแกิรมที่�� 2import 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=3b=4c=?

Page 17: Method

ต้�วอยู่��งโปรแกิรมที่�� 2import 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=3b=4c=?

25

Page 18: Method

ต้�วอยู่��งโปรแกิรมที่�� 2import 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=3b=4c=?

5.0

Page 19: Method

ต้�วอยู่��งโปรแกิรมที่�� 2import 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=3b=4c=5.0

Page 20: Method

ต้�วอยู่��งโปรแกิรมที่�� 2import 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=3b=4c=5.0

c = 5.0

Page 21: Method

2. Method ที่��ผู้��ใช�น�ยู่�มข0#นเอง

Method ที่��ผู้��ใช�น�ยู่�มข0#นเอง จ��แนกิได� 3 แบบ

2.1 Method ที่��ไม�ม�กิ�รร�บคื��แลุ่ะสั่�งคื��กิลุ่�บ

2.2 Method ที่��ม�กิ�รร�บคื��แต้�ไม�สั่�งคื��กิลุ่�บ

2.3 Method ที่��ม�กิ�รร�บคื��แลุ่ะสั่�งคื��กิลุ่�บ

Page 22: Method

2.1 Method ที่��ไม�ม�กิ�รร�บคื��แลุ่ะสั่�งคื��กิลุ่�บ

ร�ปแบบที่��วไปของกิ�รน�ยู่�มmethod ที่��ไม�ม�กิ�รสั่�งคื��แลุ่ะร�บคื��กิลุ่�บ   

scope void method_name () {

statements……………

}

* Scope public

Page 23: Method

ต้�วอยู่��งโปรแกิรมที่�� 3import 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();

}}

Page 24: Method

ต้�วอยู่��งโปรแกิรมที่�� 3import 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();

}}

Page 25: Method

ต้�วอยู่��งโปรแกิรมที่�� 3import 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

Page 26: Method

ต้�วอยู่��งโปรแกิรมที่�� 3import 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

*

Page 27: Method

ต้�วอยู่��งโปรแกิรมที่�� 3import 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

**

Page 28: Method

ต้�วอยู่��งโปรแกิรมที่�� 3import 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

********************

Page 29: Method

ต้�วอยู่��งโปรแกิรมที่�� 3import 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();

}}

********************

Page 30: Method

ต้�วอยู่��งโปรแกิรมที่�� 3import 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

Page 31: Method

ต้�วอยู่��งโปรแกิรมที่�� 3import 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 SunhemKMITL

Page 32: Method

ต้�วอยู่��งโปรแกิรมที่�� 3import 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 SunhemKMITL********************

Page 33: Method

2.2 Method ที่��ม�กิ�รร�บคื��แต้�ไม�สั่�งคื��กิลุ่�บ

ร�ปแบบที่��วไปของกิ�รน�ยู่�มmethod ที่��ม�กิ�รร�บคื��แต้�ไม�สั่�งคื��กิลุ่�บ

scope void method_name (parameter_list) {

statements……………

}

Page 34: Method

ต้�วอยู่��ง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’

Page 35: Method

Do you know it?

***************

Page 36: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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();

}}

Page 37: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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

Page 38: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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=‘*’

Page 39: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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

*

Page 40: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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=‘*’

*

Page 41: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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

*

Page 42: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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=‘*’

*

Page 43: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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

***

Page 44: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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=‘*’

***

Page 45: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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

***

Page 46: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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=‘*’

***

Page 47: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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

******

Page 48: Method

ต้�วอยู่��งโปรแกิรมที่�� 4import 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=‘*’

******

Page 49: Method

2.3 Method ที่��ม�กิ�รร�บคื��แลุ่ะสั่�งคื��กิลุ่�บ

ร�ปแบบที่��วไปของกิ�รน�ยู่�มmethod ที่��ม�กิ�รร�บคื��แลุ่ะสั่�งคื��กิลุ่�บ

scope method_type method_name (parameter_list) {

statements……………return value;

}

* method_type Data_type(int, double, …)

Page 50: Method

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

Page 51: Method

what is GCD?(Greatest common

divisor)

Page 52: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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;

}}

Page 53: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33

Page 54: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=78#y=33

Page 55: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=78#y=33#r=12

Page 56: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=78#y=33#r=12

Page 57: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=33#y=33#r=12

Page 58: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=33#y=12#r=12

Page 59: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=33#y=12#r=9

Page 60: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=33#y=12#r=9

Page 61: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=12#y=12#r=9

Page 62: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=12#y=9#r=9

Page 63: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=12#y=9#r=3

Page 64: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=12#y=9#r=3

Page 65: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=9#y=9#r=3

Page 66: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=9#y=3#r=3

Page 67: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=9#y=3#r=0

Page 68: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=9#y=3#r=0

Page 69: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33#x=9#y=3#r=0

Page 70: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=333

Page 71: Method

ต้�วอยู่��งโปรแกิรมที่�� 5import 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=78b=33

3

3

Page 72: Method

How to program efficiently

Page 73: Method

Predicate Method

return (a*b == a+b)

if(a*b == a+b) return true;

else return false;

1. กิ�รคื�นคื��กิลุ่�บของพื่ร�ด�เคืที่เมธอดสั่�ม�รถุที่��ได�ด�งน�#

Page 74: Method

if(isPrime(n)){statement1;statement2;……………….

}

if(isPrime(n)==true){ statement1;statement2;……………….

}

2. กิ�รเร�ยู่กิใช�ง�นพื่ร�ด�เคืที่เมธอด ภ�ยู่ใต้�คื��สั่��ง if สั่�ม�รถุ ที่��ได� ด�งน�#

Page 75: Method

การตรวจสอบวาเลขมี�ก��หล�ก?

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;

Page 76: Method

การต�ดเลขโดดด�านหล�งออกจากจ�านวนเต�มี

num/100.0

กิ��ห้นดให้� num = 2632 เป*นต้�วแปรชน�ดจ��นวนเต้/ม

26.32

num/100 26

int temp=num/(int)Math.pow(10,n); //n:เลุ่ขช�#กิ��ลุ่�ง