1 / 12

CSI1390 – Java Programming Methods II

CSI1390 – Java Programming Methods II. Instructor: Saeid Nourian snourian@site.uottawa.ca. Methods. Many names: Method, function, procedure, subroutine, Macro Java Syntax <type> <func name> ( <parameters> ) Example: int doWork (int duration) { return (duration * WORK_LOAD); }.

Download Presentation

CSI1390 – Java Programming Methods II

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. CSI1390 – Java ProgrammingMethods II Instructor: Saeid Nourian snourian@site.uottawa.ca

  2. Methods • Many names: • Method, function, procedure, subroutine, Macro • Java Syntax <type> <func name> ( <parameters> ) • Example: int doWork (int duration) { return (duration * WORK_LOAD); }

  3. Parameter Passing Types of Parameter Passing: • Passing Value • Primitive types • Passing Reference • Arrays • Objects

  4. tmp <Garbage> main scope swap scope a a b b 12 5 5 12 Passing Values Does not work! void main() { int a = 12; int b = 5; swap (a, b); System.out.print(a+“,”+ b); } void swap (int a, int b) { int tmp; tmp = a; a = b; b = tmp; } Stack Memory 12 12 5

  5. Passing References void main() { Integer a = new Integer(); a.value = 12; Integer b = new Integer(); b.value = 5 swap (a, b); System.out.print(a+”,”+b); } void swap (Integer a, Integer b) { Integer tmp = new Integer(); tmp.value = a.value; a.value = b.value; b.value = tmp.value; } Works!

  6. Passing References void main() { Integer a = new Integer(12); Integer b = new Integer(5); swap (a, b); cout << a << b; } void swap (Integer a, Integer b) { Integer tmp; tmp = a; a = b; b = tmp; } Does not work!

  7. Passing References void foo4 (int z[]) { } void foo2 (int z[][]) { } int[] a1 = new int[3]; foo1 (a1) int[][] a2 = new int[2][2]; foo3 (a2)

  8. Passing References void main() { int[] myArray = new int[] {12, 5}; swap (myArray, 0, 1); System.out.print(myArray[0]+“,”+ myArray[0]); } void swap (int array[], int i, int j) { int tmp; tmp = array[i]; array[i] = array[j]; array[j] = tmp; } Works!

  9. Passing References // Swaps a[i] with a[j]. private static void swap(int[] a, int i, int j) { if (i == j) return; int temp = a[i]; a[i] = a[j]; a[j] = temp; } Works!

  10. Returning Values • Functions return a new copy of the “value” or “reference” • The following function: int func_1 () { int a = 7; return a; } • Returns the value 7

  11. Returning Values & References // return value (primitive value) int getValue () { int v = 10; return v; } // return reference (object) String getLocalObject () { String s = new String(“7”); return s; } // return reference (array) int[] getArray () { int[] a = new int[] {7,9,10}; return a; }

  12. Bubble Sort public static void bubbleSort(int[] a) { int numSwaps = 0; for (int i = 0; i < a.length; i++) for (int j = 1; j < a.length - i; j++) if (a[j-1] > a[j]) { swap(a, j-1, j); numSwaps++; } System.out.println(numSwaps + " swaps"); }

More Related