Lecture 10 Methods COMP1681 / SE15 Introduction to Programming
SE15: Methods10–2 Todays Learning Objectives For you to appreciate the benefits of dividing a program up into a set of methods For you to see how methods are defined, invoked and documented in Java
SE15: Methods10–3 Lecture Outline Why methods are needed Structure of a method definition Passing data into methods Returning data from methods Documenting methods
SE15: Methods10–4 Why Do We Need Other Methods? So far, weve been putting all our code in main… Can you think of any problems with this?
SE15: Methods10–5 Anatomy of a Method Definition [ modifiers ] return-type name ( [ parameter-list ] ) { statements } public static void main(String[] args) { System.out.println("Hello, World!"); }
SE15: Methods10–6 Anatomy of a Method Definition Modifiers public means anyone can call the method static is needed for methods that are not called on objects; well stop using it shortly! Return type Specifies the type of value returned to methods caller void must be used if method returns no value Parameter list Allows us to pass data into a method Optional (except for main!)
SE15: Methods10–7 Method Names Rules Name must begin with letter or underscore Other characters can be letters, digits or underscore Convention Use camel case with initial lower-case letter Use verbs or verb phrases Exception: mathematical functions Exception: methods that return a boolean (true/false) value
SE15: Methods10–8 Answers Question 2 secondValue _Second_Value _2ndValue Question 3 displayResults
SE15: Methods10–9 A Very Simple Example class Greeting { public static void greet() { System.out.println("Hello, World!"); } public static void main(String[] args) { greet(); } }
SE15: Methods10–10 Passing Data to Methods Method definition can have a list of formal parameters Parameters act as placeholders for data that are actually passed to the method when it is called Values passed to the method are called arguments Each parameter has a name and a type public static void greet(String name) { System.out.println("Hello, " + name + "!"); } Savitch, p
SE15: Methods10–11 Returning Data From Methods Method must have a non-void return type Body of method must include a return statement public static String getName() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter your name"); String name = keyboard.next(); return name; } Local variables Savitch, p
SE15: Methods10–12 Question 4 public static int minimum(int a, int b) { if (a < b) return a; else return b; }
SE15: Methods10–13 Documenting Methods /** * Issues a personalised greeting. * name Name of person to be greeted */ public static void greet(String name) { System.out.println("Hello, " + name + "!"); }
SE15: Methods10–14 Documenting Methods /** * Obtains a person's name from keyboard. * Name of person */ public static String getName() { Scanner keyboard = new Scanner(System.in); System.out.println("Enter your name"); String name = keyboard.next(); return name; }
SE15: Methods10–15 Summary We have Recognised a need to manage the complexity of programs using methods Examined the syntax of method definitions Considered how methods should be named Looked at how parameter lists allow data to be passed into a method Looked at how a method can return a value to its caller Seen how doc comments can be used to provide documentation for methods (and the enclosing class)