SlideShare a Scribd company logo
Method Overloading
Method  แบบ  Overloading กรณีที่  1 public static int  Add (int a, int b){...} public static int  Add (int a, int b, int c){...}
Method  แบบ  Overloading กรณีที่  1 public static int  Add (int a, int b) {...} public static int  Add (int a, int b, int c) {...} พารามิเตอร์ไม่เท่ากัน
Method  แบบ  Overloading กรณีที่  2 public static int  Add (int a, int b){...} public static double  Add (double a, double b){...}
Method  แบบ  Overloading กรณีที่  2 public static  int   Add ( int a, int b ){...} public static  double   Add ( double a, double b ){...} ชนิดของพารามิเตอร์ต่างกัน
Method  แบบ  Overloading ให้นักศึกษาสร้าง  class  ขึ้นมาใหม่ ให้ชื่อว่า  TestMOverload  เพื่อทำการหาค่าผลบวกของตัวเลข โดยให้มีการทำงานผ่าน  Method  แบบ  Overloading
Method  แบบ  Overloading ให้มีการรับค่าข้อมูลเป็น  int   โดยให้ผู้ใช้สามารถเลือกได้ว่า จะรับแบบกี่จำนวน และสามารถวนรับกี่รอบก็ได้จนกว่าจะกด  3 =========Menu========= Menu 1 : Add 2 Number Menu 2 : Add 3 Number Menu 3 : Exit ====================== Enter menu:
Method  แบบ  Overloading public class  TestMOverload  { public static void main(String[] agrs){ .. สร้าง  Menu  ต่างๆ .. .. รับค่า  int  สำหรับตัวเลือกกด .. } } =========Menu========= Menu 1 : Add 2 Number Menu 2 : Add 3 Number Menu 3 : Exit ====================== Enter menu:
Method  แบบ  Overloading public class  TestMOverload  { public static void main(String[] agrs){ .. สร้าง  Menu  ต่างๆ .. .. รับค่า  int  สำหรับตัวเลือกกด .. } } ทำอย่างไรให้กด  1, 2  แล้วทำงานต่อ  แต่ถ้ากด  3  เมื่อไหร่ให้หลุด  Loop =========Menu========= Menu 1 : Add 2 Number Menu 2 : Add 3 Number Menu 3 : Exit ====================== Enter menu:
Method  แบบ  Overloading public class  TestMOverload  { public static void main(String[] agrs){ .. สร้าง  Menu  ต่างๆ .. .. รับค่า  int  สำหรับตัวเลือกกด .. } } ถ้ากด  1  ให้มีการรับค่า  2  ค่า  และให้มีการเรียกใช้  Method Add  แบบรับ  2  พารามิเตอร์ โดยส่ง  2  ค่านั้นไป =========Menu======== Menu 1 : Add 2 Number Menu 2 : Add 3 Number Menu 3 : Exit ===================== Enter menu: 1
Method  แบบ  Overloading public static int  Add(int n1, int n2) { return  (n1+n2) ; } ถ้ากด  1 ... รับค่า  2  ค่า  (num1  และ  num2)... int  sum_menu1  =  Add(num1, num2); =========Menu======== Menu 1 : Add 1 Number Menu 2 : Add 2 Number Menu 3 : Exit ===================== Enter menu:
Method  แบบ  Overloading public static int  Add(int n1, int n2, int n3) { return  (n1+n2+n3) ; } ถ้ากด  2 ... รับค่า  3  ค่า  (num1, num2  และ  num3)... int  sum_menu2  =  Add(num1, num2, num3); =========Menu======== Menu 1 : Add 1 Number Menu 2 : Add 2 Number Menu 3 : Exit ===================== Enter menu:
Method  แบบ  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);
Method Math.random( )
Method Math.random() ให้นักศึกษาสร้าง  class  ขึ้นมาใหม่ ให้ชื่อว่า  TestMRandom
Method Math.random() for(int i=1 ; i<=30 ; i++) { System.out.println(“num”+ i + “=” +   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() ให้นักศึกษาทำการ  Random  ค่า  30  ครั้ง ตั้งแต่ค่า   1-10  พร้อมทั้งแสดงผลออกมา
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() 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() for(int i=0 ; i<30 ; i++) { int value = 1 +  (int)( Math.random()  * 10); System.out.println(“num”+ i + “=”+ value); } 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 0<=  X  >1 เอา  1  มาบวก เพราะ  ?
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, 2, 3, 4, 5, 6, 7, 8, 9, 10 เอา  1  มาบวก เพราะ  ?
Method Math.random() ให้นักศึกษาทำการ  Random  ค่า  30  ครั้ง ตั้งแต่ค่า  5-10   พร้อมทั้งแสดงผลออกมา จะทำไงดี ???
Method Math.random() ให้นักศึกษาทำการ  Random  ค่า  30  ครั้ง ตั้งแต่ค่า  5-10   พร้อมทั้งแสดงผลออกมา start + (int) ( Math.random() * (end-start+1) )
Method Recursive
Method Recursive main mAdd mDisplay mInput Recursive
Method Recursive ให้นักศึกษาสร้าง  class  ขึ้นมาใหม่ ให้ชื่อว่า  TestMRecursive และให้ทำการรับค่า  int  จำนวน  1  ค่า
Method Recursive ให้สร้าง  Method  แบบ  Recursive  สำหรับบวกตัวเลข ตั้งแต่ตัวเลขที่รับเข้ามาถึง  1 Enter num1: 5 Sum :  15 5 + 4 + 3 + 2 + 1
Method Recursive public 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 Recursive public 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 Recursive ให้แก้ไข  Method  แบบ  Recursive  สำหรับบวกตัวเลข โดยสามารถรับค่าเริ่มต้นและสิ้นสุดได้ Enter num1: 5 Enter num2: 10 Sum :  45 5 + 6 + 7 + 8 + 9 + 10
Method Recursive public 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 Recursive public 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; }

More Related Content

PPT
Method JAVA
PDF
เมทริกซ์...
PDF
ลิมิต
PPTX
Computer Programming 3
DOCX
กฎของเลขยกกำลัง
PDF
เฉลยลิมิต
PDF
แบบฝึกทักษะเมทริกซ์ เล่ม 1 ระบบสมการเชิงเส้น เผยแพร่
PDF
Calculus www.clipvidva.com
Method JAVA
เมทริกซ์...
ลิมิต
Computer Programming 3
กฎของเลขยกกำลัง
เฉลยลิมิต
แบบฝึกทักษะเมทริกซ์ เล่ม 1 ระบบสมการเชิงเส้น เผยแพร่
Calculus www.clipvidva.com

What's hot (19)

PDF
ศราวุธ 58670394
PDF
ความสัมพันธ์เชิงฟังก์ชัน
PDF
Statistic1
PDF
เฉลยแคลคูลัส
PPT
Chapter 4 ลิมิตของฟังก์ชัน
PPT
เลขยกกำลังและลอการิทึม
PDF
เนื้อหาเมทริกซ์
PDF
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
PDF
เอกสารแคลคูลัส
PDF
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
PDF
อนุพันธ์
PDF
เฉลยอนุพันธ์
PDF
แบบฝึกทักษะที่ เลขยกกำลัง
PDF
ระบบสมการเชิงเส้น
PPT
Limit
PPT
ลอการิทึม..[1]
PDF
ระบบสมการเชิงเส้นสองตัวแปร
PDF
อินทิเกรต
PDF
ปริพันธ์
ศราวุธ 58670394
ความสัมพันธ์เชิงฟังก์ชัน
Statistic1
เฉลยแคลคูลัส
Chapter 4 ลิมิตของฟังก์ชัน
เลขยกกำลังและลอการิทึม
เนื้อหาเมทริกซ์
แบบฝึกทักษะแคลคูลัสเบื้องต้น สว.กจ
เอกสารแคลคูลัส
ค่าสูงสุดสัมบูรณ์และค่าต่ำสุดสัมบูรณ์ของฟังก์ชัน
อนุพันธ์
เฉลยอนุพันธ์
แบบฝึกทักษะที่ เลขยกกำลัง
ระบบสมการเชิงเส้น
Limit
ลอการิทึม..[1]
ระบบสมการเชิงเส้นสองตัวแปร
อินทิเกรต
ปริพันธ์
Ad

Similar to Method part2 (20)

PDF
Java-Chapter 08 Methods
PDF
Java-Answer Chapter 08-09
PDF
Java-Answer Chapter 12-13
PPTX
เมธอด กลุ่ม3
PDF
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
PDF
งานนำเสนอ1
PDF
Java Programming: อะเรย์และคอลเล็กชั่น
PDF
Applet 5 class_inheritance
PDF
Applet 5 class_inheritance
PDF
Power point
PDF
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
PDF
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
DOC
Chapter3
PDF
Java-Answer Chapter 08-09 (For Print)
PDF
Java-Answer Chapter 10-11
PDF
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริง
PPTX
เมธอด
Java-Chapter 08 Methods
Java-Answer Chapter 08-09
Java-Answer Chapter 12-13
เมธอด กลุ่ม3
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
งานนำเสนอ1
Java Programming: อะเรย์และคอลเล็กชั่น
Applet 5 class_inheritance
Applet 5 class_inheritance
Power point
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Chapter3
Java-Answer Chapter 08-09 (For Print)
Java-Answer Chapter 10-11
บทที่5 ข้อมูลชนิดอาร์เรย์และสตริง
เมธอด
Ad

More from Nitigan Nakjuatong (20)

PDF
วิธีการกำหนดสิทธิให้กับ Directory
PDF
Applet 7 image_j_panel
PDF
662305 LAB13
PDF
662305 LAB12
PDF
Applet 7 image_j_panel
PDF
Applet 6 mouse_keyboard
PDF
Applet 4 class_composition
PDF
PDF
PDF
PDF
Applet 3 design_class_composition
PDF
PDF
Applet 2 container and action_listener
PDF
662305 Lab7new
PDF
New Assingment3 array2D
PDF
Assingment3 array2 d
PDF
PPT
PPT
Control structure
PPT
Set putty to use numeric keyboard in pico
วิธีการกำหนดสิทธิให้กับ Directory
Applet 7 image_j_panel
662305 LAB13
662305 LAB12
Applet 7 image_j_panel
Applet 6 mouse_keyboard
Applet 4 class_composition
Applet 3 design_class_composition
Applet 2 container and action_listener
662305 Lab7new
New Assingment3 array2D
Assingment3 array2 d
Control structure
Set putty to use numeric keyboard in pico

Method part2

  • 2. Method แบบ Overloading กรณีที่ 1 public static int Add (int a, int b){...} public static int Add (int a, int b, int c){...}
  • 3. Method แบบ Overloading กรณีที่ 1 public static int Add (int a, int b) {...} public static int Add (int a, int b, int c) {...} พารามิเตอร์ไม่เท่ากัน
  • 4. Method แบบ Overloading กรณีที่ 2 public static int Add (int a, int b){...} public static double Add (double a, double b){...}
  • 5. Method แบบ Overloading กรณีที่ 2 public static int Add ( int a, int b ){...} public static double Add ( double a, double b ){...} ชนิดของพารามิเตอร์ต่างกัน
  • 6. Method แบบ Overloading ให้นักศึกษาสร้าง class ขึ้นมาใหม่ ให้ชื่อว่า TestMOverload เพื่อทำการหาค่าผลบวกของตัวเลข โดยให้มีการทำงานผ่าน Method แบบ Overloading
  • 7. Method แบบ Overloading ให้มีการรับค่าข้อมูลเป็น int โดยให้ผู้ใช้สามารถเลือกได้ว่า จะรับแบบกี่จำนวน และสามารถวนรับกี่รอบก็ได้จนกว่าจะกด 3 =========Menu========= Menu 1 : Add 2 Number Menu 2 : Add 3 Number Menu 3 : Exit ====================== Enter menu:
  • 8. Method แบบ Overloading public class TestMOverload { public static void main(String[] agrs){ .. สร้าง Menu ต่างๆ .. .. รับค่า int สำหรับตัวเลือกกด .. } } =========Menu========= Menu 1 : Add 2 Number Menu 2 : Add 3 Number Menu 3 : Exit ====================== Enter menu:
  • 9. Method แบบ Overloading public class TestMOverload { public static void main(String[] agrs){ .. สร้าง Menu ต่างๆ .. .. รับค่า int สำหรับตัวเลือกกด .. } } ทำอย่างไรให้กด 1, 2 แล้วทำงานต่อ แต่ถ้ากด 3 เมื่อไหร่ให้หลุด Loop =========Menu========= Menu 1 : Add 2 Number Menu 2 : Add 3 Number Menu 3 : Exit ====================== Enter menu:
  • 10. Method แบบ Overloading public class TestMOverload { public static void main(String[] agrs){ .. สร้าง Menu ต่างๆ .. .. รับค่า int สำหรับตัวเลือกกด .. } } ถ้ากด 1 ให้มีการรับค่า 2 ค่า และให้มีการเรียกใช้ Method Add แบบรับ 2 พารามิเตอร์ โดยส่ง 2 ค่านั้นไป =========Menu======== Menu 1 : Add 2 Number Menu 2 : Add 3 Number Menu 3 : Exit ===================== Enter menu: 1
  • 11. Method แบบ Overloading public static int Add(int n1, int n2) { return (n1+n2) ; } ถ้ากด 1 ... รับค่า 2 ค่า (num1 และ num2)... int sum_menu1 = Add(num1, num2); =========Menu======== Menu 1 : Add 1 Number Menu 2 : Add 2 Number Menu 3 : Exit ===================== Enter menu:
  • 12. Method แบบ Overloading public static int Add(int n1, int n2, int n3) { return (n1+n2+n3) ; } ถ้ากด 2 ... รับค่า 3 ค่า (num1, num2 และ num3)... int sum_menu2 = Add(num1, num2, num3); =========Menu======== Menu 1 : Add 1 Number Menu 2 : Add 2 Number Menu 3 : Exit ===================== Enter menu:
  • 13. Method แบบ 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);
  • 15. Method Math.random() ให้นักศึกษาสร้าง class ขึ้นมาใหม่ ให้ชื่อว่า TestMRandom
  • 16. Method Math.random() for(int i=1 ; i<=30 ; i++) { System.out.println(“num”+ i + “=” + Math.random() ); } ???
  • 17. Method Math.random() for(int i=1 ; i<=30 ; i++) { System.out.println(“num”+ i + “=” + Math.random() ); } 0<= X >1
  • 18. Method Math.random() ให้นักศึกษาทำการ Random ค่า 30 ครั้ง ตั้งแต่ค่า 1-10 พร้อมทั้งแสดงผลออกมา
  • 19. 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
  • 20. 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 ???
  • 21. Method Math.random() for(int i=0 ; i<30 ; i++) { int value = 1 + (int)( Math.random() * 10); System.out.println(“num”+ i + “=”+ value); } 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 0<= X >1 เอา 1 มาบวก เพราะ ?
  • 22. 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, 2, 3, 4, 5, 6, 7, 8, 9, 10 เอา 1 มาบวก เพราะ ?
  • 23. Method Math.random() ให้นักศึกษาทำการ Random ค่า 30 ครั้ง ตั้งแต่ค่า 5-10 พร้อมทั้งแสดงผลออกมา จะทำไงดี ???
  • 24. Method Math.random() ให้นักศึกษาทำการ Random ค่า 30 ครั้ง ตั้งแต่ค่า 5-10 พร้อมทั้งแสดงผลออกมา start + (int) ( Math.random() * (end-start+1) )
  • 26. Method Recursive main mAdd mDisplay mInput Recursive
  • 27. Method Recursive ให้นักศึกษาสร้าง class ขึ้นมาใหม่ ให้ชื่อว่า TestMRecursive และให้ทำการรับค่า int จำนวน 1 ค่า
  • 28. Method Recursive ให้สร้าง Method แบบ Recursive สำหรับบวกตัวเลข ตั้งแต่ตัวเลขที่รับเข้ามาถึง 1 Enter num1: 5 Sum : 15 5 + 4 + 3 + 2 + 1
  • 29. Method Recursive public 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; }
  • 30. Method Recursive public 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; }
  • 31. Method Recursive ให้แก้ไข Method แบบ Recursive สำหรับบวกตัวเลข โดยสามารถรับค่าเริ่มต้นและสิ้นสุดได้ Enter num1: 5 Enter num2: 10 Sum : 45 5 + 6 + 7 + 8 + 9 + 10
  • 32. Method Recursive public 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; }
  • 33. Method Recursive public 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; }