Download
final review n.
Skip this Video
Loading SlideShow in 5 Seconds..
Final Review PowerPoint Presentation
Download Presentation
Final Review

Final Review

103 Views Download Presentation
Download Presentation

Final Review

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Final Review Author: ThanachatThanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011

  2. Outline • Major topics • Methods • Arrays • structs

  3. Major Topics • Methods • Input • Output • Global vs. Local • Scope of variables • Array • Declare, Create, Initialize, Access • Run ‘for’ loop with array • Array with methods • 1D vs. 2D vs. multiD

  4. Major Topics (cont.) • struct • Define, Create, Access • Array of struct • struct with methods • File I/O • Collection

  5. Pre-midterm Topics • Data types & Variable declaration • Math expression • Logical expression • How to write condition • If • switch-case • Loop • while, do…while, for • break, continue

  6. Outline • Major topics • Methods • Arrays • structs

  7. Methods • Input • Output • Global vs. Local • Scope of variables

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

  9. Characteristics of Method

  10. Define Method Syntax static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; } 10

  11. When see a method, answer the followings: • What is method name? • Are there any input? If so, for each input: • what’s data type? • What’s variable name? • Is it regular or ‘ref’ or ‘out’? • Are there any output? If so, for each output: • what’s data type? • What’s variable name to store output?

  12. Method Input • Input • Name of copied variable can be different from original variable • Data type of copied and original variables must be the same! static void Subject (double num, bool okay, int unit) { ... ... ... } static void Main() { double n; int limit; bool found; ... ... Subject(12.7, true, (limit*3)+5); ... ... }

  13. Method Input (cont.) • Input (cont.) • We must choose input to be Pass by Valueor Pass by Reference • Pass by Value • Data type + Variable name • Pass by Reference => Value returns automatically. Do not use “return”. • ref + Data type + Variable name • Must initialize value of Variable before calling the method static void sub(int a, ref intb) { b = a - b; }

  14. Define Method Syntax static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; } 14 • return-type can be • void= return no value • int, double, string, … • = return value with specified data type and need return statement inside method

  15. Method Output • Output • We must choose input to be Returned Value or Pass by Reference • Returned Value: appear before Method name • static returned_data_typeMethod_name (input_parameter_list) { … } • Need “return” as one statement in a method • Pass by Reference: appear in (..) after Method name • Value returns automatically. Do not use “return”. • ref + Data type + Variable name • Must initialize value of Variable before calling the method • out + Data type + Variable name • No need to initialize value of Variable before calling the method • Value of variable before calling the method will not be passed into the method static int add(int a, int b) { return a + b; } static void add(int a, ref intb, out int c) { c = a + b; }

  16. Monitor Pass by Value num = 25 num = 5 • Changeincopied variablein the method has no effecton the original variable static void Square(int num) { num = num*num; Console.WriteLine("num = {0}",num); } num 25 5 static void Main() { int num=5; Square(num); Console.WriteLine(“num = {0}",num); } num 5

  17. Monitor Pass by reference Hello x = 4, y = 6, z = 10 Finally a = 2, b= 6, c = 10 static void Hello (int x, ref int y, out int z) { x = 2*x; y = 2*y; z = x+y; Console.WriteLine(“Hello x = {0}, y = {1}, z = {2}”, x,y,z); } x y z “Out” does not pass in any value to method 10 4 2 6 3 static void Main() { int a, b, c; a = 2; b = 3; c = 4; Hello(a, ref b, out c); Console.WriteLine(“Finally a = {0}, b = {1}, c = {2}”,a,b,c); } a b c 6 2 3 4 10

  18. Monitor Examples n is 14th alphabet 50 20 static void Main() { PrintLine(‘n’, 14); double width = 5, height = 10; double area = Area(width, height); Console.WriteLine(area); Console.WriteLine(compAve(10,20,30)); } static voidPrintLine(char a, int b ) { Console.WriteLine(“{0} is {1}th alphabet”, a, b); } static double Area(double w, double h) { return w*h; } static doublecompAve(int x1, int x2, int x3) { return(x1+x2+x3)/3; }

  19. Examples 2 static void Main() { char x = ‘p’, y = ‘q’; int p = 3, q = 5; double a=5, b=10, c=0, d, e; sub( p, q, out d); mult( a , b, ref c); switch( ref x, ref y); } static voidsub(int a, int b, out double c) { c = a – b ; } static voidmult(double p, double q, ref double r) { r = p * q; } static voidswitch(ref char x1, ref char x2) { char temp = x1; x1 = x2; x2 = temp; }

  20. static void Main() { int x = 1, y = 2; Add100_v1(x,y); Console.WriteLine(“{0},{1}”,x,y); Add100_v2(ref x,ref y); Console.WriteLine(“{0},{1}”,x,y); Add100_v3(ref x,y); Console.WriteLine(“{0},{1}”,x,y) } static voidAdd100_v1(intnum, intnum2) { num += 100; num2 += 100; } static voidAdd100_v2(refintnum, ref int num2) { num += 100; num2 += 100; } static voidAdd100_v3(refint num, intnum2) { num+= 100; num2 += 100; } Methods with ref ref can do: pass values into method return the changed values back Methods: Passing output Monitor 1,2 101,102 201,102 ref can be used with regular or out Variables passing with ref need to be initialized before passing.

  21. Out can do: • pass the changed values back. Methods with out static void Main() { int x = 1, y = 2; int result1; Add_v1(x,y,out result1); Console.WriteLine(result1); int result2; Add_v2(x,y,ref result2); Console.WriteLine(result2); } static void Add_v1(intnum, int num2, out num3) { num3 = num + num2; } static void Add_v2(intnum, int num2, ref num4) { num4 = num + num2; } No Initialized value for result1 (using out) OK! No initialized value for result2 (using ref) ERROR!!!! Variables passing with out DO NOT need to be initialized before passing.

  22. Out can do: • pass the changed values back. Methods with out static void Main() { int x = 1, y = 2; int result1; Add_v1(x,y,out result1); Console.WriteLine(result1); intresult2 = 0; Add_v2(result1,y,ref result2); Console.WriteLine(result2); } static void Add_v1(intnum, int num2, out num3) { num3 = num + num2; } static void Add_v2(intnum, int num2, ref num4) { num4 = num + num2; } No Initialized value for result1 (using out) OK! Initialized value for result2 (using ref) OK! Monitor 3 5 Variables passing with out DO NOT need to be initialized before passing.

  23. Global vs. Local Variables namespace class Global variables, constants, structs Main() Local variables, constants, statements MethodX() Local variables, constants, statements

  24. Global vs. Local Variables (cont.) class n static int n; Main() Method1() int x; int y; x y   x = 5; x = 5;  y = 12;  y = 12; n = 9;   n = 9;

  25. Variable Scope: Example #1 Scope of this i starts here using System; class ScopeTest { static void Main() { for(int i=0;i<5;i++){ Console.Write(“*”); } Console.Write(i); } } Then ends here This line is not in i's scope

  26. Variable Scope: Example #2 Scope of this x starts here using System; class ScopeTest { static void Main() { int x; x = 5; } static void Method1() { Console.WriteLine(x); } } Then ends here This line is not in x's scope

  27. Monitor Program will show value of variable “x” in Method Main Example 1 3 class Test { static void Main() { int x = 3; Add10(x); Console.WriteLine(x); } static void Add10(int x){ x += 10; } } Method Add10 will not pass new value of ‘x’ back since ‘x’ is not passed by reference.

  28. Example 2 Monitor Example 1 class Test { static int sum = 0; static void Main() { Console.Write(“sum={0}”,sum); Add(2); Console.Write(“sum={0}”,sum); } static void Add(int y){ sum += y; } } sum=0 sum=2

  29. Example 2 Monitor Example 1 class Test { static int sum = 0; static void Main() { Console.Write(“sum={0}”,sum); Add(2); Console.Write(“sum={0}”,sum); } static void Add(int y){ sum += y; } } sum=0 sum=2 Console.Write(“y={0}”,y); Error

  30. 0 Monitor Does this program work? Example 3 5 0 class Test { static int sum = 0; static void Main() { Console.WriteLine(sum); methodA(); Console.WriteLine(sum); } static void methodA(){ int sum = 5; Console.WriteLine(sum); } } In Main, value of GLOBALvariable‘sum’ is used. In methodA, value of LOCALvariable ‘sum’ is used.

  31. Outline • Major topics • Methods • Arrays • structs

  32. Arrays • Declare, Create, Initialize, Access • Run ‘for’ loop with array • Array with methods • 1D vs. 2D vs. multiD

  33. Array: 1D • Declare array • int [] arr; • Create array: Must have size • arr = new int [5]; • Declare + create: Must have size • int [] arr = new int [5]; • If we want to initialize, we need to declare (+create) array first. No need to have size • int [] arr = new int [] {5,6,7,8,9}; • int [] arr ={5,6,7,8,9};

  34. Array: 1D • Which ones are valid C# statements ? • int [] arrayA = new int[] {3, 6, 9}; • intarrayA= new int[3] {3, 6, 9}; • int [] arrayA = new int{3, 6, 9}; • intarrayA= new int{3, 6, 9}; • int [] arrayA = new int[3] ; • int [] arrayA = new int[] ; • int [] arrayA = new int[3] {3, 6, 9};       

  35. Array: 1D (cont.) • Accessing array: index = integer • Use index: 0,1,…,size-1 • Accessing 1 element in array: arr[index] • Example: arr[0] = 10; • Accessing many elements in array: ‘for’ • for (inti = 0; i < arr.Length; i++) arr[i] = i+10; • Only-Read all elements in array • foreach (int x in arr) Console.WriteLine(x);

  36. Array: 1D (cont.) • Only-Read all elements in array (cont.) • Assume arr2 is array with double type int count = 0; foreach (double y in arr2) { if (y > 0) count++; }

  37. Output String 0 1 2 3 4 B ‘B’ ‘o’ ‘s’ ‘s’ ‘a' o name • String is “array of chars.” string name = “Bossa”; Console.WriteLine(name[0]); Console.WriteLine(name[4-3]); name[4] = ‘m’;  Each element in string is for “reading only”

  38. Array: 2D • Declare array • int [,] arr; • Create array: Must have size • arr = new int [3,2]; • Declare + create: Must have size • int [,] arr = new int [3,2]; • If we want to initialize, we need to declare (+create) array first. No need to have size • int [,] arr = new int [,] {{5,6},{7,8},{9,0}}; • int [,] arr={{5,6},{7,8},{9,0}};

  39. Array: 2D • Accessing array: 2 indices • Use row index: 0,1,…,row_size-1 • Use column index: 0,1,…,col_size-1 • Accessing 1 element in 2D array: arr[row_index, col_index] • Example: arr[0,1] = 5; • Get size: • arr.GetLength(0): Number of rows • arr.GetLength(1): Number of columns • arr.Length: Number of total elements

  40. Array: 2D (cont.) • Accessing all elements in 2D array • Use 2 ‘for’ loops: go through all rows and all columns • Different index for each ‘for’ loop • for (inti = 0; i < arr.GetLength(0); i++) for (intj = 0; j < arr.GetLength(1); j++) arr[i,j] = i*10+j;

  41. Array: 2D (cont.) • Accessing one row (e.g., 3rd row) in 2D array • Use 1 ‘for’ loops to go through all columns • Fixed row index for given row • ‘i’ index for column • Example: for (inti = 0; i < arr.GetLength(1); i++) Console.WriteLine(arr[2 , i]);

  42. Array: 2D (cont.) • Accessing one column (e.g., 2nd column) in 2D array • Use 1 ‘for’ loops to go through all rows • Fixed column index for given column • ‘i’ index for row • for (inti = 0; i < arr.GetLength(0); i++) Console.WriteLine(arr[ i, 1]);

  43. Pass array into method without ‘ref’ Pass the changed values back. Can’t pass the new array back Methods: passing array with no ref static voidMain() { int [] arr = new int[10]; Arr_initial(arr); Display_arr(arr); Add10(arr); Display_arr(arr); } static voidArr_initial (int [] a) { for (inti= 0; i< a.Length; i++) a[i] = i; } static voidAdd10(int [] a) { for (inti = 0; i < a.Length; i++) a[i] = a[i]+10; } static void Display_arr(int [] a) { for (inti = 0; i < a.Length; i++) Console.Write(“{0},” a[i]); Console.WriteLine(); } Monitor 0,1,2,3,4,5,6,7,8,9, 10,11,12,13,14,15,16,17,18,19, Array is initialized all elements with 0 automatically. Print out values from first to last elements in array

  44. Pass array into method without ‘ref’ Pass the changed values back. Can’t pass the new array back Methods: passing array with no ref static voidMain() { int [] arr = new int[10]; Arr_initial(arr); Display_arr(arr); Op_arr(arr); Display_arr(arr); } static voidArr_initial (int [] a) { for (inti= 0; i< a.Length; i++) a[i] = i; } static voidOp_arr(int[] a) { a = new int [5]; } static void Display_arr(int [] a) { for (inti = 0; i < a.Length; i++) Console.Write(“{0},” a[i]); Console.WriteLine(); } Monitor 0,1,2,3,4,5,6,7,8,9, 0,1,2,3,4,5,6,7,8,9, Declare new array with 5 elements Without ref: new array will not be passed back to Main

  45. Pass array into method without ‘ref’ Pass the changed values back. Can’t pass the new array back Methods: passing array with no ref static voidMain() { int [] arr = new int[10]; Arr_initial(arr); Display_arr(arr); Op_arr(arr); Display_arr(arr); } static voidArr_initial (int [] a) { for (inti= 0; i< a.Length; i++) a[i] = i; } static voidOp_arr(int[] a) { a[0]=100; a = new int [5]; a[1]=1000; } static void Display_arr(int [] a) { for (inti = 0; i < a.Length; i++) Console.Write(“{0},” a[i]); Console.WriteLine(); } Monitor 0,1,2,3,4,5,6,7,8,9, 100,1,2,3,4,5,6,7,8,9, Declare new array with 5 elements Without ref: changed values after new array will not be passed back to Main

  46. Pass array into method with ‘ref’ Pass the changed values back. Can pass the new array back Methods: passing array with ref static voidMain() { int [] arr = new int[10]; Arr_initial(arr); Display_arr(arr); Op_arr(ref arr); Display_arr(arr); } static voidArr_initial (int [] a) { for (inti= 0; i< a.Length; i++) a[i] = i; } static voidOp_arr(ref int[] a) { a = new int[5]; } static voidDisplay_arr(int [] a) { for (inti = 0; i < a.Length; i++) Console.Write(“{0},” a[i]); Console.WriteLine(); } Monitor 0,1,2,3,4,5,6,7,8,9, 0,0,0,0,0, Declare new array with 5 elements With ref: new array will be passed back to Main

  47. Pass array into method with ‘ref’ Pass the changed values back. Can pass the new array back Methods: passing array with ref static voidMain() { int [] arr = new int[10]; Arr_initial(arr); Display_arr(arr); Op_arr(ref arr); Display_arr(arr); } static voidArr_initial (int [] a) { for (inti= 0; i< a.Length; i++) a[i] = i; } static voidOp_arr(ref int[] a) { a[0] = 100; a = new int[5]; a[2]=1000; } static voidDisplay_arr(int [] a) { for (inti = 0; i < a.Length; i++) Console.Write(“{0},” a[i]); Console.WriteLine(); } Monitor 0,1,2,3,4,5,6,7,8,9, 0,0,1000,0,0, Declare new array with 5 elements With ref: changed values after new array will be passed back to Main

  48. Outline • Major topics • Methods • Arrays • structs

  49. struct • Define, Create, Access • Array of struct • struct with methods

  50. Define struct StudentInfo Must use "struct" keyword Every structneeds a name Name: Paul structStudentInfo { public string name; public string dept; public int age; public char gender; } string Dept: Math string Age: 18 int Members of struct Gender: M char Protection level – for now always use "public”