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

Chapter5 Methods

Uploaded by

voila.alberto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Chapter5 Methods

Uploaded by

voila.alberto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Methods in Java

A method is a set of Java statements, which is referred to by a name and can be called
at any point in an application simply by its name. When method name is encountered in
an application, execution of the program branches to the body of the method. After the
method is executed, the control returns to the area of the main program code from
which it was called, and the program continues at the next statement. Java uses stack
to remember the address of Java statement from where the control is transferred to the
method, so that it can return to the calling place.

DEFINING A METHOD
access-type return-type method-name(parameters)
{
// body;
}
public static int max(int a, int b)
{
int big = a > b ? a : b;
return big;
}
Here, the access-type is public static, return type is int, name of the method is max,
there are two arguments a & b and the method returns an int value

CALLING A METHOD
variable = method-name(arguments);
// Big2.java
import java.util.*;
public class Big2
{
// define method max2(). It receives two integers and returns the biggest as an
//integer. Here variables a, b and result are visible only inside max2() and not
//outside
public static int max2(int a, int b)
{
int result = a > b ? a : b;
return result;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
// read two integers
System.out.println(“Enter two integers one by one”);
int a = sc.nextInt();
int b = sc.nextInt();
// now call method with a and b and collect result
int result = max2(a, b);
// show result
System.out.println(“Biggest: ” + result);
}
}
Output
Enter two integers one by one
23
31
Biggest: 31
METHOD OVERLOADING

In Java, it is possible to define two or more methods with the same name, within the
same class. Hence, these methods are called overloaded methods and this process is
known as method overloading. Method overloading is one of Java’s most exciting and
useful features that avoid redundant names for those methods whose behaviour is the
same.

Java distinguishes overloaded methods through their arguments. Two overloaded


methods can differ by any of the following ways:

 Type of arguments
 Number of arguments
 Order of appearance of the arguments

Case 1. Type of arguments


public static int add(int a, int b)
public static double add(double a, double b)

Case 2. Number of arguments


public static int add(int a, int b)
public static int add(int a)

Case 3. Order of appearance of arguments


public static double add(int a, double b)
public static double add(double a, int b)
// AdditionCalculator.java
public class AdditionCalculator
{
public static int add(int i, int j)
{
return i + j;
}
public static double add(double i, double j)
{
return i + j;
}
public static float add(float i, float j)
{
return i + j;
}
public static double add(int i, double j)
{
return i + j;
}
public static double add(double i, int j)
{
return i + j;
}
public static void main(String[] args)
{
System.out.println(add(10,20));
System.out.println(add(10.0,20.0));
System.out.println(add(100.0f,200.0f));
System.out.println(add(30,20.0));
System.out.println(add(50.0,20));
}
}

Output
30
30.0
300.0
50.0
70.0

VARIABLE LENGTH ARGUMENT LISTS

JDK 1.5 introduced variable arguments (or varargs) and a new syntax "Type..."

data-type… argument

Varargs can be used only for the last argument and only one variable length argument
can be specified in a method. Any regular arguments must precede it. The three dots
(...) indicate that the last argument may be passed as an array or as a sequence of
comma-separated arguments. The compiler automatically packs the varargs into an
array. You could then retrieve and process each of these arguments inside the method's
body as an array. It is possible to pass varargs as an array, because Java maintains the
length of the array in an associated variable length.

// ShowNames.java
public class ShowNames
{
public static void show(int id, String… names)
{
System.out.println(“id: ” + id);
for(int i = 0; i < names.length; i++)
System.out.println(“Name ” + i + “ : ” + names[i]);
}
public static void main(String[] args)
{
// call show() with varargs - just some value 100
show(100, “C”, “C++”, “Java”, “.NET”, “PhP”, “Python”);

// call show() with an array too


String[] names = {”C”, “C++”, “Java”, “.NET”, “PhP”,“Python”};
show(100, names); // assume 100 for id
}
}

Output
id: 100
Name 0 : C
Name 1 : C++
Name 2 : Java
Name 3 : .NET
Name 4 : PhP
Name 5 : Python
id: 100
Name 0 : C
Name 1 : C++
Name 2 : Java
Name 3 : .NET
Name 4 : PhP
Name 5 : Python
RECURSION

Recursion allows a function to call itself several times. Recursion is a powerful


programming technique where iterative computation becomes tedious. Recursion is
widely applied in combinatorial search and sorting methods. You will be able to
appreciate the power of recursion with the calculation of factorial of a number. The
factorial function for a positive number N is defined by the equation
N! = N × (N − 1) × (N − 2) × … × 2 × 1

// Factorial.java using loops


import java.util.*;
public class Factorial
{
public static int fact(int n)
{
int f = 1;
if( n == 1)
return f;
for(int i = 2; i <= n; i++)
f = f * i;
return f;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter a number: ”);
int n = sc.nextInt();
if (n >= 0)
System.out.println(“Factorial of ”+ n + “: ” + fact(n));
}
}
Output
Enter a number: 5
Factorial of 5: 120
Enter number: 2
Factorial of 2: 2
Enter number: 0
Factorial of 0: 1

//Factorial method using recursion

int fact(int n)
{
if (n == 0)
return 1;
return n * fact(n-1);
}

You might also like