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

10-Methods in Java

The document provides an introduction to methods in Java, explaining their definition, usage, parameters, and arguments. It includes examples of how to implement methods for summing integers and discusses concepts like method overloading and scope. Additionally, it presents an assignment to create methods for multiplication and division while applying method overloading.
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

10-Methods in Java

The document provides an introduction to methods in Java, explaining their definition, usage, parameters, and arguments. It includes examples of how to implement methods for summing integers and discusses concepts like method overloading and scope. Additionally, it presents an assignment to create methods for multiplication and division while applying method overloading.
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/ 32

METHODS IN JAVA

An Introduction
OBJECTIVES

• Define method, parameters and arguments.


• Discuss how methods are used in writing programs in Java.
• Write a Java program implementing methods.
• Compare programs written in Java using methods and without methods.
OPENING PROBLEM
Find the sum of integers from 1 to 10, from 20 to 30, and
from 35 to 45, respectively.

3
PROBLEM
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
System.out.println("Sum from 1 to 10 is " + sum);

sum = 0;
for (int i = 20; i <= 30; i++)
sum += i;
System.out.println("Sum from 20 to 30 is " + sum);

sum = 0;
for (int i = 35; i <= 45; i++)
sum += i;
System.out.println("Sum from 35 to 45 is " + sum);

4
PROBLEM
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
System.out.println("Sum from 1 to 10 is " + sum);

sum = 0;
for (int i = 20; i <= 30; i++)
sum += i;
System.out.println("Sum from 20 to 30 is " + sum);

sum = 0;
for (int i = 35; i <= 45; i++)
sum += i;
System.out.println("Sum from 35 to 45 is " + sum);

5
SOLUTION
public static int sum(int i1, int i2) {
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}

public static void main(String[] args) {


System.out.println("Sum from 1 to 10 is " + sum(1, 10));
System.out.println("Sum from 20 to 30 is " + sum(20, 30));
System.out.println("Sum from 35 to 45 is " + sum(35, 45));
}
6
WHAT IS A METHOD?

• A method is a block of code which only runs when it is called.


• A method is a collection of statements that are grouped together to perform an operation.
• You can pass data, known as parameters, into a method.
• Methods are used to perform certain actions, and they are also known as functions.
• Why use methods? To reuse code: define the code once and use it many times.
CREATE A METHOD Example: Create a method inside Main:

• A method must be declared within a class.


It is defined with the name of the method,
followed by parentheses ().
• Java provides some pre-defined methods,
such as System.out.println(), but you can
also create your own methods to perform
certain actions:
EXAMPLE EXPLAINED

• myMethod() is the name of the method


• static means that the method belongs to the Main
class and not an object of the Main class.
• void means that this method does not have a
return value.
CALL A METHOD Inside main, call the myMethod() method:

• To call a method in Java, write the


method's name followed by two
parentheses () and a semicolon;
• In the following
example, myMethod() is used to
print a text (the action), when it is
called:
A METHOD
CAN ALSO BE
CALLED
MULTIPLE
TIMES:
PARAMETERS AND ARGUMENTS

• Information can be passed to methods as parameter.


• Parameters act as variables inside the method.
• Parameters are specified after the method name, inside the parentheses. You
can add as many parameters as you want, just separate them with a comma.
PARAMETERS
AND ARGUMENTS

• The following example has a


method that takes
a String called firstname as
parameter.
• When the method is called,
we pass along a first name,
which is used inside the
method to print the full
name:
PARAMETERS
AND ARGUMENTS

• When a parameter is passed


to the method, it is called
an argument. So, from the
example: firstname is
a parameter, while Angela,
Mark and Cristelle
are arguments.
MULTIPLE
PARAMETERS

• You can have as many


parameters as you like:
RETURN VALUES

• The void keyword, used in the examples


above, indicates that the method should
not return a value.
• If you want the method to return a
value, you can use a primitive data type
(such as int, char, etc.) instead of void,
and use the return keyword inside the
method:
THIS EXAMPLE
RETURNS THE
SUM OF A
METHOD'S TWO
PARAMETERS:
• You can also store
the result in a
variable
(recommended, as
it is easier to read
and maintain):
A METHOD WITH
IF...ELSE

• It is common to
use if...else statements
inside methods:
JAVA METHOD OVERLOADING

• With method overloading, multiple methods can have the same name with different
parameters:
Example:
int myMethod(int x)
float myMethod(float x)
double myMethod(double x, double y)
JAVA METHOD
OVERLOADING
• Consider the following
example, which has
two methods that add
numbers of different
type:
JAVA METHOD
OVERLOADING
• Instead of defining two
methods that should do the
same thing, it is better to
overload one.
• In the example below, we
overload
the AdditionMethod method
to work for
both int and double:
JAVA SCOPE

• In Java, variables are only accessible inside the region they are created.
• This is called scope.
METHOD SCOPE
• Variables declared directly inside
a method are available anywhere
in the method following the line
of code in which they were
declared:
BLOCK SCOPE
• A block of code refers to all of the
code between curly braces {}.
• Variables declared inside blocks of
code are only accessible by the
code between the curly braces,
which follows the line in which the
variable was declared:
BLOCK SCOPE

• A block of code may exist on its own or it can belong to an if, while or for statement.
• In the case of for statements, variables declared in the statement itself are also available inside
the block's scope.
SAMPLE
PROGRAM
USING
METHODS
YOUR ASSIGNMENT:

1. Based on the given sample program, add a


method to multiply two integers and a method to
divide two integers.
2. Apply method overloading to minimize the given
sample code.
ACCEPTING
INPUT FROM
THE USER
REFERENCES:

• https://www.w3schools.com/java/java_methods.asp

You might also like