33
Method Overloading

Method part2

Embed Size (px)

Citation preview

Page 1: Method part2

MethodMethodOverloadingOverloading

Page 2: Method part2

Method แบบOverloadingMethod แบบOverloading

กรณี�ที่�� 1

public static int Add(int a, int b){...}

public static int Add(int a, int b, int c){...}

Page 3: Method part2

Method แบบOverloadingMethod แบบOverloading

กรณี�ที่�� 1

public static int Add(int a, int b){...}

public static int Add(int a, int b, int c){...} พารามิ�เตอร�ไมิ�เที่�าก�น

Page 4: Method part2

Method แบบOverloadingMethod แบบOverloading

กรณี�ที่�� 2public static int Add(int a, int b){...}public static double Add(double a, double

b){...}

Page 5: Method part2

Method แบบOverloadingMethod แบบOverloading

กรณี�ที่�� 2public static int Add(int a, int b){...}public static double Add(double a, double

b){...}ชน�ดของพารามิ�เตอร�ต�างก�น

Page 6: Method part2

Method แบบOverloadingMethod แบบOverloading

ให้�น�กศึ�กษาสร�าง class ข� นมิาให้มิ� ให้�ช!�อว่�า TestMOverload

เพ!�อที่#าการห้าค่�าผลบว่กของต�ว่เลข โดย ให้�มิ�การที่#างานผ�าน Method แบบ

Overloading

Page 7: Method part2

Method แบบOverloadingMethod แบบOverloading ให้�มิ�การร�บค่�าข�อมิ)ลเป็+น int

โดยให้�ผ)�ใช�สามิารถเล!อกได�ว่�า จะร�บแบบก��จ#านว่น

และสามิารถว่นร�บก��รอบก/ได�จนกว่�าจะกด 3

=========Menu=========

Menu 1 : Add 2 Number

Menu 2 : Add 3 Number

Menu 3 : Exit

======================

Enter menu:

Page 8: Method part2

Method แบบOverloadingMethod แบบOverloadingpublic class TestMOverload {

public static void main(String[] agrs){

.. สร�าง Menu ต�างๆ..

.. ร�บค่�า int ส#าห้ร�บต�ว่เล!อกกด..

}

}

=========Menu=========Menu 1 : Add 2 NumberMenu 2 : Add 3 NumberMenu 3 : Exit======================Enter menu:

Page 9: Method part2

Method แบบOverloadingMethod แบบOverloadingpublic class TestMOverload {

public static void main(String[] agrs){

.. สร�าง Menu ต�างๆ..

.. ร�บค่�า int ส#าห้ร�บต�ว่เล!อกกด..

}

}

=========Menu=========Menu 1 : Add 2 NumberMenu 2 : Add 3 NumberMenu 3 : Exit======================Enter menu:

ที่#าอย�างไรให้�กด 1, 2 แล�ว่ ที่#างานต�อ

แต�ถ�ากด 3 เมิ!�อไห้ร�ให้�ห้ล1ดLoop

Page 10: Method part2

Method แบบOverloadingMethod แบบOverloadingpublic class TestMOverload {

public static void main(String[] agrs){

.. สร�าง Menu ต�างๆ..

.. ร�บค่�า int ส#าห้ร�บต�ว่เล!อกกด..

}

}

=========Menu========Menu 1 : Add 2 NumberMenu 2 : Add 3 NumberMenu 3 : Exit=====================Enter menu: 1

ถ�ากด 1 ให้�มิ�การร�บค่�า 2 ค่�า

และให้�มิ�การเร�ยกใช�Method Add

แบบร�บ 2 พารามิ�เตอร� โดยส�ง 2 ค่�าน� นไป็

Page 11: Method part2

Method แบบOverloadingMethod แบบOverloadingpublic static int Add(int n1, int

n2){

return (n1+n2);

}

=========Menu========Menu 1 : Add 1 NumberMenu 2 : Add 2 NumberMenu 3 : Exit=====================Enter menu:

ถ�ากด 1... ร�บค่�า 2 ค่�า (num1 และ num2)...int sum_menu1 = Add(num1, num2);

Page 12: Method part2

Method แบบOverloadingMethod แบบOverloadingpublic static int Add(int n1, int n2,

int n3){

return (n1+n2+n3);

}

=========Menu========Menu 1 : Add 1 NumberMenu 2 : Add 2 NumberMenu 3 : Exit=====================Enter menu:

ถ�ากด 2... ร�บค่�า 3 ค่�า (num1, num2 และ num3)...int sum_menu2 = Add(num1, num2, num3);

Page 13: Method part2

Method แบบOverloadingMethod แบบOverloading

public static int Add(int n1, int n2){

return (n1+n2);

}

public static int Add(int n1, int n2, int n3){

return (n1+n2+n3);

}

int sum_menu1 = Add(num1, num2);

int sum_menu1 = Add(num1, num2, num3);

Page 14: Method part2

MethodMethodMath.random( )Math.random( )

Page 15: Method part2

Method Math.random()Method Math.random()

