You are on page 1of 11

2 Encapsula,on, Classes,

Objects and Methods


2.1 Methods

1
Objectives
At the end of the lesson, the student should be able to:
•  Review the concept of methods
•  Apply methods in object-oriented design

2
Methods
-  A block of code that is invoked by name, accepts parameters
and may return or not return any values to the method that
called it
-  A method may be predefined or user-defined
-  Method signature:
-  Access modifier
-  Return type
-  Method name
-  Parameters

3
Predefined Methods:
Math Class
public static double sqrt(double value)
public static double sin(double value)
public static double cos(double value)
public static double min(double value)
public static double max(double value)
public static double floor(double value)
public static double ceil(double value)
public static double round(double value)
public static double random(double value)
public static double pow(double base, double exponent)
4
User-defined methods
public static void main(String args){
Scanner s = new Scanner(System.in);
int a = s.nextInt();
}

public static int add(int x, int y){


return x+y;
}

5
Question 1.

6
Question 2.

7
Question 3.

8
Question 4.

9
Question 5.

10
Summary
Methods are essential components of OO design.

11

You might also like