You are on page 1of 13

User Defined Methods

 All of the programs so far have consisted of only


one method, i.e. main().
 In reality this is not very practical.
 As programs get bigger it is essential to split
them into small manageable chunks.
 These 'chunks' are called methods. A class
typically consists of many methods.

1
User Defined Methods
 Methods with no argument and no return value
 A method that takes no arguments and returns
nothing is the simplest of all examples.
 In this example the method called sayHello()
simply prints a message to the screen.

2
User Defined Methods
// A method with no arguments and no return value
import javax.swing.JOptionPane;
public class Methods1 {

public static void main( String args[]) {


sayHello(); // calls the method
}

  // method definition
public static void sayHello() {
String output = "Hello";
// print message
JOptionPane.showMessageDialog(null, output, "Methods",
JOptionPane.INFORMATION_MESSAGE);
}
} // end class 3
User Defined Methods
 Running the program only produces a window as
shown below, the important point is that a
method was called.

4
User Defined Methods
 Methods with a return value but no argument
 Let's look at another fairly simple method that
takes no arguments but this time a value is
returned.

5
User Defined Methods
// A method with no arguments and a return value
 import javax.swing.JOptionPane;
public class Methods2 {

public static void main( String args[]) {


String output;
int yourAge;
 
// call method
yourAge = readAge();
 
// print message
output = "You are " + yourAge + " years old";
JOptionPane.showMessageDialog(null, output, "Methods",
JOptionPane.INFORMATION_MESSAGE);
 
6
} // end main
User Defined Methods
// method definition
public static int readAge() {
String ageAsString;
int age;
 
// read in age
ageAsString = JOptionPane.showInputDialog("please enter your age");
  // convert string to integer
age = Integer.parseInt(ageAsString);
 
return age;
} // end method definition

} //end class

7
User Defined Methods
 Below is a sample of running the program

8
User Defined Methods
 In this example the method takes one argument
but does not return a value.

9
User Defined Methods
// A method with one argument and no return value
 import javax.swing.JOptionPane;
public class Methods3 {
public static void main( String args[]) {
String yourAgeString;
int yourAge;
 
// read in age
yourAgeString = JOptionPane.showInputDialog("please enter your age");
  // convert string to integer
yourAge = Integer.parseInt(yourAgeString);
 
// call method
printAge(yourAge);
 
} // end main
10
User Defined Methods
// method definition
public static void printAge(int anAge) {
String output;
 
// print message
output = "You are " +anAge +" years old";
JOptionPane.showMessageDialog(null, output, "Methods",
JOptionPane.INFORMATION_MESSAGE);
} // end method

} // end class

11
User Defined Methods
 Below is a sample of running the program

12
User Defined Methods
 Notice that the list of arguments in the
method must contain the argument type
followed by the name
public static void printAge(int age);

Argument type Argument name

13

You might also like