SlideShare a Scribd company logo
2
Most read
3
Most read
9
Most read
User defined Subroutines / Methods
/ Functions
in Java
Dr. Kuppusamy .P
Associate Professor / SCOPE
User defined methods in Java
• Method is a collection of statements grouped together to perform an
operation.
• User can also define methods inside a class called user-defined
methods or subroutines or functions.
• An user defined method will run only when it is called.
• User can pass data, known as parameters / arguments, into a
method.
• Why are methods needed?
• Code Reusability: Define the code once, use it many times.
Dr. Kuppusamy P
Creating Method
• Method definition consists of a method header and a method body.
Syntax
modifier returnType nameOfMethod (Parameter List)
{
// body with Set of Statements
}
• modifier − defines the access type of the method and it is optional.
• returnType − Method may return a value or void and it is mandatory.
• nameOfMethod − method name is any user defined name.
• Parameter List − The list of parameters, data type, order, and number of
parameters. These are optional, method may contain zero parameters.
• method body − Set of statements perform a specific task.
Dr. Kuppusamy P
Method - Example
public static void findFact () // Method Definition
{
Scanner sin=new Scanner(System.in);
int n = sin.nextInt();
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
System.out.println(f);
}
// Method calling
public static void main(String args[])
{
findFact();
} Dr. Kuppusamy P
Creating Method
Method can be created in the following four ways
1. Method without return-type without parameters
2. Method without return-type with parameters
3. Method with return-type without parameters
4. Method with return-type with parameters
5. Static methods
6. Non-static methods
Dr. Kuppusamy P
Creating Method - Example
1. Method without return-type without parameters
public class FactEx
{
public void Fact () // Method Definition
{
Scanner sin=new Scanner(System.in);
int n = sin.nextInt();
int f =1;
for(int i=1;i<=n;i++)
f=f * i;
System.out.println(f);
}
// Method calling
public static void main(String args[])
{
Fact();
}
} Dr. Kuppusamy P
Creating Method - Example
2. Method without return-type with parameters
public class FactEx1
{
public void Fact (int n) // Method Definition
{
int f =1;
for(int i=1;i<=n;i++)
f=f * i;
System.out.println(f);
}
// Method calling
public static void main(String args[])
{
Scanner sin=new Scanner(System.in);
int n = sin.nextInt();
Fact(n);
}
} Dr. Kuppusamy P
Creating Method - Example
3. Method with return-type without parameters
public class FactEx2
{
public int Fact() // Method Definition
{
Scanner sin=new Scanner(System.in);
int n = sin.nextInt();
int f =1;
for(int i=1;i<=n;i++)
f=f * i;
return f;
}
// Method calling
public static void main(String args[])
{
int factorial = Fact();
System.out.println(factorial);
}
}
Dr. Kuppusamy P
Creating Method - Example
4. Method with return-type with parameters
public class FactEx3
{
public int Fact (int n) // Method Definition
{
int f =1;
for(int i=1;i<=n;i++)
f=f * i;
return f;
}
// Method calling
public static void main(String args[])
{
Scanner sin=new Scanner(System.in);
int inp = sin.nextInt();
int factorial = Fact(inp);
System.out.println(factorial);
}
}
Dr. Kuppusamy P
Creating Method - Example
5. Static methods
public class StaticEx {
public static int Fact (int n) //method definition
{
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
public static void main(String[] args)
{
Scanner sin=new Scanner(System.in);
int num = sin.nextInt();
int factorial = Fact(num); //method calling
System.out.println(factorial);
}
}
Dr. Kuppusamy P
Creating Method - Example
6. Non-static methods
public class NonStaticEx {
public int Fact (int n) //method definition
{
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
public static void main(String[] args)
{
Scanner sin=new Scanner(System.in);
int num = sin.nextInt();
NonStaticEx m = new NonStaticEx(); //object or instance creation for the class NonStaticEx
int factorial = m.Fact(num); //method calling
System.out.println(factorial);
}
}
Dr. Kuppusamy P
Passing arrays as Parameters
Dr. Kuppusamy P
Passing arrays as Parameters
• Pass arrays to a method like normal variables.
• When pass an array as an argument, actually the address of the
array in the memory is passed (reference).
Creating Method with Arrays
i. Without return an array
modifier returnType Methodname (data-type array-name[])
{
// method body
}
ii. With return an array
modifier data-type[] Methodname (data-type array-name[])
{
// method body
}
Dr. Kuppusamy P
Example
import java.util.*;
public class ArrayPass
{
public int min(int [] array)
{
int min = array[0];
for(int i=0; i<array.length; i++)
{
if(array[i] < min)
{
min = array[i];
}
}
return min ;
}
Dr. Kuppusamy P
Example
public static int[] descending(int [] array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = i + 1; j < array.length; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return array ;
}
Dr. Kuppusamy P
Example
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int[] myArray = new int[size];
for(int i=0; i<size;i++)
{
myArray [i] = sc.nextInt();
}
ArrayPass m=new ArrayPass ();
int max= m.min(myArray);
System.out.println(max);
int dsc[] = m.descending(myArray);
for(int i:dsc)
System.out.println(i);
}
} Dr. Kuppusamy P
Method Overloading in java
Dr. Kuppusamy P
Method Overloading in java
• Method Overloading is a class contains more than one method with the same method
name. But varying the argument lists like count, data type and arguments sequence.
• Example:
• Addition of two number for integer as well as float.
• Advantages
• Method overloading increases the readability of the program.
Different approaches of method overloading
1. Number of parameters.
• add(int a, int b)
• add(int a, int b, int c)
2. Data type of parameters.
• add(int a, int b)
• add(int a, float b)
3. Sequence of Data type of parameters.
• add(int, float)
• add(float, int)
Dr. Kuppusamy P
Method Overloading in java
import java.util.Scanner;
public class Overload
{
public static int add(int a, int b)
{
return a+b;
}
public static float add(float a, float b)
{
return a+b;
}
public static void add(float a, int b)
{
System.out.println(a+b);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int isum=add(num1,num2);
float fsum=add(34.34f,34.34f);
add(10.0f, 20);
System.out.println(fsum);
}
}
Dr. Kuppusamy P
Recursion in java
Dr. Kuppusamy P
Recursion in java
• Recursion is a process in which a method calls itself continuously.
• A method in java that calls itself is called recursive method.
• It makes the reusability of code. So, reduces the program size.
• Syntax
returntype methodname()
{
statements;
methodname();
}
Dr. Kuppusamy P
Factorial using recursion in java
• Example
import java.util.*;
public class RecFact
{
public static int fact(int n)
{
if (n <= 1)
{
System.out.println(n);
return 1;
}
else
{
System.out.print( n+ " * ");
return n * fact(n-1);
}
}
public static void main(String[] args)
{
int n;
System.out.println("Enter the no to find
factorial " );
Scanner sin = new Scanner(System.in);
n = sin.nextInt();
int res = fact(n);
System.out.println("Factorial " + res);
}
}
Dr. Kuppusamy P
Fibonacci using Recursion in java
• Example
import java.util.*;
public class FibRec{
public static int fib( int n)
{
if(n == 0)
{
return 0;
}
if(n==1 || n==2)
{
return 1;
}
else
return fib(n-2) + fib(n-1);
}
public static void main(String [] arg)
{
int n;
Scanner sin = new Scanner(System.in);
System.out.println("Enter the fib
series");
n = sin.nextInt();
for(int i=0; i<=n; i++)
{
System.out.print(fib(i)+ " ");
}
}
}
Dr. Kuppusamy P
References
Dr. Kuppusamy P
Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition,
2017.

More Related Content

What's hot (20)

PDF
Python-02| Input, Output & Import
Mohd Sajjad
 
PDF
9 python data structure-2
Prof. Dr. K. Adisesha
 
PDF
Pandas
maikroeder
 
PPTX
Python Programming Essentials - M9 - String Formatting
P3 InfoTech Solutions Pvt. Ltd.
 
PDF
Python, the Language of Science and Engineering for Engineers
Boey Pak Cheong
 
PDF
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Edureka!
 
PPTX
2D Array
Ehatsham Riaz
 
PPTX
Two dimensional arrays
Neeru Mittal
 
PDF
Python Programming
Sreedhar Chowdam
 
PPSX
Data Structure (Stack)
Adam Mukharil Bachtiar
 
PPTX
Python Tutorial Part 1
Haitham El-Ghareeb
 
PPT
Stacks
sweta dargad
 
PDF
Railway Oriented Programming
Scott Wlaschin
 
PPTX
List and Dictionary in python
Sangita Panchal
 
PPTX
Queue Implementation Using Array & Linked List
PTCL
 
PDF
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Edureka!
 
PPTX
Data structure , stack , queue
Rajkiran Nadar
 
PDF
Python - gui programming (tkinter)
Learnbay Datascience
 
PPTX
Dictionary in python
vikram mahendra
 
PDF
Revised Data Structure- STACK in Python XII CS.pdf
MohammadImran709594
 
Python-02| Input, Output & Import
Mohd Sajjad
 
9 python data structure-2
Prof. Dr. K. Adisesha
 
Pandas
maikroeder
 
Python Programming Essentials - M9 - String Formatting
P3 InfoTech Solutions Pvt. Ltd.
 
Python, the Language of Science and Engineering for Engineers
Boey Pak Cheong
 
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Edureka!
 
2D Array
Ehatsham Riaz
 
Two dimensional arrays
Neeru Mittal
 
Python Programming
Sreedhar Chowdam
 
Data Structure (Stack)
Adam Mukharil Bachtiar
 
Python Tutorial Part 1
Haitham El-Ghareeb
 
Stacks
sweta dargad
 
Railway Oriented Programming
Scott Wlaschin
 
List and Dictionary in python
Sangita Panchal
 
Queue Implementation Using Array & Linked List
PTCL
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Edureka!
 
Data structure , stack , queue
Rajkiran Nadar
 
Python - gui programming (tkinter)
Learnbay Datascience
 
Dictionary in python
vikram mahendra
 
Revised Data Structure- STACK in Python XII CS.pdf
MohammadImran709594
 

Similar to Java methods or Subroutines or Functions (20)

PDF
Week 7 Java Programming Methods For I.T students.pdf
JaypeeGPolancos
 
PPTX
Java Foundations: Methods
Svetlin Nakov
 
DOCX
Methods in Java
Kavitha713564
 
PPTX
Computer programming 2 Lesson 15
MLG College of Learning, Inc
 
PPTX
IntroductionJava Programming - Math Class
sandhyakiran10
 
PPTX
40+ examples of user defined methods in java with explanation
Harish Gyanani
 
PPTX
Chap2 class,objects contd
raksharao
 
PPTX
slide6.pptx
ssusere3b1a2
 
PPTX
1. Methods presentation which can easily help you understand java methods.pptx
jeyoco4655
 
PPTX
Java method
sunilchute1
 
PDF
Java -lec-6
Zubair Khalid
 
PPTX
Chapter 6 Methods.pptx
ssusere3b1a2
 
PPT
05slide
Dorothea Chaffin
 
PPTX
11. java methods
M H Buddhika Ariyaratne
 
PPT
Comp102 lec 7
Fraz Bakhsh
 
PDF
CIS 1403 lab 3 functions and methods in Java
Hamad Odhabi
 
PPT
JavaYDL5
Terry Yoast
 
PPT
Cso gaddis java_chapter5
mlrbrown
 
DOCX
The Java Learning Kit Chapter 5 – Methods and Modular.docx
arnoldmeredith47041
 
PPTX
Java Methods
OXUS 20
 
Week 7 Java Programming Methods For I.T students.pdf
JaypeeGPolancos
 
Java Foundations: Methods
Svetlin Nakov
 
Methods in Java
Kavitha713564
 
Computer programming 2 Lesson 15
MLG College of Learning, Inc
 
IntroductionJava Programming - Math Class
sandhyakiran10
 
40+ examples of user defined methods in java with explanation
Harish Gyanani
 
Chap2 class,objects contd
raksharao
 
slide6.pptx
ssusere3b1a2
 
1. Methods presentation which can easily help you understand java methods.pptx
jeyoco4655
 
Java method
sunilchute1
 
Java -lec-6
Zubair Khalid
 
Chapter 6 Methods.pptx
ssusere3b1a2
 
11. java methods
M H Buddhika Ariyaratne
 
Comp102 lec 7
Fraz Bakhsh
 
CIS 1403 lab 3 functions and methods in Java
Hamad Odhabi
 
JavaYDL5
Terry Yoast
 
Cso gaddis java_chapter5
mlrbrown
 
The Java Learning Kit Chapter 5 – Methods and Modular.docx
arnoldmeredith47041
 
Java Methods
OXUS 20
 
Ad

More from Kuppusamy P (20)

PDF
Recurrent neural networks rnn
Kuppusamy P
 
PDF
Deep learning
Kuppusamy P
 
PDF
Image segmentation
Kuppusamy P
 
PDF
Image enhancement
Kuppusamy P
 
PDF
Feature detection and matching
Kuppusamy P
 
PDF
Image processing, Noise, Noise Removal filters
Kuppusamy P
 
PDF
Flowchart design for algorithms
Kuppusamy P
 
PDF
Algorithm basics
Kuppusamy P
 
PDF
Problem solving using Programming
Kuppusamy P
 
PDF
Parts of Computer, Hardware and Software
Kuppusamy P
 
PDF
Strings in java
Kuppusamy P
 
PDF
Java arrays
Kuppusamy P
 
PDF
Java iterative statements
Kuppusamy P
 
PDF
Java conditional statements
Kuppusamy P
 
PDF
Java data types
Kuppusamy P
 
PDF
Java introduction
Kuppusamy P
 
PDF
Logistic regression in Machine Learning
Kuppusamy P
 
PDF
Anomaly detection (Unsupervised Learning) in Machine Learning
Kuppusamy P
 
PDF
Machine Learning Performance metrics for classification
Kuppusamy P
 
PDF
Machine learning Introduction
Kuppusamy P
 
Recurrent neural networks rnn
Kuppusamy P
 
Deep learning
Kuppusamy P
 
Image segmentation
Kuppusamy P
 
Image enhancement
Kuppusamy P
 
Feature detection and matching
Kuppusamy P
 
Image processing, Noise, Noise Removal filters
Kuppusamy P
 
Flowchart design for algorithms
Kuppusamy P
 
Algorithm basics
Kuppusamy P
 
Problem solving using Programming
Kuppusamy P
 
Parts of Computer, Hardware and Software
Kuppusamy P
 
Strings in java
Kuppusamy P
 
Java arrays
Kuppusamy P
 
Java iterative statements
Kuppusamy P
 
Java conditional statements
Kuppusamy P
 
Java data types
Kuppusamy P
 
Java introduction
Kuppusamy P
 
Logistic regression in Machine Learning
Kuppusamy P
 
Anomaly detection (Unsupervised Learning) in Machine Learning
Kuppusamy P
 
Machine Learning Performance metrics for classification
Kuppusamy P
 
Machine learning Introduction
Kuppusamy P
 
Ad

Recently uploaded (20)

PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Controller Request and Response in Odoo18
Celine George
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 

Java methods or Subroutines or Functions

