1 / 27

ICS102 Lecture 13 : Methods

Learn about the structure and usage of methods in Java programming. Understand the concept of methods, method definition and invocation, return statements, local variables, method parameters, and array parameters.

bryantdavid
Download Presentation

ICS102 Lecture 13 : Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department ICS102 Lecture 13 : Methods

  2. A Java Program A Java program consists of one or more classes A Java class consists of one or more methods A Java method consists of one or more statements A Java Program Java classes Java Methods

  3. - What is a method … • A Java application program consists of one or more classes. • Each class has one or more methods • Each method consists of one or more statements • Each method has a name. • One of the methods must be called main • When a Java application program is run, the run-time system automatically invokes the method named main • All Java application programs start with the main method

  4. … - What is a method … public class class-name { method1 method 2 method 3 … … method n }

  5. Example of a Java Program

  6. Example of a Java Program Class name Main method Class body Instruction

  7. Exercise • Write a program that computes the factorial (n!) of 6 then the factorial of 3, then the factorial of 10.

  8. Method definition Exercise • Write a program that computes the factorial (n!) of 6 then the factorial of 3, then the factorial of 10. Method invocation Return type Method parameter Return instruction

  9. -- Method Structure public or private<static> <void or typeReturned>myMethod(<parameters>) { statement statement statement … … statement } Method name If method doesn’t return value Type of the return value Variable list Method body

  10. - Invoking a Methods • The statements inside a method body are executed when the corresponding method is called from another method. • Calling a method is also called invoking a method • Each time the method is invoked, its corresponding body is executed

  11. - return Statements … • The body of a method that returns a value must also contain one or more return statements • A return statement specifies the value returned and ends the method invocation.

  12. … - return Statements • A void method need not contain a return statement, unless there is a situation that requires the method to end before all its code is executed • Example : write a method that prints all the numbers between 10 and 30

  13. - Local Variables • A variable declared within a method definition is called a local variable • All variables declared inside the method are local variables • All method parameters are local variables

  14. - Local Variables • If two methods each have a local variable of the same name, they are still two entirely different variables. Example: Local variable in main method Local variable in factorial method Local variable in addition method

  15. - Method Parameters … • A parameter list provides a description of the data required by a method • It indicates the number and types of data pieces needed, the order in which they must be given, and the local name for these pieces as used in the method public double myMethod(int p1, int p2, double p3) • Example: What is the parameter list of a method division that divides two integers and returns the result (double)? Parameters list

  16. … - Method Parameters … • When a method is invoked, the appropriate values must be passed to the method in the form of arguments • The number, order, and types of the arguments must exactly match that of the parameter list Error: type mismatch

  17. … - Method Parameters … • Is this program correct? Yes it is! .. Details in the next slide ..

  18. … - Method Parameters … • If argument and parameter types do not match exactly, Java will attempt to make an automatic type conversion • In the preceding example, the int value of argument a would be cast to a double • A primitive argument can be automatically type cast from any of the following types, to any of the types that appear to its right: byteshortintlongfloatdouble char

  19. … - Method Parameters … • A parameters is often thought of as a blank or placeholder that is filled in by the value of its corresponding argument • However, a parameter is more than that: it is actually a local variable • When a method is invoked, the value of its argument is computed, and the corresponding parameter (i.e., local variable) is initialized to this value • Even if the value of a formal parameter is changed within a method (i.e., it is used as a local variable) the value of the argument cannot be changed

  20. Call-by-Value Example Prints 10 Prints 10

  21. - Method Parameters: Array Parameters • Methods can have array parameters. • Example: Write a program that prints the values of an array of integers, then increments each value of the array by two and then prints the new values of the same array. A parameter of type array

  22. - Details • Array arguments are always passed by reference. This means any changes done to an array element by the invoked method, will also change the corresponding array element in the caller method. • Example: Public static void m1() { int [] A = { 1, 2, 3}; m2(A); } public static void m2(int [] A) { A[1] = 5}; • Method m2 changes the value of a[1] to 5; as a result the value of a[1] in m1 will also change to 5.

  23. - Method Overloading … • In java the same class can have methods with the same name. Such methods are called overloaded methods. • Overloaded methods must differ in the number or type of their arguments. • The name of a method together with the number, order, and types of its arguments are called the method signature. No two methods of the same class must have the same signature. • The compiler treats overloaded methods as completely different methods. It knows which one to call by using method signatures.

  24. - Method Overloading … These 3 methods have the same name but different signatures

  25. The end Important to do at home : - read pages 206-212

  26. Exercises • Write a method that takes as input an integer value and returns the sum of all integers less than that value. For example, if the input is 6, the output is 5+4+3+2+1 = 15. If the input is negative, the output should be -1. • Write a method that takes as input an array of integers and returns the average of the values in the array.

  27. Exercises • Write a method public void printTriangleNumbers(int n) such that: • The call: printTriangleNumbers(5) prints the following on the screen: • 1 2 3 4 5 • 1 2 3 4 • 1 2 3 • 1 2 • 1 • The call: printTriangleNumbers(6) prints the following on the screen: • 1 2 3 4 5 6 • 1 2 3 4 5 • 1 2 3 4 • 1 2 3 • 1 2 • 1 • The call: printTriangleNumbers(3) prints the following on the screen: • 1 2 3 • 1 2 • 1

More Related