0% found this document useful (0 votes)
8 views23 pages

Lec 6 - Methods

jok

Uploaded by

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

Lec 6 - Methods

jok

Uploaded by

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

Object Oriented Programming (Java)

JAVA PROGRAMMING

(BICT 102)

Lecture 6
Methods

Ho Technical University 1
Methods

Suppose that you need to find the sum of integers


from 1 to 10,
JAVA PROGRAMMING

from 20 to 37, and


from 35 to 49, respectively.

You may write the code as follows:

Sep 27, 202 Ho Technical University 2


public class MethodsDemoOne {

public static void main(String[] args) {


int sum = 0;
for (int i = 1; i <= 10; i++)
JAVA PROGRAMMING

sum += i;
System.out.println("Sum from 1 to 10 is " +
sum);
sum = 0;
for (int i = 20; i <= 37; i++)
sum += i;
System.out.println("Sum from 20 to 37 is " +
sum);
sum = 0;
for (int i = 35; i <= 49; i++)
sum += i;
System.out.println("Sum from 35 to 49 is " +
Ghana Technology University College
sum);
Sep 27, 202 3
Methods

This could however be achieved by writing some common


code once and reusing it.
JAVA PROGRAMMING

This is called defining a method and calling or invoking it.

Sep 27, 202 Ho Technical University 4


Methods
public class MyMethodsDemo2 {

public static int sum(int firstInt, int


secondInt) {
JAVA PROGRAMMING

int result = 0;
for (int i = firstInt; i <= secondInt; i++)
result += i;
return result;
}

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 37 is " + sum(20,
37));
System.out.println("Sum from 35 to 49 is " + sum(35,
49)); Ho Technical University
Sep 27, 202 5
Methods

• A method is a series of statements that carry out a task


JAVA PROGRAMMING

• Any class can contain an unlimited number of methods


• Methods simplify code, making the code easier to follow
• Methods can be reused any number of times

Sep 27, 202 Ho Technical University 6


Methods
• A method must include the following:
• A declaration (or header or definition)
JAVA PROGRAMMING

• An opening curly brace


• A body
• A closing curly brace
i.e. declaration
{
body
}

Sep 27, 202 Ho Technical University 7


Methods
• The method declaration contains the following:
• Optional access modifiers (i.e. private, public,
static, protected)
JAVA PROGRAMMING

• The return type for the method


• The method name
• An open parentheses
• An optional list of method arguments (you
separate the arguments with commas if there is
more than one
• A closing parentheses

Sep 27, 202 Ho Technical University 8


Methods
e.g. A method that void means the method
can be used returns nothing. int, double
anywhere etc. means it returns an
within the class integer, double etc.
JAVA PROGRAMMING

public means
any class can requires the
Name of the
use this method word static
method

public static void getMyAddress()


{
body The method
receives nothing
} so parentheses
access modifiers are empty

Sep 27, 202 Ho Technical University 9


Methods
E.g.
public class MyMethods
{
public static void main (String [ ] args)
JAVA PROGRAMMING

{
System.out.println("My address is ");

getAddress(); // calling a method within the


same //class
}
public static void getAddress()
{
System.out.println("P. O. Box 100");
System.out.println("Accra, Ghana");
System.out.println("West Africa");
}
}
Sep 27, 202 Ho Technical University 10
Methods

A method in one class can be called into another.

public class MyOtherMethods


JAVA PROGRAMMING

{
public static void main (String [ ] args) {

System.out.println("My address is ");

MyMethods.getAddress(); // calling a method from


another //class
}
}

Sep 27, 202 Ho Technical University 11


Methods

•Some methods require additional information

•Information passed to methods are called arguments


JAVA PROGRAMMING

• When you write a method declaration for a method that


can receive an argument, you must include the following
within the declaration parentheses:

• The type of the argument

• The local name for the argument

Sep 27, 202 Ho Technical University 12


Methods
e.g. Method name Type of argument

public void squareAge (int myAge)


JAVA PROGRAMMING

{
Name of argument
int result = myAge*myAge
System.out.println("When you double my age,
you get " + result);
}

Sep 27, 202 Ho Technical University 13


Methods
public class MyCallingMethods
{
public static void main (String [ ] args) {
JAVA PROGRAMMING

squareAge(30);
}

public static void squareAge(int myAge) //method


{
int result = myAge*myAge;
System.out.println("When you square my age, you get
" + result);
}
}

Sep 27, 202 Ho Technical University 14


Methods
•A method can require more than one argument

• You pass multiple arguments to a method by listing the


JAVA PROGRAMMING

arguments within the call to the method and separating


them with commas
e.g.
public void sum(int myAge, int yourAge)
{
int result = myAge + yourAge;
System.out.println("When you sum our ages, you
get "
+ result);
}
• You call this method using e.g. sum(22,32) and it
prints:
Sep 27, 202 When you sumHo
our ages,
Technical you get 54
University 15
Methods
•Some methods can be made to return values

•Such values can be of types int, char, double etc.


JAVA PROGRAMMING

•Methods can also return Boolean (true, false) values

•If a method returns nothing, it is void

•For a method to return a value, the keyword return is


used

•If a method that returns a value is called, it simply


returns the return value

Sep 27, 202 Ho Technical University 16


Methods
e.g. Return value type

public static int sum(int i, int j)


JAVA PROGRAMMING

{
int result = 0;
Parameters
result = i + j;

return result; return value


}
You call this method using e.g. sum(10, 15); and it returns
25
Returned values can be stored in variables

Sep 27, 202 Ho Technical University 17


Methods
public class MyMethods2
{
public static void main (String [ ] args) {
squareAge(40);
JAVA PROGRAMMING

int bothAges = addAges(10, 15);


System.out.println("When you sum our ages, you get "
+ bothAges);
}
public static void squareAge(int myAge) //method 1
{
int result = myAge*myAge;
System.out.println("When you square my age, you get "
+ result);
}
public static int addAges(int myAge, int yourAge) //method 2
{
int ourAges = myAge + yourAge;
return ourAges;
} }
Sep 27, 202 Ho Technical University 18
Methods

Overloading Methods

Two methods can be defined having the same name but


JAVA PROGRAMMING

different parameter lists within one class. This is called


method overloading.

Sep 27, 202 Ho Technical University 19


class Dog {
Overloaded
public void bark(String s)
{
System.out.println(s);
}
public void bark(String s, int num)
JAVA PROGRAMMING

{
System.out.println(s + " " + num + "
times");
}
}
public class DisplayOverloading
{
public static void main(String args[])
{
Dog fido = new Dog();
fido.bark("woah!");
fido.bark("woah!",10);
}}
Sep 27, 202 Ho Technical University 20
Recap

• A method is a series of statements that carry out a task,


and can be used repeatedly
JAVA PROGRAMMING

• We define methods and then call or invoke them to carry


out tasks

• In defining a method we must include:


 A declaration (or header or definition)
 An opening curly brace
 A body
 A closing curly brace

Sep 27, 202 Ho Technical University 21


Recap

• The method declaration contains:


• Optional access modifiers (i.e. private, public, static,
JAVA PROGRAMMING

protected)
• The return type for the method
• The method name
• An open parentheses
• An optional list of method arguments (you separate
the arguments with commas if there is more than one
• A closing parentheses

Sep 27, 202 Ho Technical University 22


Recap
•Some methods can be made to return values

•If a method returns nothing, it is void


JAVA PROGRAMMING

•For a method to return a value, the keyword return is


used

•Defining two or more methods with the same name but


different parameter lists within one class is known as
method overloading

Sep 27, 202 Ho Technical University 23

You might also like