  • 1. User defined Subroutines / Methods / Functions in Java Dr. Kuppusamy .P Associate Professor / SCOPE
  • 2. User defined methods in Java • Method is a collection of statements grouped together to perform an operation. • User can also define methods inside a class called user-defined methods or subroutines or functions. • An user defined method will run only when it is called. • User can pass data, known as parameters / arguments, into a method. • Why are methods needed? • Code Reusability: Define the code once, use it many times. Dr. Kuppusamy P
  • 3. Creating Method • Method definition consists of a method header and a method body. Syntax modifier returnType nameOfMethod (Parameter List) { // body with Set of Statements } • modifier − defines the access type of the method and it is optional. • returnType − Method may return a value or void and it is mandatory. • nameOfMethod − method name is any user defined name. • Parameter List − The list of parameters, data type, order, and number of parameters. These are optional, method may contain zero parameters. • method body − Set of statements perform a specific task. Dr. Kuppusamy P
  • 4. Method - Example public static void findFact () // Method Definition { Scanner sin=new Scanner(System.in); int n = sin.nextInt(); int f=1; for(int i=1;i<=n;i++) f=f*i; System.out.println(f); } // Method calling public static void main(String args[]) { findFact(); } Dr. Kuppusamy P
  • 5. Creating Method Method can be created in the following four ways 1. Method without return-type without parameters 2. Method without return-type with parameters 3. Method with return-type without parameters 4. Method with return-type with parameters 5. Static methods 6. Non-static methods Dr. Kuppusamy P
  • 6. Creating Method - Example 1. Method without return-type without parameters public class FactEx { public void Fact () // Method Definition { Scanner sin=new Scanner(System.in); int n = sin.nextInt(); int f =1; for(int i=1;i<=n;i++) f=f * i; System.out.println(f); } // Method calling public static void main(String args[]) { Fact(); } } Dr. Kuppusamy P
  • 7. Creating Method - Example 2. Method without return-type with parameters public class FactEx1 { public void Fact (int n) // Method Definition { int f =1; for(int i=1;i<=n;i++) f=f * i; System.out.println(f); } // Method calling public static void main(String args[]) { Scanner sin=new Scanner(System.in); int n = sin.nextInt(); Fact(n); } } Dr. Kuppusamy P
  • 8. Creating Method - Example 3. Method with return-type without parameters public class FactEx2 { public int Fact() // Method Definition { Scanner sin=new Scanner(System.in); int n = sin.nextInt(); int f =1; for(int i=1;i<=n;i++) f=f * i; return f; } // Method calling public static void main(String args[]) { int factorial = Fact(); System.out.println(factorial); } } Dr. Kuppusamy P
  • 9. Creating Method - Example 4. Method with return-type with parameters public class FactEx3 { public int Fact (int n) // Method Definition { int f =1; for(int i=1;i<=n;i++) f=f * i; return f; } // Method calling public static void main(String args[]) { Scanner sin=new Scanner(System.in); int inp = sin.nextInt(); int factorial = Fact(inp); System.out.println(factorial); } } Dr. Kuppusamy P
  • 10. Creating Method - Example 5. Static methods public class StaticEx { public static int Fact (int n) //method definition { int f=1; for(int i=1;i<=n;i++) f=f*i; return f; } public static void main(String[] args) { Scanner sin=new Scanner(System.in); int num = sin.nextInt(); int factorial = Fact(num); //method calling System.out.println(factorial); } } Dr. Kuppusamy P
  • 11. Creating Method - Example 6. Non-static methods public class NonStaticEx { public int Fact (int n) //method definition { int f=1; for(int i=1;i<=n;i++) f=f*i; return f; } public static void main(String[] args) { Scanner sin=new Scanner(System.in); int num = sin.nextInt(); NonStaticEx m = new NonStaticEx(); //object or instance creation for the class NonStaticEx int factorial = m.Fact(num); //method calling System.out.println(factorial); } } Dr. Kuppusamy P
  • 12. Passing arrays as Parameters Dr. Kuppusamy P
  • 13. Passing arrays as Parameters • Pass arrays to a method like normal variables. • When pass an array as an argument, actually the address of the array in the memory is passed (reference). Creating Method with Arrays i. Without return an array modifier returnType Methodname (data-type array-name[]) { // method body } ii. With return an array modifier data-type[] Methodname (data-type array-name[]) { // method body } Dr. Kuppusamy P
  • 14. Example import java.util.*; public class ArrayPass { public int min(int [] array) { int min = array[0]; for(int i=0; i<array.length; i++) { if(array[i] < min) { min = array[i]; } } return min ; } Dr. Kuppusamy P
  • 15. Example public static int[] descending(int [] array) { for (int i = 0; i < array.length; i++) { for (int j = i + 1; j < array.length; j++) { if (array[i] > array[j]) { temp = array[i]; array[i] = array[j]; array[j] = temp; } } } return array ; } Dr. Kuppusamy P
  • 16. Example public static void main(String args[]) { Scanner sc = new Scanner(System.in); int size = sc.nextInt(); int[] myArray = new int[size]; for(int i=0; i<size;i++) { myArray [i] = sc.nextInt(); } ArrayPass m=new ArrayPass (); int max= m.min(myArray); System.out.println(max); int dsc[] = m.descending(myArray); for(int i:dsc) System.out.println(i); } } Dr. Kuppusamy P
  • 17. Method Overloading in java Dr. Kuppusamy P
  • 18. Method Overloading in java • Method Overloading is a class contains more than one method with the same method name. But varying the argument lists like count, data type and arguments sequence. • Example: • Addition of two number for integer as well as float. • Advantages • Method overloading increases the readability of the program. Different approaches of method overloading 1. Number of parameters. • add(int a, int b) • add(int a, int b, int c) 2. Data type of parameters. • add(int a, int b) • add(int a, float b) 3. Sequence of Data type of parameters. • add(int, float) • add(float, int) Dr. Kuppusamy P
  • 19. Method Overloading in java import java.util.Scanner; public class Overload { public static int add(int a, int b) { return a+b; } public static float add(float a, float b) { return a+b; } public static void add(float a, int b) { System.out.println(a+b); } public static void main(String args[]) { Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int num2 = sc.nextInt(); int isum=add(num1,num2); float fsum=add(34.34f,34.34f); add(10.0f, 20); System.out.println(fsum); } } Dr. Kuppusamy P
  • 20. Recursion in java Dr. Kuppusamy P
  • 21. Recursion in java • Recursion is a process in which a method calls itself continuously. • A method in java that calls itself is called recursive method. • It makes the reusability of code. So, reduces the program size. • Syntax returntype methodname() { statements; methodname(); } Dr. Kuppusamy P
  • 22. Factorial using recursion in java • Example import java.util.*; public class RecFact { public static int fact(int n) { if (n <= 1) { System.out.println(n); return 1; } else { System.out.print( n+ " * "); return n * fact(n-1); } } public static void main(String[] args) { int n; System.out.println("Enter the no to find factorial " ); Scanner sin = new Scanner(System.in); n = sin.nextInt(); int res = fact(n); System.out.println("Factorial " + res); } } Dr. Kuppusamy P
  • 23. Fibonacci using Recursion in java • Example import java.util.*; public class FibRec{ public static int fib( int n) { if(n == 0) { return 0; } if(n==1 || n==2) { return 1; } else return fib(n-2) + fib(n-1); } public static void main(String [] arg) { int n; Scanner sin = new Scanner(System.in); System.out.println("Enter the fib series"); n = sin.nextInt(); for(int i=0; i<=n; i++) { System.out.print(fib(i)+ " "); } } } Dr. Kuppusamy P
  • 24. References Dr. Kuppusamy P Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition, 2017.