Transcript method

CHAPTER 3
METHODS
Introduction
• A method is also called as a function or an operation.
• A method is a block of statements that is executed to
perform specific task when it is called.
• To execute a method, we must call it.
• There are 2 types of method:
 User defined method
 Pre-defined method
User defined method
• User can create a method using the following format:
Modifier ReturnValueType MethodName (formal parameter list)
{
Body of a method;
return statement;
}
• Example:
public static int square(int x)
Modifier
• The modifier in the function header is optional.
• The purpose is to specify the way the method can be
accessed.
• There are different kinds of modifier such as:
Modifier
 The ReturnValueType specifies the data type of value
that the method can return.
 If the ReturnValueType is void, this means that the method
does not return any value.
 If the ReturnValueType is other than void, then, the
method must have a return statement.
• The method may have list of parameters to be used.
However, if the is no parameters, then, the parameters
should be written as ‘void’ or left blank. The parameters
listed at method header are called formal parameter.
Writing Method Header
Below are examples of how function header can be written
from function description.
• Method square( ) is a public static method that takes an
integer parameter and returns an integer value
public static int square(int x)
• Method mult( ) is a public method takes 3 parameters of
type float and returns a floating point value
public float mult( float a, float b, float c)
Writing Method Header
• method Total( ) is a public method that takes 2 integer
parameter and does not return any value
public void Total(int x, int y)
• method DisplayValue( ) is a public static method does not
accept any parameter and does not return any value
public static void DisplayValue( )
Notice that there is no semicolon at the end of function header.
Calling a Method
Calling a public static method
 Since a static method is free from object, the method
can be executed directly without the use of object.
Calling a non-static method
 Any non-static method must be accessed via object of
the class. This means the object must be declared first
prior to calling the method.
Passing Parameters
• When calling a method that accepts parameters, we must
pass the actual parameters in the same order, same
number and compatible type as formal parameters.
• Formal parameters are parameters listed at method
header.
• Actual parameters are parameters listed at method call.
Method Overloading
• Overloading methods refer to more than one
method that shares the same name but different
parameter list defined in a class.
• Example:
#Module page 50
Predefined Methods
• Pre-defined method refers to the methods in Java API.
• The method that are present in the Java Class Libraries that
we can use.
• Using Java Predefined Classes, Included in the Java SDK are
more than 2,000 classes that can be used to add
functionality to your programs APIs (the complete list is
published on www.java.sun.com.
Predefined Methods
• Here are some classes that we can use:








The String Class //we use some of it
Using System.out //we use this already
Formatting Output
The Math Class
The Wrapper Classes //we use it already...not all
Dialog Boxes //we use it in input/output dialog
System.in object //we use it in input/output
Console Input Using the Scanner Class
Predefined Methods
• Classes are grouped in packages according to functionality
here are some packages examples:
Predefined Methods
• Classes in java.lang are automatically available to use.
• Classes in other packages need to be "imported" using this
syntax:
import package.ClassName;
or
import package.*;
Example:
import java.utils.*;
JOptionPane
• JOptionPane makes it easy to pop up a standard dialog
box that prompts users for a value or informs them of
something.
• While the JOptionPane class may appear complex because
of the large number of methods, almost all uses of this
class are one-line calls to one of the static showXxxDialog.
• Example:
showMessageDialog ( )
showInputDialog ( )
JOptionPane
String class
• String class contains methods that can be used to
manipulate string.
• A string refers to a sequence of characters enclosed in
double quotes.
• Example:
#Module page 56
The Wrapper Class
• A wrapper class is a class that surrounds a relatively
simple item in order to add functionality to the
simple item.
• fWraps the value of primitive data type into an object
which is useful when methods require an object
argument and also useful for converting Strings to an int
or double.
The Wrapper Class
• Here are wrapper classes for some of the Java primitive types:
Wrapper Class
Primitive Type
Integer
int
Long
long
Float
float
Double
double
Character
char
• Note that the wrapper class names are the same as the primitive
names except for the uppercase first letter.
• The wrapper classes are defined in the java.lang package. The
Java compiler automatically imports all the classes in the
java.lang package, so there's no need to import the wrapper
classes explicitly.
The Wrapper Class
• With GUI programs, all numeric output is string based.
• So to display a number, you need to convert the number to
a string prior to calling the GUI display method.
• All numeric input is string based, too.
• So to read a number in a GUI program, you first read the
input as a string and then convert the string to a number.
The Wrapper Class
Here are string conversion methods provided by the numeric
wrapper classes:
The Wrapper Class
• Type of Wrapper class
The Math Class
• All methods of math class are static.
• Unlike String class, the math class methods are accessible
without object. (#Module page 62)
Let’s Try !
 What are the two types of method?
 Give 3 examples of pre-defines method.