Presentation is loading. Please wait.

Presentation is loading. Please wait.

METHODS (FUNCTIONS) By: Lohani Adeeb khan.

Similar presentations


Presentation on theme: "METHODS (FUNCTIONS) By: Lohani Adeeb khan."— Presentation transcript:

1 METHODS (FUNCTIONS) By: Lohani Adeeb khan

2 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);

3 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);

4 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));

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

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

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

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

9 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.

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

11 Calling Methods, cont.

12 Trace Method Invocation
i is now 5

13 Trace Method Invocation
j is now 2

14 Trace Method Invocation
invoke max(i, j)

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

16 Trace Method Invocation
declare variable result

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

18 Trace Method Invocation
result is now 5

19 Trace Method Invocation
return result, which is 5

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

21 Trace Method Invocation
Execute the print statement

22 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.

23 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.

24 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.

25 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..

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

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

28 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

29 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)

30 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 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.

31 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).

32 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

33 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

34 ARRAYS By: Lohani Adeeb khan

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

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

37 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.

38 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];

39 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

40 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];

41 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];

42 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.

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

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

45 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];

46 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];

47 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];

48 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];

49 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];

50 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];

51 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];

52 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];

53 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];

54 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];

55 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];

56 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];

57 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];

58 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];

59 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];

60 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];

61 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.

62 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

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

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

65 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*/

66 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

67 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

68 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

69 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

70 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

71 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

72 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

73 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

74 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

75 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

76 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

77 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

78 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

79 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

80 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

81 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

82 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

83 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

84 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

85 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

86 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

87 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

88 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

89 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.


Download ppt "METHODS (FUNCTIONS) By: Lohani Adeeb khan."

Similar presentations


Ads by Google