Upload
raven-dejesus
View
22
Download
1
Embed Size (px)
DESCRIPTION
CSI1390 – Java Programming Methods II. Instructor: Saeid Nourian [email protected]. Methods. Many names: Method, function, procedure, subroutine, Macro Java Syntax ( ) Example: int doWork (int duration) { return (duration * WORK_LOAD); }. - PowerPoint PPT Presentation
Citation preview
CSI1390 – Java ProgrammingCSI1390 – Java Programming
Methods IIMethods II
Instructor: Saeid NourianInstructor: Saeid Nourian
[email protected]@site.uottawa.ca
MethodsMethods
Many names:Many names:– Method, function, procedure, subroutine, Method, function, procedure, subroutine,
MacroMacro Java SyntaxJava Syntax
<type> <func name> <type> <func name> (( <parameters> <parameters> )) Example:Example:
int doWork (int duration) {int doWork (int duration) {return (duration * WORK_LOAD);return (duration * WORK_LOAD);
}}
Parameter PassingParameter Passing
Types of Parameter Passing:Types of Parameter Passing: Passing Value Passing Value
– Primitive typesPrimitive types Passing ReferencePassing Reference
– ArraysArrays– ObjectsObjects
Passing ValuesPassing Values
void main() {void main() {int a = 12;int a = 12;int b = 5;int b = 5;swap (a, b);swap (a, b);System.out.print(a+“,”+ b);System.out.print(a+“,”+ b);
}}
void swap (int a, int b) {void swap (int a, int b) {int tmp;int tmp;tmp = a;tmp = a;a = b;a = b;b = tmp;b = tmp;
}} 12a
5b
5b
12a
Stack Memory
<Garbage>tmp 12
5
12
swap scope
main scope
Does not work!
Passing ReferencesPassing Referencesvoid main() {void main() {
Integer a = new Integer();Integer a = new Integer();a.value = 12;a.value = 12;Integer b = new Integer();Integer b = new Integer();b.value = 5b.value = 5swap (a, b);swap (a, b);System.out.print(a+”,”+b);System.out.print(a+”,”+b);
} }
void swap (Integer a, Integer b) {void swap (Integer a, Integer b) {Integer tmp = new Integer();Integer tmp = new Integer();tmp.value = a.value;tmp.value = a.value;a.value = b.value;a.value = b.value;b.value = tmp.value;b.value = tmp.value;
}}
Works!
Passing ReferencesPassing References
void main() {void main() {Integer a = new Integer(12);Integer a = new Integer(12);Integer b = new Integer(5);Integer b = new Integer(5);swap (a, b);swap (a, b);cout << a << b;cout << a << b;
}}
void swap (Integer a, Integer b) {void swap (Integer a, Integer b) {Integer tmp;Integer tmp;tmp = a;tmp = a;a = b;a = b;b = tmp;b = tmp;
}}
Does not work!
Passing ReferencesPassing References
void foo4 (int z[]) {void foo4 (int z[]) {
}}
void foo2 (int z[][]) {void foo2 (int z[][]) {
}}
int[] a1 = new int[3];int[] a1 = new int[3];foo1 (a1)foo1 (a1)
int[][] a2 = new int[2][2];int[][] a2 = new int[2][2];foo3 (a2) foo3 (a2)
Passing ReferencesPassing References
void main() {void main() {int[] myArray = new int[] {12, 5}; int[] myArray = new int[] {12, 5}; swap (myArray, 0, 1);swap (myArray, 0, 1);System.out.print(myArray[0]+“,”+ myArray[0]);System.out.print(myArray[0]+“,”+ myArray[0]);
}}
void swap (int array[], int i, int j) {void swap (int array[], int i, int j) {int tmp;int tmp;tmp = array[i];tmp = array[i];array[i] = array[j];array[i] = array[j];array[j] = tmp;array[j] = tmp;
}}
Works!
Passing ReferencesPassing References
// Swaps a[i] with a[j]. // Swaps a[i] with a[j]. private static void swap(int[] a, int i, int j) private static void swap(int[] a, int i, int j) { {
if (i == j) if (i == j) return; return;
int temp = a[i]; int temp = a[i]; a[i] = a[j]; a[i] = a[j]; a[j] = temp; a[j] = temp;
} }
Works!
Returning ValuesReturning Values
Functions return a new Functions return a new copycopy of the of the “value” or “reference” “value” or “reference”
The following function:The following function:int func_1 () {int func_1 () {
int a = 7;int a = 7;
return a;return a;
}} Returns the value 7Returns the value 7
Returning Values & ReferencesReturning Values & References// return value (primitive value)// return value (primitive value)int getValue () {int getValue () {
int v = 10;int v = 10;return v;return v;
}}
// return reference (object)// return reference (object) String getLocalObject () {String getLocalObject () {
String s = new String(“7”);String s = new String(“7”);return s;return s;
}}
// return reference (array)// return reference (array) int[] getArray () {int[] getArray () {
int[] a = new int[] {7,9,10};int[] a = new int[] {7,9,10};return a;return a;
}}
Bubble SortBubble Sort
public static void bubbleSort(int[] a) public static void bubbleSort(int[] a) { {
int numSwaps = 0; int numSwaps = 0; for (int i = 0; i < a.length; i++) for (int i = 0; i < a.length; i++)
for (int j = 1; j < a.length - i; j++) for (int j = 1; j < a.length - i; j++) if (a[j-1] > a[j]) { if (a[j-1] > a[j]) {
swap(a, j-1, j); swap(a, j-1, j); numSwaps++; numSwaps++;
} } System.out.println(numSwaps + " swaps"); System.out.println(numSwaps + " swaps");
} }