1 / 36

Method 1

Method 1. Thanachat Thanomkulabut. Outline. Method Idea. Password. Input1. Input2. Light Switch. Safe. Calculator. Output. Pre-definded Method in C#. Console.WriteLine(”Hello world!”);. double j = Math.Sqrt(9);. Hello world!. 9. Console.WriteLine. Math.Sqrt. 3.

ischultz
Download Presentation

Method 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Method 1 Thanachat Thanomkulabut

  2. Outline

  3. Method Idea Password Input1 Input2 Light Switch Safe Calculator Output

  4. Pre-definded Method in C# • Console.WriteLine(”Hello world!”); • double j = Math.Sqrt(9); Hello world! 9 Console.WriteLine Math.Sqrt 3

  5. Pre-definded Method in C# • int N = int.Parse(”5”); • double ans =Math.Max(9, 2); String “5” 9 2 int.Parse Math.Max Int 5 9

  6. What is Method? Method is a function/procedure owned by class. System.Console.WriteLine(“Hello World!”);

  7. Type of Method 1. Non-Returned Value Method - Procedure Console.WriteLine(“Hello World!”); Parameter Method Name Class Name 2. Returned Value Method – Function • J = Math.Max(5, 20); 20 Class Name Parameter Method Name

  8. Type of Method n1 n2 Hello world! Math.Max Console.WriteLine Max of n1 & n2 Return no value(void) Return Value

  9. Method Example *** Program End using System; class method_example { static voidMain() { displaybox(); Console.WriteLine("Program End"); } static voiddisplaybox() { Console.WriteLine("x"); Console.WriteLine("xx"); } } Monitor 0 1 3 2

  10. Outline • Method Overview • Using Method • Parameter Passing in Method • Pass by value • Pass by reference

  11. Using User-Defined Method 1.DefineMethod 2.Call Method

  12. Define Method in C# static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; } • #remarkreturn-type • return type can beint, double, string, … • and need return statement • void= return no value

  13. Method Declaration Example staticvoid displaybox () { } staticint calage () { } staticint calbox(int x,int y) { }

  14. Example1: Non-Returned Value using System; class NReturned { static void Main() { Console.WriteLine(”Hello, Mckazine”); Console.WriteLine(”Hello, Mckazine”); Console.WriteLine(”Hello, Mckazine”); Console.WriteLine(”Hello, Mckazine”); } } HelloM Hello, Mckazine

  15. Example1: Non-Returned Value using System; class NReturned { static void HelloM() { Console.WriteLine(”Hello, Mckazine”); } static void Main() { HelloM(); HelloM(); HelloM(); HelloM(); } } Hello Mckazine Hello Mckazine Hello Mckazine Hello Mckazine

  16. Example2: Non-Returned Value with parameters using System; class NReturned { static void Main() { Console.WriteLine(”Hi, AAA”); Console.WriteLine(”Hi, BBB”); Console.WriteLine(”Hi, CCC”); } } string string name Hi Hi, name

  17. Example2: Non-Returned Value with parameters using System; class NReturned { static void Hi(string name) { Console.WriteLine(”Hi, {0}”, name); } static void Main() { Hi(”AAA”); Hi(”BBB”); Hi(”CCC”); } } string name Hi Monitor Hi,AAA Hi, name Hi, BBB Hi, CCC

  18. Example 3 static void Main(){ int i,j,n; Console.WriteLine("Square size = 2"); for(i=0;i<2;i++){ for(j=0;j<2;j++) Console.Write("*"); Console.WriteLine(); } Console.WriteLine(); Console.WriteLine("Square size = 4"); for(i=0;i<4;i++){ for(j=0;j<4;j++) Console.Write("*"); Console.WriteLine(); } Console.WriteLine(); } int n DrawSquare Result ... **** * **** * **** * **** * ... Square size = 2** ** Square size = 4**** **** **** ...   ...

  19. Example 3 static void Main(){ Console.WriteLine("Square size = 2"); drawSquare(2); Console.WriteLine("Square size = 4"); drawSquare(4); } static void drawSquare(int n){ int i,j; for(i=0;i<n;i++){ for(j=0;j<n;j++) Console.Write("*"); Console.WriteLine(); } Console.WriteLine(); } Monitor int n Square size = 2 **** DrawSquare Square size = 4 ... ************ **** **** * **** * **** * **** * ... ...   ...

  20. Self Test 1 • Write the method PrintNumber(n) • Recieve parameter n • Show 1234...n Example int n 7 PrintNumber PrintNumber 1234...n 1234567

  21. Self Test 1 int n PrintNumber • Write the method PrintNumber(n) • Recieve parameter n • Show 1234...n 1234...n static void PrintNumber(int n) { int i; for(i=1;i<=n;i++) Console.Write(i); }

  22. Outline

  23. Define Method in C# static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; } • #remarkreturn-type • return type can beint, double, string, … • and need return statement • void= return no value

  24. Return value method n static void PrintNumber(int n) { double n, ans; n = int.Parse(Console.ReadLine()); ans = Math.Sqrt(n); Console.Write(“Root {0} is equal {1}",n,ans); } Math.Sqrt n n

  25. Example 1: Returned Value using System; class Returned { static void Main() { int j; j = 2*2; Console.WriteLine(”{0}”, j); j = 5*5; Console.WriteLine(”{0}”, j); } } n Power2 n2

  26. Example 1: Returned Value using System; class Returned { static int Power2(int n) { return n*n; } static void Main() { int j; j = Power2(2); //j=2*2; Console.WriteLine(”{0}”, j); j = Power2(5); //j=5*5; Console.WriteLine(”{0}”, j); } } n Power2 n2 Result 4 25

  27. Example 2: Returned Value static void Main() { int n,i,count=0; n = int.Parse(Console.ReadLine()); for(i=1;i<=n;i++) if(n%i==0) count++; if(count==2) Console.Write("{0} is Prime.",n); } Process for check Prime number.

  28. Example 2: Returned Value static void Main() { int n,i,count=0; n = int.Parse(Console.ReadLine()); for(i=1;i<=n;i++) if(n%i==0) count++; if(count==2) Console.Write("{0} is Prime.",n); } x Is_Prime True if x is prime.False if x is not prime

  29. Example 2: Returned Value static bool Is_Prime(int x){ int i,count=0; for(i=1;i<=x;i++) if(x%i==0) count++; if(count==2) return true; else return false; } static void Main(){ int n = int.Parse(Console.ReadLine()); if(Is_Prime(n) == true) Console.Write("{0} is Prime.",n); } x Is_Prime True if n is prime.False if n is not prime

  30. Example 3: Returned Value static void Main() { double r,h,V; r = double.Parse(Console.ReadLine()); h = double.Parse(Console.ReadLine()); V = Math.Pi*r*r*h; Console.Write("V = {0}",V); } h r V_Cylinder V

  31. Example 3: Returned Value static double V_Cylinder(double r, double h){ return Math.PI*r*r*h; } static void Main() { double r,h,V; r = double.Parse(Console.ReadLine()); h = double.Parse(Console.ReadLine()); V = V_Cylinder(r,h); Console.Write("V = {0}",V); } h r V_Cylinder V

  32. Self Test 1 • Define Power4(n) Method • Return n*n*n*n • Ex. int j = Power4(3); //Return 81 int int n static ____ Power4(___________________) { } int ans; return n*n*n*n; ans = n*n*n*n; reture ans;

  33. Self Test 2 • Define Max(x, y, z) Method • Return Max Value • Ex. int j = Max(5, 20, 12); //Return 20 int int x, int y, int z static ____ MAX(___________________) { } int ans; if(x >= y && x >= z) ans = x; return x; else if(y >= x && y >= z) return y; ans = y; else ans = z; return z; return ans;

  34. Main() Namespace Class Method1() Method2() Method3() MethodN() C# Structure – Multiple Methods

  35. Example 5 : Mutiple Method static double FindMedian(double n1, double n2) { return (n1+n2)/2; } static void Report(double x, double y) { Console.Write("Median = {0} ", FindMedian(x,y)); } static void Main(string[] args) { Console.Write("Please Key 2 Input : "); double a1 = double.Parse(Console.ReadLine()); double a2 = double.Parse(Console.ReadLine()); Report(a1, a2); }

  36. Any question?

More Related