0% found this document useful (0 votes)
6 views

Method 2223

Uploaded by

syahmimoktar2004
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)
6 views

Method 2223

Uploaded by

syahmimoktar2004
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/ 25

Topic 4: Java Language

4.5 JAVA Method


4.5 Method

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

4.5.1 Introduction to JAVA Method (L)


a. Explain the meaning and the advantage of functions
b. Explain types of methods: predefined and user-defined.

4.5.2 Predefined method (T)


a) Identify the use of a common predefined method: pow(x,y), sqrt(x).
4.5 Method

4.5.3 Static user-defined method


a) Explain two (2) types of static user-defined methods.
b) Explain the general structure of static user-defined method: method header,
method body
c) Write a method definition with a return value, with or without formal parameters
d) Write a method definition without a return value, with or without formal
parameters.
e) Write a method call with a return value, with or without actual parameters.
f) Write a method call with no return value, with or without actual parameters.
g) Write a static user-defined method to perform simple computations. (P)
4.5 Method
4.5.1 Introduction to Java Method

Definition
A Java method is a collection of statements that are
grouped together to perform an operation.
4.5 Method
Advantages of method

•Can breaking up programs and algorithms into smaller, more manageable pieces
•This makes it easier for writing, testing, and debugging.
•Easier to break up the work for team development
•It can be called to do their tasks anywhere in a program, as many times as needed
•Avoids repetition of code in a program .(can be placed into libraries to be used by
more than one "program“)
4.5 Method

Types of
Java
Method

a) b) User-
Predefined defined
Methods Methods
4.5 Method
4.5.2 Predefined method (T)

A.K.A 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.


4.5 Method

a) Standard Library Methods - Example 1


public class power
{
public static void main(String[] args)
{
double result;

result = Math.pow(2,3);
System.out.println("The answer is: " + result);
Output:
} The answer is: 8.0
}
4.5 Method

a) Standard Library Methods - Example 2


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
4.5 Method
4.5.3 User-defined Methods

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

⮚ Before you can call or invoke 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.
1. a method that returns no value should have a return type void.
2. the return type is based on the type of value returned by the method.
Eg: primitive data types (int, double, float, 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 the value of the passed arguments.
4.5 Method
b) User-define Methods
*** addition

Access Specifier: Access specifier or modifier is the access type of the method. It specifies the
visibility of the method.

Java provides four types of access specifiers:

Access Specifier Explanation

Public The method is accessible by all classes when we use a public specifier in our application.

When we use a private access specifier, the method is accessible only in the classes in which
Private
it is defined.
When we use protected access specifier, the method is accessible within the same package
Protected
or subclasses in a different package.
When we do not use any access specifier in the method declaration, Java uses default access
Default
specifier by default. It is visible only from the same package
4.5 Method
b) User-define Methods – example 1
public class primary
{
public static void main (String [] args)
{
System.out.println("About to encounter a method.");
myMethod(); // method call
System.out.println("Method was executed successfully!");
}

Access Specifier Return type Method Name

public static void myMethod() Method header

{ Method definition
Method body
System.out.println("Printing from inside myMethod()!");
}
}
4.5 Method
b) User-define Methods – example 2

public class ArithematicMain


{
public static void main(String[] args) // Method call
{
System.out.println("10 + 20 = " + getIntegerSum(10, 20));
}
Access Specifier Return type Method Name Parameters

public static int getIntegerSum (int i, int j) Method header


{
return i + j; Method body Method definition
}
}
4.5 Method
Check point 1

1. Define Method - _____________________________________________________________

2. State 2 types of method - _______________________________________________

- _______________________________________________

3. Define predefined method - __________________________________________________

4. Define User-define method -


_______________________________________________________________
4.5 Method

4.5.3 Static user-defined method


a) Explain two (2) types of static user-defined methods.
b) Explain the general structure of static user-defined method: method header,
method body
c) Write a method definition with a return value, with or without formal parameters
d) Write a method definition without a return value, with or without formal
parameters.
e) Write a method call with a return value, with or without actual parameters.
f) Write a method call with no return value, with or without actual parameters.
g) Write a static user-defined method to perform simple computations. (P)
4.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
4.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 methodName(arguments);
•Number of parameters
•Sequence of data type for each parameter.

•methodName is the method defined in the same class, and


•arguments is the value(s) we pass to a method.
4.5 Method
4.5.3 Static user-defined method

Method Definition
Method Definition General Format of Method Definitions
•A user-defined method must first be
defined before it is called. Method header
•Method definition consists of:
▪ Return type
▪ Method name Method body
▪ Parameters
▪ Method body
4.5 Method
Method Definition
This method:​
•named DisplayMessage
•did not return any values, void and​
Examples of method definition •did not accept any parameters, ().

void DisplayMessage() Method header


{
System.out.print(“Hello, World!”); Method body

}
4.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 = methodName;

⮚ Printed using
System.out.print(methodName)
4.5 Method
Methods Call with no return value and no passing argument
Class AddMethod
{
public static void main (String[] args)
{
Addition(); // method call
}

static 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);
}
}
4.5 Method
Methods Call with no return value and with passing argument
Class AddMethod
{
public static void main (String[] args)
{
Addition(10 , 20); // method call
}

static void Addition(int i, int j)


{
int add;
add = a + b;
System.out.println("Addition = "+ add);
}}
4.5 Method
Methods Call with return value and without passing argument
class SquareMain
{
public static void main(String[] args)
{
int result;
result = square(); // method call
System.out.println("Squared value of 10 is: " + result);
}

static int square()


{
return 10 * 10; // return statement
}
}
4.5 Method
Methods Call with return value and with passing
argument
class AddMETHOD2
{
public static void main (String[] args)
{ int a, b, result;

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 = ADD (a , b); // method call
System.out.print("Sum = "+ result);
}
static int ADD(int h, int j)
{
return h + j;
}

}
4.5 Method
Check point 2

1. What is return type? _____________________________________________________________

2. What is method name ? __________________________________________________

3. What is parameter? _______________________________________________________________

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy