Академический Документы
Профессиональный Документы
Культура Документы
Java Programming:
From Problem Analysis to Program Design,
Second Edition
Chapter Objectives
Understand how methods are used in Java
programming.
Learn about standard (predefined) methods
and discover how to use them in a program.
Learn about user-defined methods.
Examine value-returning methods, including
actual and formal parameters.
Java Programming: From Problem Analysis to Program Desig
Chapter Objectives
Explore how to construct and use a valuereturning, user-defined method in a program.
Learn how to construct and use user-defined
void methods in a program.
Explore variables as parameters.
Learn about the scope of an identifier.
Become aware of method overloading.
Java Programming: From Problem Analysis to Program Desig
Predefined Classes
Methods already written and provided by
Java.
Organized as a collection of classes (class
libraries).
To use, import package.
Method type: The data type of the value
returned by the method.
Java Programming: From Problem Analysis to Program Desig
Predefined Classes
Predefined Classes
Predefined Classes
User-Defined Methods
Value-returning methods:
Used in expressions.
Calculate and return a value.
Can save value for later calculation or print value.
Syntax
Syntax of formal parameter list:
dataType identifier, dataType
identifier,...
Syntax
Syntax of the actual parameter list:
expression or variable, expression
or variable, ...
Programming Example:
Palindrome Number
Palindrome: An integer or string that reads the
same forwards and backwards.
Input: Integer or string.
Output: Boolean message indicating whether
integer string is a palindrome.
Flow of Execution
Execution always begins with the first statement in
the method main.
User-defined methods execute only when called.
Call to method transfers control from caller to called
method.
In the method call statement, specify only actual
parameters, not data type or method type.
Control goes back to caller when method exits.
Java Programming: From Problem Analysis to Program Desig 21
Void Methods
Similar in structure to value-returning
methods.
Call to method is always stand-alone
statement.
Can use return statement to exit method
early.
//illegal declaration,
//x is already declared
...
}
}
Scope Rules
Scope rules of an identifier that is declared within a class and
accessed within a method (block) of the class.
An identifier, say X, that is declared within a method (block) is
accessible:
Only within the block from the point at which it is declared until the end
of the block.
By those blocks that are nested within that block.
Scope Rules
Example 7-12
public class ScopeRules
{
static final double rate = 10.50;
static int z;
static double t;
public static void main(String[] args)
{
int num;
double x, z;
char ch;
//...
}
public static void one(int x, char y)
{
//...
}
Scope Rules
public static int w;
public static void two(int one, int z)
{
char ch;
int a;
//block three
{
int x = 12;
//...
} //end block three
//...
}
}
Method Overloading: An
Introduction
Method overloading: More than one method can have
the same name.
Two methods are said to have different formal
parameter lists:
If both methods have a different number of formal
parameters.
If the number of formal parameters is the same in
both methods, the data type of the formal
parameters in the order you list must differ in at
least one position.
Java Programming: From Problem Analysis to Program Desig 39
Method Overloading
public
public
public
public
void methodOne(int x)
void methodTwo(int x, double y)
void methodThree(double y, int x)
int methodFour(char ch, int x,
double y)
public int methodFive(char ch, int x,
String name)
Method Overloading
public void methodSix(int x, double y,
char ch)
public void methodSeven(int one, double u,
char firstCh)
Method Overloading
Method overloading: Creating several methods within
a class with the same name.
The signature of a method consists of the method
name and its formal parameter list. Two methods have
different signatures if they have either different
names or different formal parameter lists. (Note that
the signature of a method does not include the return
type of the method.)
Method Overloading
The following method headings correctly overload
the method methodXYZ:
public
public
public
public
void
void
void
void
methodXYZ()
methodXYZ(int x, double y)
methodXYZ(double one, int y)
methodXYZ(int x, double y,
char ch)
Method Overloading
public void methodABC(int x, double y)
public int methodABC(int x, double y)
Group No
1
2
Course Average
83.71
80.82
ENG
1
2
82.00
78.20
HIS
1
2
77.69
84.15
MTH
1
2
83.57
84.29
PHY
1
2
83.22
82.60
Chapter Summary
Pre-defined methods
User-defined methods:
Value-returning methods
Void methods
Formal parameters
Actual parameters
Flow of execution
Java Programming: From Problem Analysis to Program Desig 48
Chapter Summary
Primitive data type variables as parameters:
One-way link between actual parameters and
formal parameters (limitations caused).