You are on page 1of 22

3.

5 JAVA Method
3.5 Method
3.5.1 Introduction to Java Method

At the end of the lesson, students should be able to :

a) Introduction to JAVA Method (L)


i. Explain the meaning and the use of functions
ii. Explain types of method: standard library and user-defined.
iii. Explain the general structure of a user-defined method (method name, type, parameters).
iv. Explain method return types based on return values.
3.5 Method

Definition
A Java method is a collection of statements that are
grouped together to perform an operation.
3.5 Method
Why write and use functions?

Divide-and-conquer
•Can breaking up programs and algorithms into smaller, more manageable pieces
•This makes easier for writing, testing, and debugging
•Easier to break up the work for team development

Reusability
•Functions can be called to do their tasks anywhere in a program, as many times as
needed
•Avoids repetition of code in a program
•Functions can be placed into libraries to be used by more than one "program"
3.5 Method

Types of Java
Method

a) Standard b) User-
Library defined
Methods Methods
3.5 Method

a) Standard Library Methods


•The standard library methods are built-in methods in Java that are readily available
for use.

For example:
•print() is a method defined in java.io.PrintStream. The print("...") prints the string
inside quotation marks.

•sqrt() is a method defined in Math class. It returns square root of a number.


3.5 Method

a) Standard Library Methods - Example


public class Numbers
{
public static void main(String[] args)
{
System.out.print("Square root of 4 is: " + Math.sqrt(4));
}
}

Output:
Square root of 4 is: 2.0
3.5 Method
b) User-define Methods

➢ User-defined method is a method that is defined by the programmer.

➢ Before you can call a method, you need to define it based on the general structure of methods
containing:
• return type – the data type of the value returned by the method.
❖ a method that returns no value should have return type void.
❖ return type is based on the type of value returned by the method.
Eg: primitive data types (int, double, boolean, and char)

• method name - It is an identifier that is used to refer to the particular method in a program.

• parameters – a placeholder in the called method to hold value of the passed arguments.
3.5 Method
b) User-define Methods – example 1
public class primary
{
public static void main (String [] args)
{
primary sc = new primary();
System.out.println("About to encounter a method.");
sc.myMethod(); // method call
System.out.println("Method was executed successfully!");
} Method Name
Return type
void myMethod()
{
System.out.println("Printing from inside myMethod()!"); Method definition
}
}
3.5 Method
b) User-define Methods – example 2

public class ArithematicMain


{
public static void main(String[] args)
{ // Method call
ArithematicMain sc = new ArithematicMain();
System.out.println("10 + 20 = " + sc.getIntegerSum(10, 20));
}
Method Name Parameters
Return type
int getIntegerSum (int i, int j)
{ Method definition
return i + j;
}
}
3.5 Method
Check point 1

1. Define Method - _____________________________________________________________

2. State 2 types of method - _______________________________________________

- _______________________________________________

3. Define standard library method - __________________________________________________

4. Define User define method - _______________________________________________________________


3.5 Method
3.5.2 Methods Definition and Call

•Learning Outcomes

•At the end of this topic, you should be able to:


•Explain types and use of statements:( L )
i. Method definition
ii. Method calls
•Explain the general format of method definition and method call. :( L )
•Construct non-static user-defined methods. ( T )
•Construct programs that use standard and/or user-defined methods based on given problem. ( P )
3.5 Method
Method Definition

General Format of Method Definitions


Method Definition
•A user-defined method must first be
defined before it is called.
•Method definition consists of:
▪ Return type
▪ Method name
▪ Parameters
▪ Method body
3.5 Method
Method Definition
Method Definition
Return Types Method Name Parameters

•return type is based on the type •The name of the method is an •A parameter is a placeholder in
of value returned by the method identifier. the called method to hold value
•You can give any name to a of the passed arguments.
•Method can return value method. However, it is more
of primitive data types (int, conventional to name it after the •If there are values passed, then
double, boolean, and char). tasks it performs. the values will be assigned to the
parameters accordingly. (You can
•If the method does not return a For example: pass any number of arguments to
value, its return type is void. •CalculatePrice, ComputeMarks, a method.)
CalculateArea, and
FindMaximum. •However, methods may or may
not have parameters.
•usually represents the task
performed by the method
3.5 Method
Method Definition

This method:​
•named DisplayMessage
Examples of method definition •did not return any values, void and​
•did not accept any parameters, ().
void DisplayMessage()
{
System.out.print(“Hello, World!”);
}
3.5 Method
Method Call

General Format of Method Calls


A method must be called based on exactly how the
method was defined.
•Method name
•Method type objectName.methodName(arguments);
•Number of parameters
•Sequence of data type for each parameter.
•objectName is the instance of the class (object for the class name)
•methodName is the method defined in the same class, and
•arguments is the value(s) we pass to a method.
3.5 Method
Method Call

•There are two ways to call a method, which depends on whether:


•it returns a value, or
•it returns nothing (no return value).

•If a method returns a value, then the method should either:


➢ Be assigned to (=) a variable of the same data type,

variableName = obj.methodName;

➢ Printed using
System.out.print(obj.methodName)
3.5 Method
Methods Call with return value and without passing argument
class SquareMain
{
public static void main(String[] args)
{
SquareMain sc = new SquareMain(); //create objectName
int result;
result = sc.square(); // method call
System.out.println("Squared value of 10 is: " + result);
}

int square()
{
return 10 * 10; // return statement
}
}
3.5 Method
Methods Call with return value and with passing argument
class AddMETHOD2
{
public static void main (String[] args)
{ int a, b, result;
AddMETHOD2 sc = new AddMETHOD2(); //create objectName
Scanner bc = new Scanner(System.in);
System.out.println("Enter number 1");
a = bc.nextInt();
System.out.println("Enter number 2");
b = bc.nextInt();
result = sc.ADD (a , b); // method call
System.out.print("Sum = "+ result);
}
int ADD(int h, int j)
{
return h + j;
}

}
3.5 Method
Methods Call with no return value and no passing argument
Class AddMethod1
{
public static void main (String[] args)
{
AddMethod1 sc = new AddMethod1();
sc.Addition(); // method call
}
void Addition()
{
int a, b, add;
Scanner bc = new Scanner(System.in);
System.out.println("Enter number 1");
a = bc.nextInt();
System.out.println("Enter number 2");
b = bc.nextInt();
add = a + b;
System.out.println("Addition: "+ add);
}

}
3.5 Method
Methods Call with no return value and with passing argument
Class AddMethod1
{
public static void main (String[] args)
{
AddMethod1 sc = new AddMethod1();
sc.Addition(10 , 20); // method call
}
void Addition(int i, int j)
{
int add;
add = a + b;
System.out.println("Addition = "+ add);
}
}
3.5 Method
Check point 2

1. What is return type? _____________________________________________________________

2. What is method name ? __________________________________________________

3. What is parameter? _______________________________________________________________

You might also like