[go: up one dir, main page]

0% found this document useful (0 votes)
72 views27 pages

3.5.1 Method

Methods are reusable blocks of code that perform specific tasks and define the behavior of classes; there are two types of methods - standard library methods that are built-in to Java and user-defined methods created by programmers; a method definition includes the return type, method name, parameters, and method body between curly braces.

Uploaded by

Fatin Aqilah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views27 pages

3.5.1 Method

Methods are reusable blocks of code that perform specific tasks and define the behavior of classes; there are two types of methods - standard library methods that are built-in to Java and user-defined methods created by programmers; a method definition includes the return type, method name, parameters, and method body between curly braces.

Uploaded by

Fatin Aqilah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

3.

5 METHODS
3.5.1 INTRODUCTION TO JAVA METHOD
Learning Outcomes
• At the end of this topic, you should be able to:
• Explain the meaning and the use of methods.
• Explain types of method:
i. Standard library
ii. User-defined
• Explain the general structure of A user-defined method (method name, type, parameters)
• Explain method return types based on return values.
METHODS : MEANING
• A method is a component of a
program / small program
written to perform a specific
task.

• Methods are bound to a class and


they define the behavior of a class.

Note: ‘Method’ and ‘function’ mean the same in Java


ADVANTAGES OF USING METHODS
1. Code reusability
• The code written once can be used many
times anywhere in the program.

2. Make code more readable and easier to


debug
• As the program is segregated into different
smaller modules, locating the errors and
correcting it is easy.

Disadvantages of Methods : It takes initially a little more time to set them up


TYPES OF METHODS
Two (2) types of method:
a) Standard library methods b) User-defined methods
STANDARD LIBRARY METHODS
• The standard library methods are built-in methods in java and
part of the compiler package.
• For example,
i. Math.sqrt(x)
ii. Math.pow(x,y)
iii. String chartAt(index), length ( )
iv. Scanner methods – next( ), nextInt( ), nextDouble( ), nextBoolean.
v. System methods – print( ), println( )
STANDARD LIBRARY METHODS

If a Java program wants to use a


method in Java library, the Java
program must first import the
containing class.
EXAMPLE
USER-DEFINED METHODS

• A method created by the user /


programmer. These methods take-on
names that user / programmer assign
to them and perform tasks that user /
programmer create.
STANDARD LIBRARY vs USER-DEFINED

Standard Library Methods


GENERAL STRUCTURE OF A USER-DEFINED METHOD
<Modifier> <Return value type> <Method Name> (<data type> <var1>, <data type> <var2>)
{
statement (s);
} Parameter

Content Description
Modifier It defines the access type of the method and it is
optional to use.(public static or private static)
Return value Method may return a value.
type
Method Name This is the method name. The method signature
consists of the method name and the parameter list.

Parameters Parameters are the variables in the method definition.


Method body The method body defines what the method does with
the statements.
EXAMPLE : PUBLIC METHOD
Method task : To find the maximum
number.
<Return value type> <Method Name> (<data type> <var1>, <data type> <var2>)
{
statement (s);
}

double Max (double x, double y)


{
double result = x;
- Parameter
if (x <y)
result = y;
return result;
}
EXAMPLE 1

Method task : To calculate the area of a Method task : To calculate the volume of a
circle cylinder

double Area (double radius) double Volume (double r, double h)


{ {
double value; double vol;
value = 3.14 * radius * radius; vol = 3.14 * r * r * h;
return value; return vol;
} }
METHOD RETURN TYPES
Two (2) types :
a) Do not return a value b) Return a value
• A method that returns a value must
• A void method – uses the keyword
specify the type of that value in its
void as return type in its heading to
heading.
show that it does not return a value.

void displayLine ( ) double max (double x, double y)


{ {
for(int i=1; i<=80; i++) double result = x;
System.out.print(“ – “); if(x<y)
System.out.println(“ “); result = y;
} return result;
}
EXAMPLE METHOD NOT RETURN A VALUE
A void method – uses the keyword void as return type in its heading to show that it does not
return a value.

Method task : To display / print name and age


Method task : To find the area of circle

void WriteOuput (String name, int age)


void Area (double radius)
{
System.out.println(“Name: “ + name); {
System.out.println(“Age: “ + age); System.out.println(“The area of circle is ” + (3.14 * radius * radius));
} }
EXAMPLE METHOD RETURN A VALUE
A method that returns a value must specify the type of that value in its heading.

Method task : To calculate the Volume of a cylinder


