Presentation is loading. Please wait.

Presentation is loading. Please wait.

 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.

Similar presentations


Presentation on theme: " To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods."— Presentation transcript:

1

2  To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods ◦ To be able to write Methods for use in your programs.

3  Consider the following code: public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } ‘main’ program: the entry point of our code methods that the ‘main’ method will call

4 public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } Call to sayHelloEnters the method, executes all the code, and then returns to the main program. public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); } The code which is actually run so far...

5 public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = square(2); System.out.println(result); } The code which is actually run so far...

6 public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } Call to squareEnters the method with a parameter value 2, executes all the code, and then returns to the main program with the answer 4. public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = 2 * 2; System.out.println(result); } The code which is actually run so far...

7 public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = 2 * 2; System.out.println(result); } The code which is actually run so far... 2 4

8 public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = 2 * 2; System.out.println(result); } The total code which is actually run.

9 our method does not require any parameters  The sayHello() method in more detail: public static void sayHello() { System.out.println("Hello! "); } nothing is returned name of method empty parameter list ‘()’

10  The square() method in more detail: public static int square(int x) { return x * x; } name of method this method requires an integer an integer is returned... … and the value of that integer is given by this expression

11  In our main method, we have the following code: ◦ The program will:  output the word Hello  calculate the result of 2 * 2  output the result public static void main(String args[]) { sayHello(); int result = square(2); System.out.println(result); }

12  In our main method, we now have the following code:  The program will: ◦ output the word Hello ◦ calculate the result of 2 * 2 ◦ output the result ◦ calculate the result of 5 * 5 ◦ output the result ◦ calculate the result of 34 * 34 ◦ output the result ◦ calculate the result of 2000 * 2000 ◦ output the result public static void main(String args[]) { sayHello(); int result = square(2); System.out.println(result); result = square(5); System.out.println(result); result = square(34); System.out.println(result); result = square(2000); System.out.println(result); }

13 public static void main(String args[]) { sayHello(); int result = square(2); System.out.println(result); result = square(5); System.out.println(result); result = square(34); System.out.println(result); result = square(2000); System.out.println(result); } public static int square(int x) { return x * x; } 2 4 5 25 1,156 34 2,000 4,000,000

14 int calculateArea(int w, int h) { int area = w * h; return area; } Parameters Used for passing data to a method Return type Defines the type of data to be passed from the method. Keyword void is used here if method returns no data. Return statement Used for passing data from the method. Omitted for void methods.

15 int calculateArea(int w, int h) { int area = w * h; return area; } int area; area = calculateArea(2, 5); System.out.println( "Area = " + area); Parameter values Numbers 2 and 5 are transferred to the method from main. Return value Result 10 is passed from the method into variable area. 2 & 5 10

16  Consider these two methods:  Both calculate the square of the number that is supplied by the input parameter.  The first simply prints out the computed value. ◦ i.e. the value is lost  The second returns the computed value ◦ i.e. the value can be used by the program that called the method public void square1 (int y) { System.out.println(y * y); } public int square2 (int y) { return (y * y); }

17 Four Types of Method exist: 1. Those which ‘do something’ but don’t require any data from the main program and which don’t return any data to the main program (no data in or out). 2. Those which ‘do something’ where the ‘something’ depends on data supplied by the main program but don’t return any data to the main program (data in). 3. Those which ‘do something’ and then do return resulting data to the main program (data out). 4. A combination of the last 2 – Require data from the main program and return data back to the main program (data in and out).

18  A Method may require more than one Input parameter ◦ or none at all  A Method can only return a single Output return value ◦ or none at all  Some Methods may have Optional input parameters ◦ these will take Default values if not supplied

19  Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Thursday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm.  Not necessarily lectures in the afternoons. ◦ Some modules have lectures during the morning and workshops/tutorials in the afternoons.  Module classes are mostly on the same day (but not always).  It depends on which modules that you are taking.


Download ppt " To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods."

Similar presentations


Ads by Google