Professional Documents
Culture Documents
5 JAVA Method
3.5 Method
3.5.1 Introduction to Java 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
For example:
•print() is a method defined in java.io.PrintStream. The print("...") prints the string
inside quotation marks.
Output:
Square root of 4 is: 2.0
3.5 Method
b) User-define Methods
➢ 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
- _______________________________________________
•Learning Outcomes
•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
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