Method

Post on 02-Jan-2016

23 views 0 download

description

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

Transcript of Method

Method

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

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

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

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

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

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

แนวคื�ดกิ�รที่��ง�นของ methodConcept กิ�รใช�ง�น 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

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

ConsoleProgram { public void run() {

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

} }

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

ConsoleProgram { public void run() {

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

} }

x = ?

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

ConsoleProgram { public void run() {

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

} }

x = ?

? 5

5

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

ConsoleProgram { public void run() {

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

} }

x = 17

? 5

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

ConsoleProgram { public void run() {

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

} }

x = 17

? 5x = 17

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

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

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

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

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

เห้ลุ่��น�#นให้�อยู่��ในร�ปของ 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.

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

} }

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

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

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

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

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

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

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

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

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

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

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

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

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

scope void method_name () {

statements……………

}

* Scope public

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

}}

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

}}

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

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

*

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

**

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

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

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

}}

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

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

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

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

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?

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

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

}}

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

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

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

*

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

*

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

*

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

*

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

***

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

***

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

***

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

***

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

******

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

******

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)

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

}}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 เป*นต้�วแปรชน�ดจ��นวนเต้/ม

26.32

num/100 26

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