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. To be able 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 programs that use Methods from Library classes. ◦ To be able to write Methods for use in your programs. ◦ To develop an active understanding of the different kinds of method and their uses We have looked at developing algorithms for solving problems. We saw how the problem could be broken down into separate stages, then each stage was further broken down into more detailed steps. ◦ We repeat this process until we have single instructions. This is Stepwise Refinement. We can regard the first level stages as separate (smaller) programs. ◦ Similarly, we can regard any group of (related) single instructions as a small program. We are programming a robot to make a cup of tea using tea leaves and a tea pot. This can take several steps: 1. Put leaves in the pot 2. Boil some water 3. Add water to the pot 4. Wait 5 minutes 5. Pour tea into cup 5 1. Put leaves in the pot 1.1. Open tea caddy 1.2. Scoop out a spoonful of tea 1.3. Tip the spoon into the pot 1.4. Close tea caddy 2. Boil some water 2.1. Fill kettle with water 2.2. Switch on the kettle Many of the steps can be broken down into smaller steps. 2.3. Wait until water is boiled 2.4. Switch off kettle 3. Add water to the pot 6 1. Put leaves in the pot 1.1. Open tea caddy 1.1.1. Take caddy from cupboard 1.1.2. Remove lid from caddy 1.2. Scoop out a spoonful of tea 1.3. Tip the spoon into the pot 1.4. Close tea caddy 1.4.1. Put lid on caddy 1.4.2. Return caddy to cupboard 2. Boil some water 2.1. Fill kettle with water 2.1. Take kettle to the water tap 7 The small programs (or SubPrograms) described are known as Methods. The main program making tea then becomes a series of smaller SubPrograms or Methods. Making Tea 1. Put leaves in pot 2. Boil some water 3. Add water to pot 4. Wait 5. Pour tea into cup Methods In the tea-making example, each method would most likely just involve a fixedmovement by the robot. Even if sensors were required, this could all be dealt with inside the method. Methods of this kind, which ◦ do not need any data, and ◦ do not give us any results are just one of four main types… 1. 2. 3. 4. Four Types of Method exist: 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). 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). Those which ‘do something’ and then do return resulting data to the main program (data out). A combination of the last 2 – Require data from the main program and return data back to the main program (data in and out). No data in, no data out Data in, no data out ◦ e.g. the methods for the tea-making robot ◦ e.g. telling the robot to move a certain distance: robot.moveForward(50) No data in, data out ◦ e.g. getting information from the computer’s operating system: int t = currentTimeMillis() // t would contain the current time in milliseconds after this code is run Data in, data out ◦ e.g. a method to square a given number: int result = square(3); // result would contain 9 after this code is run You may not have realised it at the time but you have already used some methods… ◦ main () belongs to the class in which it is declared. ◦ println () belongs to the System.out class ◦ showInputDialog () belongs to the JOptionPane class ◦ parseDouble () belongs to the Double class The data passed into a method when it is called are known as parameters If the method gives us some data back, this is known as the return value 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 Looking at our example methods… ◦ main() has a single (optional) input parameter… an array of Strings (supplied when calling the program) we will look at arrays later on. ◦ println() has a single (optional) input parameter… a String which will be printed out on the console e.g. System.out.println("Hello World"); Input Parameter showInputDialog() requires a single Input parameter and returns a single parameter… ◦ both parameters are Strings ◦ e.g. sWidth = JOptionPane.showInputDialog(null,"Enter width."); Output Variable Input Parameter ◦ This will show the Input Dialog with the message from the Input parameter… ◦ When the user clicks the OK button the text they typed will be returned and stored in the String variable sWidth. parseDouble() requires a single Input parameter and returns a single value… ◦ the input parameter is a String ◦ the output is a double (a number) ◦ e.g. dWidth = Double.parseDouble(sWidth); Output Variable Input Parameter ◦ This will convert the text in the String sWidth into a number ◦ and return this number which will be stored in the double variable dWidth Consider the following code: public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } ‘main’ method: 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 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 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. Parameters values Are used to transfer data to a method. int calculateArea(int w, int h) { int area = w * h; return area; } int area; area = calculateArea(2, 5); System.out.println( "Area = " + area); Return value Contains the data passed from the method. Here it is copied into a variable. A call to a method called calculateArea A non-void method can be called anywhere an expression of the same type is permitted. e.g. from within calculations. 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 Variables passed TO a method as Parameters are passed by VALUE only. The name they are given in the method parameters is unique to the method ◦ even if it is the same as a variable in the main program Changes to the value of such a variable in the method are NOT ‘seen’ by the main program. ◦ consider the program and method on the next slide… … int y, result; y = 3; result = square(y); … When square() is called, the parameter x takes on the value of variable y in the main program public static int square(int x) { x = x * x; return x; Inside the } computer’s memory however, x and y are totally separate Method to square a number: public static int square(int x) { x = x * x; return x; } Main method… What would be the output? … int x, y; x = 2; y = square(x); System.out.println(x + " squared = " + y); … 2 squared = 4 In addition to variables passed in as parameters a method may also declare Local Variables ◦ These only exist in the method. ◦ They cease to exist when the method terminates. ◦ The ‘main’ program cannot ‘see’ them. Example… int highest(int x, int y) { int temp; if( x > y) { temp = x; } else { temp = y; } return temp; } Local variable Read Chapter 8 of Currie Workshop 8 on Wolf