Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 03 & 04 Method and Arrays Jaeki Song.

Similar presentations


Presentation on theme: "Lecture 03 & 04 Method and Arrays Jaeki Song."— Presentation transcript:

1 Lecture 03 & 04 Method and Arrays Jaeki Song

2 Outlines Method Array Creating method Method call Passing parameters
Method overloading Math class Array Definition of an Array How to Declare and Initialize an Array How to Pass Arrays to Methods

3 Methods Java API provides a rich collection of classes and methods
User-defined method Build-in method Math class method

4 Creating Method Structure
modifier returnValueType methodName (list of parameters) modifiers Return value Type Method header public static int max (int num1, int num2) { ….. } methodName parameters Method body

5 Return Type Void Nonvoid
does not return a value to the calling program Nonvoid returns a single value to the calling program.

6 Method Name A verb or a verb phrase.
Capitalize the first letter of the second word Never mention the method name inside of the function definition except if the function is recursive.

7 Parameter Listings Are variables sent to the method to perform its designated tasks. Must determine and list the data type of each parameter before listing its name.

8 Method Body Includes the statements that will perform the task
Must be framed with curly braces. Start by defining variables (only the ones outside of the class and not listed within the parameter listing)

9 Calling a Method A method is invoked by a method call
Specifies the method name and provides information (argument) Two ways to call a method Based on whether the method returns a value or not Return value int larger = max (3,4); System.out.println( max (3,4)); Return void A call to the method must be a statement System.out.println (“ Welcome”); Example: Max

10 Passing Parameters Pass by value
When you invoke a method with parameters, a copy of the value of the actual parameter is passed to the method Example: Swap Formal parameters are changed in the example, but the actual parameter are not affected

11 Overloading Methods Create another method with the same name, but different parameters Example Overloading the Max method

12 Commonly Used Math Class Methods
Description abs (x) Absolute value of x exp (x) Exponential method ex log (x) Natural logarithm of x (base e) max (x, y) Larger value of x and y min (x, y) Smaller value of x and y pow (x, y) X raised to power y (xy) sqrt (x) Square root of x

13 Example Car Loan Systems Shipping Charge

14 Arrays A list of related values with the same data type that is stored using a single group name When you need to store multiple values, use an array It is a static entity, in that it remains the same size throughout program execution

15 Array studentGrade[ ] array The individual variable called
element of the array Subscript (or index): the first element in every array is the zeroth element [0] [1] [2] [3] [4] .. 87 57 69 78 95 “second element of the array  it has a subscript of 1 “array element two”  has a subscript of 2

16 Declaring Array Specifies the type of the elements and uses operator new to allocate the number of elements Arrays are considered to be objects and all objects must be created with new operator

17 Declaring Array General format
DataType ArrayName [ ]; //Declares the array ArrayName = new DataType [Size]; // allocates the array DataType ArrayName[ ] = new DataType [ Size] e.g: float fltPayment[]; or float[ ] fltPayment; fltPayment = new float [ 20]; float fltPayment[] = new float [20];

18 Initializing an Array For primitive data types and Strings, you can give initial values to array element as you declare an array e.g. int DepartNumber[ ] = {423, 635, 589}; String DepartName[] = {“MIS”, “Marketing”, “Finance”}; This does not allow you to specify the size of the array and do not use new keyword

19 Array Length Every array in java knows it own length Example: Grade
fltPayment = new float [ 20]; fltPayment.length Example: Grade

20 Passing Arrays to Methods
To pass an array argument to a method, specify the name of the array of the array without any brackets Pass the entire array (call-by-reference) float score [ ] = new float [20]; grade = calculateAverage (score); public float calculateAverage (float b [ ])

21 Passing Arrays to Methods
To pass an array element to a method, use the subscripted name of the array element as an argument in the method call float score [ ] = new float [20]; grade = calculateAverage (score[3] ); public float calculateAverage (float b)

22 Example: Bubble Sort Compare pairs of adjacent memory cells
The small number: “bubble” The large number: “sink” Example: Comparison Results Action num[0] > num[1] False None num[1] > num[2] True Swap num[2] > num[3] True Swap 5 10 8 2 num[0] num[1] num[2] num[3]

23 Example: Bubble Sort for(i=0 ; i<num.length – 1 ; i++) {
} Ascending order for (i=1; i<num.length; i++) { } num[1]=10 num[2]=8 > if (num[j] < num [j-1]) { temp = num[j]; // one swap num[j] = num[j-1]; num[j-1] = temp; } temp=8 num[1]=8 num[2]=10 Example 2

24 Example: Selection Sort
Finds the largest/smallest number in the list and places it last/first. num[0] num[1] num[2] num[3] 8 9 5 4 num[0] num[1] num[2] num[3] 8 4 5 9 Example 3


Download ppt "Lecture 03 & 04 Method and Arrays Jaeki Song."

Similar presentations


Ads by Google