ให้�น�กศึ�กษาสร�าง class ข� นมิาให้มิ� ให้�ช!�อว่�า TestMRandom

Page 16: Method part2

Method Math.random()Method Math.random()for(int i=1 ; i<=30 ; i++)

{

System.out.println(“num”+ i + “=” + Math.random());

} ???

Page 17: Method part2

Method Math.random()Method Math.random()

for(int i=1 ; i<=30 ; i++)

{

System.out.println(“num”+ i + “=” + Math.random());

} 0<= X >1

Page 18: Method part2

Method Math.random()Method Math.random()

ให้�น�กศึ�กษาที่#าการ Random ค่�า30 ค่ร� ง

ต� งแต�ค่�า 1-10 พร�อมิที่� งแสดงผลออกมิา

Page 19: Method part2

Method Math.random()Method Math.random()

for(int i=0 ; i<30 ; i++)

{

int value = 1 + (int)(Math.random() * 10);

System.out.println(“num”+ i + “=”+ value);

}

0<= X >1

Page 20: Method part2

Method Math.random()Method Math.random()

for(int i=0 ; i<30 ; i++)

{

int value = 1 + (int)(Math.random() * 10);

System.out.println(“num”+ i + “=”+ value);

}

0<= X >1

???

Page 21: Method part2

Method Math.random()Method Math.random()

for(int i=0 ; i<30 ; i++)

{

int value = 1 + (int)(Math.random() * 10);

System.out.println(“num”+ i + “=”+ value);

}

0<= X >1

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

เอา 1 มิาบว่ก เพราะ ?

Page 22: Method part2

Method Math.random()Method Math.random()

for(int i=0 ; i<30 ; i++){

int value = 1 + (int)(Math.random() * 10);

System.out.println(“num”+ i + “=”+ value);

}

0<= X >1

0, 1, 2, 3, 4, 5, 6, 7, 8, 91, 2, 3, 4, 5, 6, 7, 8, 9, 10

เอา 1 มิาบว่ก เพราะ ?

Page 23: Method part2

Method Math.random()Method Math.random()

ให้�น�กศึ�กษาที่#าการ Random ค่�า30 ค่ร� ง

ต� งแต�ค่�า 5-10 พร�อมิที่� งแสดงผลออกมิาจะที่#าไงด�???

Page 24: Method part2

Method Math.random()Method Math.random()

ให้�น�กศึ�กษาที่#าการ Random ค่�า30 ค่ร� ง

ต� งแต�ค่�า 5-10 พร�อมิที่� งแสดงผลออกมิา

start + (int)(Math.random() * (end-start+1))

Page 25: Method part2

MethodMethodRecursiveRecursive

Page 26: Method part2

Method RecursiveMethod Recursive

main

mAdd

mDisplay

mInput

Recursive

Page 27: Method part2

Method RecursiveMethod Recursive

ให้�น�กศึ�กษาสร�าง class ข� นมิาให้มิ�

ให้�ช!�อว่�า TestMRecursive และให้�ที่#าการร�บค่�า int จ#านว่น 1

ค่�า

Page 28: Method part2

Method RecursiveMethod Recursive

ให้�สร�าง Method แบบRecursive ส#าห้ร�บบว่กต�ว่เลข

ต� งแต�ต�ว่เลขที่��ร �บเข�ามิาถ�ง 1Enter num1: 5Sum : 15

5 + 4 + 3 + 2 + 1

Page 29: Method part2

Method RecursiveMethod Recursivepublic static void main(String[] args){... ร�บค่�า num1...int sum = Sumation(num1);System.out.println(“sum :”+sum);}

public static int Sumation(int n1){int s=0;for(int i=n1;i>=1;i--){

s = s + i;}

return s;}

Page 30: Method part2

Method RecursiveMethod Recursivepublic static int Sumation(int n1){if(n >1){

return(n1 + Sumation(n1 - 1));}else return(1); } public static int Sumation(int

n1){int s=0;for(int i=n1;i>=1;i--){

s = s + i;}return s;}

Page 31: Method part2

Method RecursiveMethod Recursive ให้�แก�ไข Method แบบ Recursive

ส#าห้ร�บบว่กต�ว่เลขโดยสามิารถร�บค่�าเร��มิต�นและส� นส1ดได�

Enter num1: 5Enter num2: 10Sum : 45

5 + 6 + 7 + 8 + 9 + 10

Page 32: Method part2

Method RecursiveMethod Recursivepublic static void main(String[] args){... ร�บค่�า num1, num2...int sum = Sumation(num1,num2);System.out.println(“sum :”+sum);}

public static int Sumation(int n1,int n2){int s=0;for(int i=n1;i<=n2;i++){

s = s + i;}return s;}

Page 33: Method part2

Method RecursiveMethod Recursivepublic static int Sumation(int n1,int n2){if(???){

return(??? + Sumation(???));}else return(???); }

public static int Sumation(int n1,int n2){int s=0;for(int i=n1;i<=n2;i++){

s = s + i;}return s;}