SlideShare una empresa de Scribd logo
1 de 22
Class 10
Computer Application
Chapter 3
User Defined Methods
in java
Presented by,
Siva Shankari Rajan,
CPSV
What is method?
A method or a
function is a sequence
of statements
grouped together and
given a name
This group of statements
can be called at any point in
the program using its name
to perform a specific task.
Why to use
methods?
Methods are used
in the programs for
various reason
To code complexity
To hide the details
For reusable code
To simplify program
maintenance
Method declaration
Method Declaration
It declare either
specifies the type of
the value which a
method will return
or
use the keyword void
to indicate that it will
not return any value
Method Declaration
A method consists of
two parts:
• Header part
• Body part.
The method header is the
first line of the function
declaration or definition.
The method body contains a
set of statements for related
operations written in curly
brackets.
Method Declaration
Header part :
1. Modifier
2. Return type
3. Method name
4. Parameter list
 Modifier tells the compiler how to
call the method.
 Return type specifies the type of
value returned from a function.
 Function name is the name
assigned to the method.
 Parameter list is comma-
separated list of variables of a
function.
The methods are of
two types:
1. Built in method
2. User defined
method
The built-in methods are
pre-defined methods
stored in the Java library.
The user-defined methods
are defined by the
programmers as per their
need.
A Method exists in
three different forms
within a program:
Method prototype
Method definition
Method call
Method Prototype
Method Definition
Function prototype is the first line of the method
definition ended by semicolon that tells the
program about the signatures of the function.
A method is called (invokedorexecuted)by
providing the method name along with the
argument list enclosed in the parenthesis.
Method Call
A method is called (invokedorexecuted)by
providing the method name along with the
argument list enclosed in the parenthesis.
Method Signature
It is basically refers to
the number and types
of the arguments in the
method.
It itself is used to refer
to a method prototype.
Public class drawing
{
Public void draw(String s)
{….}
Public void draw(int i)
{….}
Public void draw(double d)
{….}
Public void draw(int I, double s)
{….}
}
Is also called method overloaded
Arguments
An argumentis a value itself
that is passed to a method when
it is executed
Parameters
the parameter list appears
between the parentheses
following the method name.
Public void main(){
A=obj.Sum(5,8);//5,8 arearguments being
passed toparameter x
andy
----------
----------
}
Float Sum(int x, int y){
Float add=(x+y)/2; //x andy
areparameter
Return add;
}
Actual parameter
Theparameter that appear in a
method call statement arecalled
actual parameter.
Formal Parameter
the parameter that appear in the
function definition are called
formal parameter.
Public int sum(int a, int b)
{
return a+b; //a andbareformal parameter
}
int length=10;
int breadth=5;
int add=sum(length,breadth);
//areactual parameter
How to access
Method?
A method can be
accessed or called by
providing the name of
the method followed
by parameter enclosed
in parentheses.
Class callmethod
{
Public double area(double a, double b)
{
double x;
x=a*b;
return x;
}
Public void accessingmethod()
{double x,y,z;
x=3.0;y=9.3;
z=area(x,y);//calling method area()
System.out.println(z);
Pure functions
Thefunction which returnvalues
and do not change state are
called purefunction.
impure function
Thefunction which changesthe
state of objects is called impure
function
class add
{
private double a,b,c;
public add()
{a=10.0;b=10.0;c=1.0;}
public double sum()
{
return(a+b);// purefunction, return exact value
}
public void sum1()
{c=a+b;//impurefunction, value ofcvaries
System.out.println(c);}
Passing values to
method
Values can be passed
in the following two
ways,
1. Call by value
2. Call by address
(reference)
Call by value
Themethod createsits newset of variables to copy the
value of actual parameters andworks with them
Call by address
Reference of the actual parameters is passed on to the
method. No newset ofvariables is created.
Call by value
class passbyvalue
{
public void changed()
{
int a=12;
System.out.println(“original value=“+a);
System.out.println(“changed
value=“+value(a));
System.out.println(“again value=“+a);
}
public static int value(int x)
{
x=10;
return x;
}
}
Call by Reference
class Test
{
int x;
Test(int i) { x = i; }
Test() { x = 0; }
}
class Main {
public static void main(String[] args) {
Test t = new Test(5); // t is a reference
change(t);
System.out.println(t.x);
}
public static void change(Test t)
{
t = new Test();
t.x = 10;
}
}
RecursiveMethod
Recursionin java is a
process in which a method
calls itself continuously.
It makes the code compact
but complex to understand.
public class RecursionExample1 {
static void p(){
System.out.println("hello");
p();
}
public static void main(String[] args) {
p();
}
}
Method overloading
Method overloading
means that there are
more than one
function doing
different kinds of jobs
but have same name.
this process is also
known as
polymorphism.
Public class calc
{
Public void sum(int s,int v)
{System.out.println(s+v);
}
Public void sum(int I, int j,int k)
{
System.out.println(i+j+k);
}
Public static void main(string args[])
{
calc ob=new calc();
Ob.sum(7,8);
Ob.sum(4,5,7);}
}
Thanks for watching

Más contenido relacionado

La actualidad más candente (20)

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Array in c++
Array in c++Array in c++
Array in c++
 
Methods in java
Methods in javaMethods in java
Methods in java
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
C functions
C functionsC functions
C functions
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in Java
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 

Similar a User Defined Methods in Java: Class 10 Computer Application Chapter 3

EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...vekariyakashyap
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parametersPrem Kumar Badri
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: MethodsSvetlin Nakov
 
C programming session 08
C programming session 08C programming session 08
C programming session 08Vivek Singh
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Abou Bakr Ashraf
 
Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.Questpond
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4dplunkett
 

Similar a User Defined Methods in Java: Class 10 Computer Application Chapter 3 (20)

EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
C# p8
C# p8C# p8
C# p8
 
Module 4 : methods & parameters
Module 4 : methods & parametersModule 4 : methods & parameters
Module 4 : methods & parameters
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
Functions
FunctionsFunctions
Functions
 
Java execise
Java execiseJava execise
Java execise
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Ch06
Ch06Ch06
Ch06
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
C language 3
C language 3C language 3
C language 3
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Function in c
Function in cFunction in c
Function in c
 
Explain Delegates step by step.
Explain Delegates step by step.Explain Delegates step by step.
Explain Delegates step by step.
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Method parameters in c#
Method parameters in c#Method parameters in c#
Method parameters in c#
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
 

Último

Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 

Último (20)

Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 

User Defined Methods in Java: Class 10 Computer Application Chapter 3

  • 1. Class 10 Computer Application Chapter 3 User Defined Methods in java Presented by, Siva Shankari Rajan, CPSV
  • 2. What is method? A method or a function is a sequence of statements grouped together and given a name This group of statements can be called at any point in the program using its name to perform a specific task.
  • 3. Why to use methods? Methods are used in the programs for various reason To code complexity To hide the details For reusable code To simplify program maintenance
  • 5. Method Declaration It declare either specifies the type of the value which a method will return or use the keyword void to indicate that it will not return any value
  • 6. Method Declaration A method consists of two parts: • Header part • Body part. The method header is the first line of the function declaration or definition. The method body contains a set of statements for related operations written in curly brackets.
  • 7. Method Declaration Header part : 1. Modifier 2. Return type 3. Method name 4. Parameter list  Modifier tells the compiler how to call the method.  Return type specifies the type of value returned from a function.  Function name is the name assigned to the method.  Parameter list is comma- separated list of variables of a function.
  • 8. The methods are of two types: 1. Built in method 2. User defined method The built-in methods are pre-defined methods stored in the Java library. The user-defined methods are defined by the programmers as per their need.
  • 9. A Method exists in three different forms within a program: Method prototype Method definition Method call
  • 10. Method Prototype Method Definition Function prototype is the first line of the method definition ended by semicolon that tells the program about the signatures of the function. A method is called (invokedorexecuted)by providing the method name along with the argument list enclosed in the parenthesis.
  • 11. Method Call A method is called (invokedorexecuted)by providing the method name along with the argument list enclosed in the parenthesis.
  • 12. Method Signature It is basically refers to the number and types of the arguments in the method. It itself is used to refer to a method prototype. Public class drawing { Public void draw(String s) {….} Public void draw(int i) {….} Public void draw(double d) {….} Public void draw(int I, double s) {….} } Is also called method overloaded
  • 13. Arguments An argumentis a value itself that is passed to a method when it is executed Parameters the parameter list appears between the parentheses following the method name. Public void main(){ A=obj.Sum(5,8);//5,8 arearguments being passed toparameter x andy ---------- ---------- } Float Sum(int x, int y){ Float add=(x+y)/2; //x andy areparameter Return add; }
  • 14. Actual parameter Theparameter that appear in a method call statement arecalled actual parameter. Formal Parameter the parameter that appear in the function definition are called formal parameter. Public int sum(int a, int b) { return a+b; //a andbareformal parameter } int length=10; int breadth=5; int add=sum(length,breadth); //areactual parameter
  • 15. How to access Method? A method can be accessed or called by providing the name of the method followed by parameter enclosed in parentheses. Class callmethod { Public double area(double a, double b) { double x; x=a*b; return x; } Public void accessingmethod() {double x,y,z; x=3.0;y=9.3; z=area(x,y);//calling method area() System.out.println(z);
  • 16. Pure functions Thefunction which returnvalues and do not change state are called purefunction. impure function Thefunction which changesthe state of objects is called impure function class add { private double a,b,c; public add() {a=10.0;b=10.0;c=1.0;} public double sum() { return(a+b);// purefunction, return exact value } public void sum1() {c=a+b;//impurefunction, value ofcvaries System.out.println(c);}
  • 17. Passing values to method Values can be passed in the following two ways, 1. Call by value 2. Call by address (reference) Call by value Themethod createsits newset of variables to copy the value of actual parameters andworks with them Call by address Reference of the actual parameters is passed on to the method. No newset ofvariables is created.
  • 18. Call by value class passbyvalue { public void changed() { int a=12; System.out.println(“original value=“+a); System.out.println(“changed value=“+value(a)); System.out.println(“again value=“+a); } public static int value(int x) { x=10; return x; } }
  • 19. Call by Reference class Test { int x; Test(int i) { x = i; } Test() { x = 0; } } class Main { public static void main(String[] args) { Test t = new Test(5); // t is a reference change(t); System.out.println(t.x); } public static void change(Test t) { t = new Test(); t.x = 10; } }
  • 20. RecursiveMethod Recursionin java is a process in which a method calls itself continuously. It makes the code compact but complex to understand. public class RecursionExample1 { static void p(){ System.out.println("hello"); p(); } public static void main(String[] args) { p(); } }
  • 21. Method overloading Method overloading means that there are more than one function doing different kinds of jobs but have same name. this process is also known as polymorphism. Public class calc { Public void sum(int s,int v) {System.out.println(s+v); } Public void sum(int I, int j,int k) { System.out.println(i+j+k); } Public static void main(string args[]) { calc ob=new calc(); Ob.sum(7,8); Ob.sum(4,5,7);} }