Download presentation
Presentation is loading. Please wait.
Published byPaige Carpenter Modified over 10 years ago
1
Lecture 10 Methods COMP1681 / SE15 Introduction to Programming
2
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
3
SE15: Methods10–3 Lecture Outline Why methods are needed Structure of a method definition Passing data into methods Returning data from methods Documenting methods
4
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?
5
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!"); }
6
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!)
7
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
8
SE15: Methods10–8 Answers Question 2 secondValue _Second_Value _2ndValue Question 3 displayResults
9
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(); } }
10
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.243-249
11
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.231-235
12
SE15: Methods10–12 Question 4 public static int minimum(int a, int b) { if (a < b) return a; else return b; }
13
SE15: Methods10–13 Documenting Methods /** * Issues a personalised greeting. * * @param name Name of person to be greeted */ public static void greet(String name) { System.out.println("Hello, " + name + "!"); }
14
SE15: Methods10–14 Documenting Methods /** * Obtains a person's name from keyboard. * * @return 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; }
15
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.