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. Consider the following code: public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } ‘main’ program: the entry point of our code public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } } methods that the ‘main’ method will call public class MethodDemo { Call to sayHello public static void main (String args[]) { sayHello(); Enters the int result = square(2); System.out.println(result); method, executes } public static void sayHello() { System.out.println("Hello! "); } all the code, and then returns to the main program. public static int square(int x) { return x * x; } } The code which is actually run so far . . . public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); } } 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; } } The code which is actually run so far . . . public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = square(2); System.out.println(result); } } 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; } } The code which is actually run so far . . . public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = 2 * 2; System.out.println(result); } } Call to square Enters 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[]) { sayHello(); int result = square(2); System.out.println(result); } 4 2 public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } } The code which is actually run so far . . . public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = 2 * 2; System.out.println(result); } } 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; } } The total code which is actually run. public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = 2 * 2; System.out.println(result); } } The sayHello() method in more detail: nothing is returned name of method empty parameter list ‘()’ public static void sayHello() { System.out.println("Hello! "); } our method does not require any parameters The square() method in more detail: an integer is returned... name of method public static int square(int x) { return x * x; } … and the value of that integer is given by this expression this method requires an integer In our main method, we have the following code: public static void main(String args[]) { sayHello(); int result = square(2); System.out.println(result); } ◦ The program will: output the word Hello calculate the result of 2 * 2 output the result In our main method, we now have the following code: The program will: ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ 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); } 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[]) { 4 2 sayHello(); int result = square(2); public static int square(int x) { System.out.println(result); return x * x; 5 25 result = square(5); } System.out.println(result); 34 2,000 1,156 result = square(34); System.out.println(result); result = square(2000); System.out.println(result); } 4,000,000 Parameters Used for passing data to a method int calculateArea(int w, int h) { int area = w * h; return area; 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. Parameter values Numbers 2 and 5 are transferred to the method from main. int calculateArea(int w, int h) { int area = w * h; return area; } 2&5 10 Return value Result 10 is passed from the method into variable area. int area; area = calculateArea(2, 5); System.out.println( "Area = " + area); Consider these two methods: public void square1 (int y) { System.out.println(y * y); } public int square2 (int y) { return (y * y); } 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 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). 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 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.