METHODS (FUNCTIONS) By: Lohani Adeeb khan.

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014 Lecture 08 Title: Methods, Part 2 Reference: MalikFarrell, chap 1, Liang, Ch 5.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Liang, Introduction to Java Programming1 Arrays Gang Qian Department of Computer Science University of Central Oklahoma.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Programming Fundamentals I (COSC-1336), Lecture 5 (prepared after Chapter 5 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 5.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Method Abstraction You can.
Chapter 4 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Methods 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Introducing Methods.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods Chapter.
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Lecture 5: java.lang.Math, java.lang.String and Characters Michael Hsu CSULA.
Chapter 5 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 7: Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 5 Methods.
Chapter 6 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 3 Methods.
Chapter 5 Methods.
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 6 Methods.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 6 Arrays Solution Opening Problem
Chapter 4: Mathematical Functions, Characters, and Strings
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Chapter 5 Arrays Introducing Arrays
Group Status Project Status.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 5 Methods.
Chapter 6 Arrays.
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 4 Methods Introducing Methods Declaring Methods
Chapter 6: Methods CS1: Java Programming Colorado State University
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

METHODS (FUNCTIONS) By: Lohani Adeeb khan

Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. Here is Logic: int sum = 0; for (int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (int i = 20; i <= 30; i++) System.out.println("Sum from 20 to 30 is " + sum); for (int i = 35; i <= 45; i++) System.out.println("Sum from 35 to 45 is " + sum);

for (int i = 1; i <= 10; i++) sum += i; Note that the blocked highted text of code is repeated again and again, int sum = 0; for (int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for (int i = 20; i <= 30; i++) System.out.println("Sum from 20 to 30 is " + sum); for (int i = 35; i <= 45; i++) System.out.println("Sum from 35 to 45 is " + sum);

Solution:we put the repeating code in a single function(method) called sum and just call the function with its name in main function( so v don’t write the same code again and again public static int sum(int i1, int i2) { int sum = 0; for (int i = i1; i <= i2; i++) sum += i; return sum; } public static void main(String[] args) { System.out.println("Sum from 1 to 10 is " + sum(1, 10)); System.out.println("Sum from 20 to 30 is " + sum(20, 30)); System.out.println("Sum from 35 to 45 is " + sum(35, 45));

Defining Methods A method is a collection of statements that are grouped together to perform an operation.

Method Signature Method signature is the combination of the method name and the parameter list.

Formal Parameters The variables defined in the method header are known as formal parameters.

Actual Parameters When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.

Return Value Type A method may return a value. The returnValueType is the data type of the value the method returns. If the method does not return a value, the returnValueType is the keyword void. For example, the returnValueType in the main method is void.

Calling Methods Testing the max method This program demonstrates calling a method max to return the largest of the int values

Calling Methods, cont.

Trace Method Invocation i is now 5

Trace Method Invocation j is now 2

Trace Method Invocation invoke max(i, j)

Trace Method Invocation invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2

Trace Method Invocation declare variable result

Trace Method Invocation (num1 > num2) is true since num1 is 5 and num2 is 2

Trace Method Invocation result is now 5

Trace Method Invocation return result, which is 5

Trace Method Invocation return max(i, j) and assign the return value to k

Trace Method Invocation Execute the print statement

Scope of Local Variables A local variable: a variable defined inside a method is called Local variable. Scope: the part of the program where the variable can be referenced. The scope of a local variable starts from its declaration and continues to the end of the block {} that contains the variable. A local variable must be declared before it can be used.

Scope of Local Variables, cont. You can declare a local variable with the same name multiple times in different non-nesting blocks in a method, but you cannot declare a local variable twice in nested blocks.

Scope of Local Variables, cont. A variable declared in the initial action part of a for loop header has its scope in the entire loop. But a variable declared inside a for loop body has its scope limited in the loop body from its declaration and to the end of the block that contains the variable.

Scope of Local Variables, cont. In the method2(), int i is a local variable. A variable i is being declared in for loop again, so it displays a error..

Scope of Local Variables, cont. // Fine with no errors public static void correctMethod() { int x = 1;//local variables int y = 1;//Local variables // i is declared for (int i = 1; i < 10; i++) { x += i; } // i is declared again y += i;

Scope of Local Variables, cont. // With no errors public static void incorrectMethod() { int x = 1; int y = 1; for (int i = 1; i < 10; i++) { int x = 0; x =x+i; }

The Math Class Class constants: Class methods: PI usually used as global variables before a class containing main method. E Class methods: Trigonometric Methods Exponent Methods Rounding Methods min, max, abs, and random Methods

Trigonometric Methods sin(double a) cos(double a) tan(double a) acos(double a) asin(double a) atan(double a) Examples: Math.sin(0) returns 0.0 Math.sin(Math.PI / 6) returns 0.5 Math.sin(Math.PI / 2) returns 1.0 Math.cos(0) returns 1.0 Math.cos(Math.PI / 6) returns 0.866 Math.cos(Math.PI / 2) returns 0 Radians toRadians(90)

Exponent Methods Examples: exp(double a) Math.exp(1) returns 2.71 Math.log(2.71) returns 1.0 Math.pow(2, 3) returns 8.0 Math.pow(3, 2) returns 9.0 Math.pow(3.5, 2.5) returns 22.91765 Math.sqrt(4) returns 2.0 Math.sqrt(10.5) returns 3.24 exp(double a) Returns e raised to the power of a. log(double a) Returns the natural logarithm of a. log10(double a) Returns the 10-based logarithm of a. pow(double a, double b) Returns a raised to the power of b. sqrt(double a) Returns the square root of a.

Rounding Methods double ceil(double x) x rounded up to its nearest integer. This integer is returned as a double value. double floor(double x) x is rounded down to its nearest integer. This integer is returned as a double value. double rint(double x) x is rounded to its nearest integer. If x is equally close to two integers, the even one is returned as a double. int round(float x) Return (int)Math.floor(x+0.5). long round(double x) Return (long)Math.floor(x+0.5).

Rounding Methods Examples Math.ceil(2.1) returns 3.0 Math.ceil(2.0) returns 2.0 Math.ceil(-2.0) returns –2.0 Math.ceil(-2.1) returns -2.0 Math.floor(2.1) returns 2.0 Math.floor(2.0) returns 2.0 Math.floor(-2.0) returns –2.0 Math.floor(-2.1) returns -3.0 Math.rint(2.1) returns 2.0 Math.rint(2.0) returns 2.0 Math.rint(-2.0) returns –2.0 Math.rint(-2.1) returns -2.0 Math.rint(2.5) returns 2.0 Math.rint(-2.5) returns -2.0 Math.round(2.6f) returns 3 Math.round(2.0) returns 2 Math.round(-2.0f) returns -2 Math.round(-2.6) returns -3

min, max, and abs max(a, b)and min(a, b) abs(a) random() Examples: Returns the maximum or minimum of two parameters. abs(a) Returns the absolute value of the parameter. random() Returns a random double value in the range [0.0, 1.0). Examples: Math.max(2, 3) returns 3 Math.max(2.5, 3) returns 3.0 Math.min(2.5, 3.6) returns 2.5 Math.abs(-2) returns 2 Math.abs(-2.1) returns 2.1

ARRAYS By: Lohani Adeeb khan

Introducing Arrays Array is a data structure that represents a collection of the same types of data.

Declaring Array Variables datatype[] arrayRefVar; Example: double[] myList; datatype arrayRefVar[]; // This style is allowed, but not preferred double myList[];

Creating Arrays arrayRefVar = new datatype[arraySize]; Example: myList = new double[10]; myList[0] references the first element in the array. myList[9] references the last element in the array.

Declaring and Creating in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10]; datatype arrayRefVar[] = new datatype[arraySize]; double myList[] = new double[10];

The Length of an Array For example, Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayRefVar.length For example, myList.length returns 10

Indexed Variables The array elements are accessed through the index. The array indices are 0-based, i.e., it starts from 0 to arrayRefVar.length-1. In the example in Figure 6.1, myList holds ten double values and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: arrayRefVar[index];

Using Indexed Variables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in myList[0] and myList[1] to myList[2]. myList[2] = myList[0] + myList[1];

Array Initializers This shorthand syntax must be in one statement. Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement.

Declaring, creating, initializing Using the Shorthand Notation double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;

CAUTION Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double[] myList; myList = {1.9, 2.9, 3.4, 3.5};

Trace Program with Arrays Declare array variable values, create an array, and assign its reference to values public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays i becomes 1 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays i (=1) is less than 5 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays After this line is executed, value[1] is 1 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays After i++, i becomes 2 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays i (= 2) is less than 5 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays After this line is executed, values[2] is 3 (2 + 1) public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays After this, i becomes 3. public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays i (=3) is still less than 5. public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays After this line, values[3] becomes 6 (3 + 3) public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays After this, i becomes 4 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays i (=4) is still less than 5 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays After this, values[4] becomes 10 (4 + 6) public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays After i++, i becomes 5 public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays i ( =5) < 5 is false. Exit the loop public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Trace Program with Arrays After this line, values[0] is 11 (1 + 10) public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4];

Getting input for a specified number of numbers in array) Scanner input = new Scanner(System.in); System.out.print("Enter " + myList.length + " values: "); for (int i = 0; i < myList.length; i++) myList[i] = input.nextDouble(); //here myList is name of array that has been declare. // myList.length gives the size of array //for example the array size is 4 then myList.length=4 //we use for loop to get 4 numbers as input to be stored in array.

Printing arrays for (int i = 0; i < myList.length; i++) { System.out.print(myList[i] + " "); } // to print back all for elements of array we use for loop again

Summing all elements //Adding all array elements double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; }

Finding the largest element //logic to find the largest element in array double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; }

Copying Arrays LOGIC Using a loop: int[] sourceArray = {2, 3, 1, 5, 10};//FIRST ARRAY int[] targetArray = new int[sourceArray.length]; //Second array being assgined same size as first array for (int i = 0; i < sourceArrays.length; i++) targetArray[i] = sourceArray[i];/* values of first array being copied to second array by using a for loop*/

Searching Arrays Searching is the process of looking for a specific element(number/value) in an array; for example, discovering whether a certain score is included in a list of scores. The key is the element/number we want to search. The logic for searching element in array is given below… It is called Linear search

Declare result and create array Trace the reverse Method:HERE WE COPY ALL ARRAY ELEMENTS OF FIRST ARRAY TO ANOTHER ARRAY BUT IT IS COPIED IN THE REVERSED ORDER. SO THE SECOND ARRAY LIST IS REVERSE OF THE 1ST ARRAY int[] list1 = {1, 2, 3, 4, 5, 6};/*these lines are declared in main method*/ int[] list2 = reverse(list1);//function call Declare result and create array public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; This is a method called int reverse()…it is outside the main method(psvm) of the class. list 1 2 3 4 5 6 result

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i = 0 and j = 5 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i (= 0) is less than 6 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i = 0 and j = 5 Assign list[0] to result[5] public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); After this, i becomes 1 and j becomes 4 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i (=1) is less than 6 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i = 1 and j = 4 Assign list[1] to result[4] public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); After this, i becomes 2 and j becomes 3 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i (=2) is still less than 6 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i = 2 and j = 3 Assign list[i] to result[j] public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); After this, i becomes 3 and j becomes 2 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i (=3) is still less than 6 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i = 3 and j = 2 Assign list[i] to result[j] public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 4 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); After this, i becomes 4 and j becomes 1 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 4 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i (=4) is still less than 6 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 4 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i = 4 and j = 1 Assign list[i] to result[j] public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 5 4 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); After this, i becomes 5 and j becomes 0 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 5 4 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i (=5) is still less than 6 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 5 4 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i = 5 and j = 0 Assign list[i] to result[j] public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 6 5 4 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); After this, i becomes 6 and j becomes -1 public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 6 5 4 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); i (=6) < 6 is false. So exit the loop. public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 result 6 5 4 3 2 1

Trace the reverse Method, cont. int[] list1 = new int[]{1, 2, 3, 4, 5, 6}; int[] list2 = reverse(list1); Return result public static int[] reverse(int[] list) { int[] result = new int[list.length];   for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; list 1 2 3 4 5 6 list2 result 6 5 4 3 2 1

IMPORTANT NOTICE Study the Programs that were taught in the class…All array,strings and method programs…All assignment and practical programs are included for your quiz,midterm, and final.