Introduction to Programming (Java) 4/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction to Programming (Java) 1/28
Introduction to Programming (Java) 4/12 Method is a named block with the definition of the input and output. The Meaning: the reusability of a code. The definition includes header and body. c 2006 2008 Michal Krátký Introduction to Programming (Java) 2/28
, Header Header: type name (arguments) Where: type is a return type of the method name is the method name arguments is a comma (,) separated list of arguments between parenthesis, it may be empty Each argument has the form: type name Example: int Search(String str, char ch) c 2006 2008 Michal Krátký Introduction to Programming (Java) 3/28
, Body The body is a block, i.e. a sequence of statements between braces and. Example: public s t a t i c void main ( S t r i n g [ ] args )... c 2006 2008 Michal Krátký Introduction to Programming (Java) 4/28
Introduction to Programming (Java) 4/12 The return type is void if the method does not return any value. If the return type is not void then the method must return a value by return expression; If the return type is void we can use return; for the return from this method. Notice: In this course (we are not interested in object-oriented features of Java), we will declare all methods as public static, private static or static. c 2006 2008 Michal Krátký Introduction to Programming (Java) 5/28
, Example 4.1 Task: Write the int Search(String str, char ch) method returning the number of occurrences of the ch character in the str string. c 2006 2008 Michal Krátký Introduction to Programming (Java) 6/28
, Example 4.1 s t a t i c i n t Search ( S t r i n g s t r, char ch ) i n t number = 0 ; / / the t e s t of a l l characters for ( i n t i = 0 ; i < s t r. l e n g t h ( ) ; i ++) i f ( s t r. charat ( i ) = = ch ) number ++; return number ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 7/28
Method Invocation A method is called by its name with a comma (,) separated list of parameters in parenthesis (). i n t x, y ; x = gcd ( 2 4, 1 8 ) ; A parameter may be an arbitrary expression. All parameters are evaluated and assigned to arguments of the method. x = 0 ; y = gcd ( x + 1, 3 6 ) ; The method invocation may be used in an expression. The value of the invocation is the return value of the method. System. out. p r i n t l n ( gcd ( x + 2, 1 2 ) ) ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 8/28
Method Invocation i n t x = 0, y ; y = gcd ( x + 1, 3 6 ) ; 1 Evaluation of parameters. 2 Evaluated values are stored on a stack. Stack: 1 36 3 Method invocation. 4 Execution of the method, setting the return value. 5 The return from the method, parameters are deleted from the stack. c 2006 2008 Michal Krátký Introduction to Programming (Java) 9/28
Method Invocation, Example 4.1 Task: Call the Search method returning the number of occurrences. public class Example0401 public s t a t i c void main ( S t r i n g [ ] args ) S t r i n g s t r = " This i s the s t r i n g " ; char ch = t ; i n t number = Search ( s t r, ch ) ; System. out. p r i n t l n ( " The number of occurrences : " + number ) ; / / number = 2 c 2006 2008 Michal Krátký Introduction to Programming (Java) 10/28
Method Invocation, Example 4.1 public s t a t i c i n t Search ( S t r i n g s t r, char ch ) i n t number = 0 ; / / t e s t a l l characters of the s t r i n g for ( i n t i = 0 ; i < s t r. l e n g t h ( ) ; i ++) i f ( s t r. charat ( i ) = = ch ) number ++; return number ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 11/28
Example 4.2 Task: Write the int GetOrderFirstOne(int number) method returning the index of the lowest true bit in the number. If the number does not contain any true bit then it returns -1. Example: 16 10 = 10000 2, return: 4 127 10 = 1111111 2, return: 0 0 10 = 0 2, return: -1 c 2006 2008 Michal Krátký Introduction to Programming (Java) 12/28
Example 4.2, Method Invocation public class Example0402 public s t a t i c void main ( S t r i n g [ ] args ) i n t value1 = 1 6, value2 = 1 2 7, value3 = 0 ; System. out. p r i n t l n ( " Value : " + value1 + ", index : " + GetOrderFirstOne ( value1 ) ) ; System. out. p r i n t l n ( " Value : " + value2 + ", index : " + GetOrderFirstOne ( value2 ) ) ; System. out. p r i n t l n ( " Value : " + value3 + ", index : " + GetOrderFirstOne ( value3 ) ) ; / / Output : 4, 0, 1 c 2006 2008 Michal Krátký Introduction to Programming (Java) 13/28
Example 4.2 s t a t i c i n t GetOrderFirstOne ( i n t value ) i n t tmp = 1 ; i n t order = 1; for ( i n t i = 0 ; i < I n t e g e r. SIZE ; i ++) / / i < 32 / / t e s t a l l b i t s i f ( ( tmp & value )! = 0 ) order = i ; break ; tmp < < = 1 ; / / the sequence : 1, 2, 4, 8,... return order ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 14/28
Method Overloading can be overloaded. => A class contains more methods with the same name. must differ in the number or types of parameters. Method is selected according to the number or types of parameters. The return type need not be the same. c 2006 2008 Michal Krátký Introduction to Programming (Java) 15/28
Method Overloading public s t a t i c i n t Search ( S t r i n g s t r, char ch )... public s t a t i c i n t Search ( i n t [ ] a, i n t a )... c 2006 2008 Michal Krátký Introduction to Programming (Java) 16/28
Example 4.3 Task: Write the int Max(int []array) method returning the maximal value of the array. Example: Input: -1, 45, 78, 456, 741, -875 Output: 741 c 2006 2008 Michal Krátký Introduction to Programming (Java) 17/28
Example 4.3 public class Example0403 public s t a t i c void main ( S t r i n g [ ] args ) i n t [ ] array = 1, 4 5, 7 8, 4 5 6, 7 4 1, 8 7 5 ; System. out. p r i n t l n ( " The maximal value : " + Max( array ) ) ; / / output : 7 4 1 c 2006 2008 Michal Krátký Introduction to Programming (Java) 18/28
Example 4.3 s t a t i c i n t Max( i n t [ ] array ) i n t max = I n t e g e r. MIN_VALUE ; / / 2147483648 / / t e s t a l l array items for ( i n t i = 0 ; i < array. l e n g t h ; i ++) i f ( max < array [ i ] ) max = array [ i ] ; return max ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 19/28
Reference 1/5 A pointer (or reference) does not contain data, however it contains an address in the main memory, where data is stored. Example: / / C++ i n t a = 6 0 ; i n t pa = & a ; 0x1232 0x1235 0x1285 a 60 pa 0x1232 60 c 2006 2008 Michal Krátký Introduction to Programming (Java) 20/28
Reference, Java 2/5 Primitive vs object data types and arrays. Example: public void Compute ( ) i n t a = 6 0 ; Set ( a ) ; / / a = 60 0x1232 0x2437 60 a 60 80 public void Set ( i n t b ) b = 8 0 ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 21/28
Reference, C++ 3/5 Example: public void Compute ( ) i n t a = 6 0 ; Set (&a ) ; / / a = 80 public void Set ( i n t pa ) pa = 8 0 ; pa 0x1232 Stack: a 60 0x1232 c 2006 2008 Michal Krátký Introduction to Programming (Java) 22/28
Reference, Java 4/5 Primitive data types, like int, float, and so on, are handled by the value, arrays and instances of object data types are handled by the reference (pointer). Example: public void Compute ( ) i n t array [ ] = new i n t [ 1 ] ; array [ 0 ] = 6 0 ; Set ( array ) ; / / array [ 0 ] = 8 0 public void Set ( i n t [ ] array ) array [ 0 ] = 8 0 ; array 0x1232 Stack: 60 0x1232 c 2006 2008 Michal Krátký Introduction to Programming (Java) 23/28
Reference, Java 5/5 array 0x1232 60 public void Compute ( ) i n t array [ ] = new i n t [ 1 ] ; array [ 0 ] = 6 0 ; Set ( array ) ; / / array [ 0 ] = 6 0 Stack: 0x1282 0x1232 80 0x1282 public void Set ( i n t [ ] array ) array = new i n t [ 1 ] ; array [ 0 ] = 8 0 ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 24/28
1/4 represents a function, which calls the same function in the body. We say that the function calls itself. We show this principle at the example of factorial. The factorial function is defined: 1 pro n = 0 f (n) = f (n f (n 1)) pro n 1 c 2006 2008 Michal Krátký Introduction to Programming (Java) 25/28
2/4, Factorial f (n) = f (n f (n 1)) = f (n f ((n 1) f ((n 2)))) = = = f (n f ((n 1) f ((n 2) f (1 f (0))... )) We can follow: n! = n (n 1)! = n (n 1)(n 2)! = = n (n 1) (n 2) 1 0! c 2006 2008 Michal Krátký Introduction to Programming (Java) 26/28
3/4, Factorial s t a t i c i n t f a c t o r i a l ( i n t n ) i f ( n = = 0 ) return 1 ; else return ( n f a c t o r i a l ( n 1 ) ) ; c 2006 2008 Michal Krátký Introduction to Programming (Java) 27/28
, Nesting of the Method Invocation 4! Invocation The First Invocation, n=4 The Second Invocation, n=3 The Third Invocation, n=2 The Fourth Invocation, n=1 The Fifth Invocation, n=0 Return 1 Return n * f(n-1) = 1 * 1 Return 2 * 1 Return 3 * 2 Return 4 * 6 Finish with the value 24 c 2006 2008 Michal Krátký Introduction to Programming (Java) 28/28