Method task : To calculate the area of a circle
double Volume (double r, double h)
int Area (int radius)
{
{
double vol;
int value;
vol = 3.14 * r * r * h;
value = 3.14 * radius * radius;
return vol;
return value;
}
}
3.5 METHODS
3.5.2 METHOD DEFINITIONS AND CALLS
Learning Outcomes
• At the end of this topic, you should be able to:
• Explain types and use of statements:
i. Method definition, and
ii. Method calls
• Explain the general format of method definition and method call.
METHOD DEFINITIONS

From previous knowledge, a method must first be defined before it is called.


Method definition consists of:
1. Return type Returntype Methodname (parameters)

2. Method name {
// Method-body
3. Parameters
}
METHOD DEFINITIONS
Returntype Methodname (parameters)
1. Return Type
{

// Method-body
• Return Type is based on the type of value returned by the method
}
• From previous slide

Method task : To calculate the volume of a


cylinder

double Volume (double r, double h)


{
• A method can return a :-
double vol; ▪ Primitive(basic) data types (int, double, boolean, and char).
vol = 3.14 * r * r * h;
return vol;
} Note: If the method does not return a value, its return type is Void.
METHOD DEFINITIONS Returntype Methodname (parameters)

// Method-body
2. Method name
}
• The name of the method is an identifier.
• You can give any name to a method. However, Method task : To calculate the area of a
it is more conventional to name it after the
circle
tasks it performs. Usually represents the task
performed by the method. int Area (int radius)
{
• For example: int value;
• Comparenumbers, Computeprice, value = 3.14 * radius * radius;
Calculatearea, and Findminimum. return value;
}
METHOD DEFINITIONS Returntype Methodname (parameters)

// Method-body

}
3. Parameters
• Parameters are the variables in the Method task : To calculate the area of a
method definition circle

int Area (int radius)


{
int value;
value = 3.14 * radius * radius;
return value;
}
Parameter Parameter
int max (int x, int y) private double volume (double r, double h)
Example : { {
…… …….
} }
EXAMPLE OF METHOD DEFINITION (WITH AND WITHOUT RETURN VALUE)

double Max (double x, double y)


{ void DisplayMessage( )
double result = x; {
if (x <y) System.out.print(“Hello, World!”);
result = y; }
return result; Definition Details

} Return type void : did not return any values


Method Name DisplayMessage
Definition Details
Parameters ( ) : did not accept any parameters
Return type double
Method Name Max
Parameters (double x, double y)
METHOD CALLS
A method must first be defined before it is called.

It must be called based exactly on how the method was defined:


1. Method name
2. Method type
3. Number of Parameters
4. Sequence of data type for each parameter
PUBLIC NON-STATIC METHOD CALLS
• To call public non-static method, we must :
1) declare object method for the class name
• General format : <CLASSNAME> <OBJECTNAME> = new CLASSNAME( );
Example : Cubicvolume task = new Cubicvolume( );

2) Use object to call method


• General format : OBJECTNAME.METHODNAME(ARGUMENTS)
Example : double maxs = task.Max (num1, num2); System.out.print(task.Multiply (12,13);

result1 = task.Multiply (12,13);

Definition Details
CLASSNAME the program class name
OBJECTNAME the instance of the class (object for the class name),
METHODNAME the method defined in the same class,
(ARGUMENTS) the value(s) we pass to a method
EXAMPLE NON-STATIC METHOD : RETURN VALUE METHOD
public class Callingthemethod {
public static void main (String [ ] args)
{
int x = 10, area;
Callingthemethod m = new Callingthemethod( ); //declare object m
area = m.Square (x); Content Description
System.out.println(area); Object m
area = m.Square (5);
Method Name square
System.out.println(area);
area = m.Square (x + 5 % 2); Argument (x) , (5) , (x + 5 % 2);
System.out.println(area); Return type int
} Method Name square
int Square (int num) Parameters (int num)
{
return num * num;
Method call
}
} Method definition

Note: The number and data type of the parameters passed in the method call should match with the number and data type of the
parameters in the method definition
EXAMPLE NON-STATIC METHOD CALLS

public class Cylinder { public class Circle {


public static void main(String [ ] args)
public static void main(String [ ] args)
{
{
Circle mtd = new Circle( );
Cylinder call = new Cylinder( );
System.out.println(“The area is “ + mtd.Area(50));
System.out.println(“The volume is “ + call.Volume(5.0, 4.25)); }
} double Area (double radius)
double Volume (double r, double h) {
double value;
{
value = 3.14 * radius * radius;
double vol; return value;
vol = 3.14 * r * r * h; }
return vol; }
}
}
ARGUMENT vs PARAMETER

• An argument is a value we pass to a method.

• Parameters are the variables in the method definition.

You might also like