Tải bản đầy đủ (.pptx) (79 trang)

Chapter 5 Methods

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (378.66 KB, 79 trang )

Chapter 5

Methods


Contents
1. Introduction to Methods
2. Passing Arguments to a Method
3. More about Local Variables
4. Returning a Value from a Method
5. Problem Solving with Methods

2


1. Introduction to Methods
A method is a collection of statements that
performs a specific task.


We have experienced methods in two ways:



Creating a method named main in every program



Using predefined methods from the Java API,
such as



System.out.println



Integer.parseInt



Math.pow



3


1. Introduction to Methods
Methods are commonly used to break a problem
into small manageable pieces.


Instead of writing one long method that contains
all of the statements, several small methods that
each solve a specific part of the problem can be
written.


This approach is called divide and conquer
because a large problem is divided into several
problems.



4


1. Introduction to Methods
public class BigProblem
{
public static void main(String[]
args)
{
statement;
statement;
statement;
statement;
statement;
statement;
statement;
statement;
statement;
statement;
statement;
statement;
statement;
statement;
statement;
statement;
statement;
}
}


public class DividedProblem
{
public static void main(String[]
args)
{
statement;
statement;
statement;
}
public static void method2()
{
statement;
statement;
statement;
}
public static void method3()
{
statement;
statement;
statement;
}
public static void method4()
{
statement;

5


1. Introduction to Methods

Another reason to write methods is that they
simplify programs.


If a specific task is performed in several places in
a program, a method can be written once and
then be executed anytime it is needed.


This benefit of using methods is known as code
reuse.


6


void Methods and ValueReturning Methods
Two general categories of methods:



void methods



Value-returning methods



A void method performs a task and then

This method displays the value on
terminates.
the screen, and then terminates.


int number = 7;
System.out.println(number);
number = 0;
7


void Methods and ValueReturning Methods
A value-returning methods not only performs a
task, but also sends a value back to the code that
called it.


int number;
String str = “700”;
number = Integer.parseInt(str);
This method converts the string to a number,
and then returns the number to this line of code.

8


Defining a void Method
To create a method we must write its definition,
which consists of two general parts:



A header



Appears at the beginning of a method definition



Header

public static void main(String[] args)

Lists several important
things
about
the
method
{



Body

Method Modifiers}



System.out.println(“Hello World!”);


Return type



Method's name



Arguments



9


Defining a void Method
Return Type
Method Modifiers

Method Name
Parentheses

public static

void

displayMessage ()

{


The method header is never te

System.out.println(“Hello from the displayMessage method.”);
}
Method Modifiers:
- public: The method is publicly available to code outside the class
- static: The method belongs to the class, not a specific object.
Return Type:
- void: The method is a void method, and does not return a value.
Method Name: A descriptive name that can describes the function of the method.
Parentheses:
- The method name is always followed by a set of parentheses.

10


Calling a Method
A method executes when it is called.



The main method is automatically called when a
program starts.


Other methods are executed by method call
statements.


When a method is called, the JVM branches to

that method and executes the statements in its
body.


Method call statement:
- The statement is simply the name of the method followed by a set of parentheses.
- The method modifiers and the void return type are not written.

displayMessage();

11


Calling a Method

12


Branching in the
SimpleMethod.java Program
public static void main(String[] args)
{
System.out.println("Hello from the main method.");
displayMessage();
System.out.println("Back in the main method.");
}

public static void displayMessage()
{
System.out.println("Hello from the displayMessage method.");

}
13


Calling a Method

14


Calling a Method
Problem: Write a program to ask the user to enter
his annual salary and credit rating. The program
determines whether the user qualifies for a credit
card. One of two void methods, qualify or
noQualify, is called to display a message.


Credit rating:



1 (very bad) through 10 (excellent)



The user qualifies for a credit card if his salary is
not less than $20,000 and his credit rating is not
15
less than 7.




Calling a Method

16


Calling a Method

17


Checkpoint
5.1



5.2



5.3



5.4



5.5




18


Hierarchical Method Calls
Methods can also called in a hierarchical, or
layered fashion.


Method A can call method B, which can call
method C.


When method C finishes, the JVM returns to
method B.


When method B finishes, the JVM returns to
method A.


19


Hierarchical Method Calls

20



Using Documentation Comments
with Methods
You should always document a method by writing
comments that appear just before the method's
definition.


The comment should provide a brief explanation
of the method's purpose.


Recall that documentation comments begin
with /** and end with */. These types of
comments can be read and processed by a
program named javadoc which produces
attractive HTML documentation.


21


2. Passing Arguments to a
Method
Values that are sent into a method are called
arguments.


System.out.println(“Hello World!”);
This call statement passes “Hello World!” as

an argument.


number = Integer.parseInt(str);
This call statement passes the contents of the
str variable as an argument.


22


2. Passing Arguments to a
Method
A parameter variable, sometimes simply referred
to as a parameter, is a special variable that holds
a value being passed into a method.


public static void displayValue(int num)
{
System.out.println(“The value is “ + num);
}

int num



declaration of an parameter variable




23

enables the displayValue method to accept an




2. Passing Arguments to a
Method
public static void displayValue(int num)
{
System.out.println(“The value is “ + num);
}

A call to the displayValue



The argument 5 is copied into
the parameter variable num.

displayValue(5);

This statement executes the displayValue
method.


Statements call the displayValue method with
24

various arguments passed:



2. Passing Arguments to a
Method

25


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×