CSI1390 – Java Programming Methods II Instructor: Saeid Nourian

Slides:



Advertisements
Similar presentations
EC-241 Object-Oriented Programming
Advertisements

Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Sorting int s[20], size; size = 5; Original array Final array (in Ascending Order) Final array (in Descending Order)
1 Selection Sort and Quick Sort Instructor: Mainak Chaudhuri
Chapter 11 Sorting. Selection Sort 0 1 n-2 n-1index 第 0 回合 第 1 回合 第 n-2 回合 min.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
1 Pointers & functions Pointers allow us to simulate pass by reference. void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main () { int.
CSE 373: Data Structures and Algorithms
Bubble Sort Notes David Beard CS181. Bubble Sort for Strings Double pass algorithm to sort a single dimensional array. Inner loop “bubbles” largest element.
Introduction to C Programming CE
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
Sorting Text Read Shaffer, Chapter 7 Sorting O(N 2 ) sorting algorithms: – Insertion, Selection, Bubble O(N log N) sorting algorithms – HeapSort, MergeSort,
Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
CSE 373 Data Structures and Algorithms
CSC 172 DATA STRUCTURES. SORTING Exercise : write a method that sorts an array. void mysort(int [] a) { }
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Spring 2006CISC101 - Prof. McLeod1 Last Time The File class. Back to methods –Passing parameters by value and by reference. –Review class attributes. –An.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
CS 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Sorting Slowly. Swap (used in bubble sort and selectSort) public static void swap(int[] array, int a, int b ) { //save a int temp = array[a]; //copy b.
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
Arrays and Sorting. Process Open a file that contains integers, one per line. Read each line, convert to short and store each into an array Sort the array.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
Sorting Mr. Jacobs.
Functions (Methods) Pascal C, C++ Java Scripting Languages
COP 3503 FALL 2012 Shayan Javed Lecture 16
Building Java Programs
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Building Java Programs
Building Java Programs
An Introduction to Java – Part II
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
محاور المحاضرة خوارزمية الفقاعات الهوائية (Bubble Sort)
Shaker.
Building Java Programs
Java Lesson 36 Mr. Kalmes.
Parameter Passing in Java
References, Objects, and Parameter Passing
CS2011 Introduction to Programming I Arrays (II)
CS2011 Introduction to Programming I Methods (II)
slides created by Marty Stepp
slides created by Marty Stepp
References and Objects
Simulating Reference Parameters in C
Why did the programmer quit his job?
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Complexity analysis.
C++ Pointers and Strings
CSE 373 Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort
C++ Pointers and Strings
Building Java Programs
Stacks, Queues, ListNodes
Presentation transcript:

CSI1390 – Java Programming Methods II Instructor: Saeid Nourian

Methods  Many names: –Method, function, procedure, subroutine, Macro  Java Syntax ( ) ( )  Example: int doWork (int duration) { return (duration * WORK_LOAD); }

Parameter Passing Types of Parameter Passing:  Passing Value –Primitive types  Passing Reference –Arrays –Objects

Passing Values 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; } 12 a 5 b 5 b a Stack Memory tmp swap scope main scope Does not work!

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!

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!

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)

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!

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!

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

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; }

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"); }