Method part2

Preview:

Citation preview

MethodMethodOverloadingOverloading

Method แบบOverloadingMethod แบบOverloading

กรณี�ที่�� 1

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

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

Method แบบOverloadingMethod แบบOverloading

กรณี�ที่�� 1

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

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

Method แบบOverloadingMethod แบบOverloading

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

b){...}

Method แบบOverloadingMethod แบบOverloading

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

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

Method แบบOverloadingMethod แบบOverloading

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

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

Overloading

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

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

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

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

Menu 1 : Add 2 Number

Menu 2 : Add 3 Number

Menu 3 : Exit

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

Enter menu:

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:

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

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 ค่�าน� นไป็

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);

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);

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);

MethodMethodMath.random( )Math.random( )

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

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

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

{

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

} ???

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

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

{

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

} 0<= X >1

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

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

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

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

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

???

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 มิาบว่ก เพราะ ?

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 มิาบว่ก เพราะ ?

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

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

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

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

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

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

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

MethodMethodRecursiveRecursive

Method RecursiveMethod Recursive

main

mAdd

mDisplay

mInput

Recursive

Method RecursiveMethod Recursive

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

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

ค่�า

Method RecursiveMethod Recursive

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

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

5 + 4 + 3 + 2 + 1

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;}

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;}

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

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

Enter num1: 5Enter num2: 10Sum : 45

5 + 6 + 7 + 8 + 9 + 10

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;}

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;}