Transcript lect11

COMP1681 / SE15
Introduction
to Programming
Lecture 10
Methods
Today’s 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: Methods
10–2
Lecture Outline





Why methods are needed
Structure of a method definition
Passing data into methods
Returning data from methods
Documenting methods
SE15: Methods
10–3
Why Do We Need Other Methods?

So far, we’ve been putting all our code in main…

Can you think of any problems with this?
SE15: Methods
10–4
Anatomy of a Method Definition
[ modifiers ] return-type name ( [ parameter-list ] )
{
statements
}
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
SE15: Methods
10–5
Anatomy of a Method Definition

Modifiers



Return type



public means anyone can call the method
static is needed for methods that are not called on
objects; we’ll stop using it shortly!
Specifies the type of value returned to method’s 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: Methods
10–6
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: Methods
10–7
Answers

Question 2




secondValue
_Second_Value
_2ndValue
Question 3

displayResults
SE15: Methods
10–8
A Very Simple Example
class Greeting {
public static void greet()
{
System.out.println("Hello, World!");
}
public static void main(String[] args)
{
greet();
}
}
SE15: Methods
10–9
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
SE15: Methods
10–10
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
SE15: Methods
10–11
Question 4
public static int minimum(int a, int b)
{
if (a < b)
return a;
else
return b;
}
SE15: Methods
10–12
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 + "!");
}
SE15: Methods
10–13
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;
}
SE15: Methods
10–14
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)
SE15: Methods
10–15