Вы находитесь на странице: 1из 23

Chapter 5 METHODS

METHODS
It is common to break programs into manageable methods. A method is a named block of statements that performs a specific task.

void METHODS AND VALUE-RETURNING METHODS


There are two general categories of methods: void methods and value-returning methods. Methods that do not return a value are called void methods. A value-returning method not only performs a task, but also sends a value back to the code that called it.

void METHODS AND VALUE-RETURNING METHODS


Methods that do not return a value are called void methods. The System.out.println method is an example of a void method. It performs its job, displaying a string of characters in a console window, and terminates.

void METHODS AND VALUE-RETURNING METHODS


The statements below call the void method System.out.println to display a string of characters in a console window.
int number = 10; System.out.println("The number is: " + number);

void METHODS AND VALUE-RETURNING METHODS


A value-returning method not only performs a task, but also sends a value back to the code that called it. The Integer.parseInt method is a value-returning method. It does its job of converting the String it receives as its argument to an int and then returns this value back to the code that called it. The code that receives the value typically does something with it like assigning it to a variable, using it in a calculation, displaying it, writing it to a file, etc.

void METHODS AND VALUE-RETURNING METHODS


For example, the statements below call the Integer.parseInt method to convert a string to an integer, assign the integer returned by the method to the variable number, and then increases the contents of the variable named number by 5.
String numberString = "15"; int number; number = Integer.parseInt(numberString); number += 5;

DEFINING A METHOD
To create a method you write a method definition. A method definition includes the method header and the method body. The method header, which is the beginning of the method definition, gives important information about the method, including its name. The body is the group of statements, enclosed in braces, which perform the methods operation.

Every Java application program must include the definition of a method named main. Java programs can contain multiple methods.
8

DEFINING A METHOD
Here is an example of the definition of a simple method called displayMenu.
/** */ public static void displayMenu( ) This is the header of the method named displayMenu. { System.out.println( ); System.out.println("\t\tHealth Club Membership Menu\n"); System.out.println("1. Standard Adult Membership"); System.out.println("2. Child Membership"); System.out.println("3. Senior Citizen Membership"); System.out.println("4. Quit the Program\n"); System.out.print("Enter your choice: "); return; }
9

A method that displays the Health Club Membership Menu in a console window.

DEFINING A METHOD
The key words public and static are method modifiers. Every method we write in this class will begin with these modifiers.
Method modifiers

public static void displayMenu( ) { System.out.println( ); System.out.println("\t\tHealth Club Membership Menu\n"); System.out.println("1. Standard Adult Membership"); System.out.println("2. Child Membership"); System.out.println("3. Senior Citizen Membership"); System.out.println("4. Quit the Program\n"); System.out.print("Enter your choice: "); return; }
10

DEFINING A METHOD
The return type of the method immediately precedes the method name in the method header. The return type is the data type of the value returned by the method. Here the return type is void. This means the method is a void method (it does not return a value).
Return type

public static void displayMenu( ) { System.out.println( ); System.out.println("\t\tHealth Club Membership Menu\n"); System.out.println("1. Standard Adult Membership"); System.out.println("2. Child Membership"); System.out.println("3. Senior Citizen Membership"); System.out.println("4. Quit the Program\n"); System.out.print("Enter your choice: "); return; }
11

DEFINING A METHOD
The name of the method is displayMenu. The name of a method is another type of identifier, so the rules discussed previously for naming variables also apply to method names.
Method name

public static void displayMenu( ) { System.out.println( ); System.out.println("\t\tHealth Club Membership Menu\n"); System.out.println("1. Standard Adult Membership"); System.out.println("2. Child Membership"); System.out.println("3. Senior Citizen Membership"); System.out.println("4. Quit the Program\n"); System.out.print("Enter your choice: "); return; }
12

DEFINING A METHOD
The method name is followed by a parenthesized parameter list. The parameter list is a list of variables that hold the values passed into the method. The parameter list below is empty. The method displayMenu does not The parameter list accept any arguments.
public static void displayMenu( ) { System.out.println( ); System.out.println("\t\tHealth Club Membership Menu\n"); System.out.println("1. Standard Adult Membership"); System.out.println("2. Child Membership"); System.out.println("3. Senior Citizen Membership"); System.out.println("4. Quit the Program\n"); System.out.print("Enter your choice: "); return; }
13

DEFINING A METHOD
Notice that there is no semicolon following the method header.
Do not put a semicolon here. The public static void displayMenu( ) complete method definition includes the { body of the method. System.out.println( ); System.out.println("\t\tHealth Club Membership Menu\n"); System.out.println("1. Standard Adult Membership"); System.out.println("2. Child Membership"); System.out.println("3. Senior Citizen Membership"); System.out.println("4. Quit the Program\n"); System.out.print("Enter your choice: ");

return; }

14

DEFINING A METHOD
Even if the body of the method consists of a single statement, the statement must be enclosed in braces.
public static void displayMenu( ) { System.out.println( ); System.out.println("\t\tHealth Club Membership Menu\n"); System.out.println("1. Standard Adult Membership"); System.out.println("2. Child Membership"); System.out.println("3. Senior Citizen Membership"); System.out.println("4. Quit the Program\n"); System.out.print("Enter your choice: "); return; }

This is the body of the method.

15

DEFINING A METHOD
It is good programming style to give methods meaningful names, indent the statements in the body of the method one level from the header, and include a paragraph before each method describing the purpose of this method. This paragraph should document the data type and usage of each argument and the data type and purpose of the return value.
/** */ A method that displays the Health Club Membership Menu in a console window.

public static void displayMenu( ) { System.out.println( ); System.out.println("\t\tHealth Club Membership Menu\n"); System.out.println("1. Standard Adult Membership"); System.out.println("2. Child Membership"); System.out.println("3. Senior Citizen Membership"); System.out.println("4. Quit the Program\n"); System.out.print("Enter your choice: "); return; }

16

DEFINING A METHOD
A method named getMenuChoice is defined below. This method gets a menu choice of either 1, 2, 3, or 4 from the user and returns the choice.
public static int getMenuChoice( ) { int menuChoice; Scanner keyboard = new Scanner(System.in); menuChoice = keyboard.nextInt( ); // Validate the menu selection. while(menuChoice < 1 || menuChoice > 4) { System.out.println("\nError: Valid menu choices are 1, 2, 3, or 4."); displayMenu( ); // Call the method to display the menu menuChoice = keyboard.nextInt( ); } return menuChoice; }

17

DEFINING A METHOD
Notice that the return type for this method is int.
public static int getMenuChoice( ) { int menuChoice; Scanner keyboard = new Scanner(System.in); menuChoice = keyboard.nextInt( ); // Validate the menu selection. while(menuChoice < 1 || menuChoice > 4) { System.out.println("\nError: Valid menu choices are 1, 2, 3, or 4."); displayMenu( ); // Call the method to display the menu menuChoice = keyboard.nextInt( ); } return menuChoice; }

18

DEFINING A METHOD
The method returns an integer value to the location where it was called from via the statement return menuChoice; The word return is a Java key word.
public static int getMenuChoice( ) { int menuChoice; Scanner keyboard = new Scanner(System.in); menuChoice = keyboard.nextInt( ); // Validate the menu selection. while(menuChoice < 1 || menuChoice > 4) { System.out.println("\nError: Valid menu choices are 1, 2, 3, or 4."); displayMenu( ); // Call the method to display the menu menuChoice = keyboard.nextInt( ); } return menuChoice; }

19

CALLING A METHOD
A method executes when it is called. The method main is called automatically when the program is executed. We call methods by inserting method calls in our program. A method call consists of the name of the method followed by a parenthesized list of arguments. Arguments are values sent into the method.

20

CALLING A METHOD
The method displayMenu does not expect any input values, so it is called with an empty argument list. We can call displayMenu by including the following method call in our program: displayMenu( ); This method call ends with a semicolon, because it is a complete statement.

21

CALLING A METHOD
When a method is called, execution branches to the method and executes the statements in the body of the method. Once the method has finished executing, execution goes back to the location of the method call.

22

*** See the program ClubMembership.java which is available on webct.

23

Вам также может понравиться