You are on page 1of 5

Various ways to declare java methods

=====================================
There are four ways to declare methods in java.

1) No returntype with No argument method

2) No returtype with Argument method

3) With returntype with No argument method

4) With returntype with Argument method

1) No returntype with No argument method


-----------------------------------------
If there is no arguments then we need to ask inputs inside callie method.

Q)Write a java program to perfrom sum of two numbers using no returntype with no
argument method?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
//caller method
sum();
}
//callie method
public static void sum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number :");
int a=sc.nextInt();
System.out.println("Enter the second number :");
int b=sc.nextInt();

//logic
int c=a+b;
System.out.println("sum of two numbers is ="+c);
}
}

Q)Write a java program to perform swapping of two numbers using no returntype with
no argument method?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
//caller
swap();
}
//callie method
public static void swap()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number :");
int a=sc.nextInt();
System.out.println("Enter the second number :");
int b=sc.nextInt();

System.out.println("Before swapping a="+a+" and b="+b);

//logic
a=a+b;
b=a-b;
a=a-b;

System.out.println("After swapping a="+a+" and b="+b);


}
}

2) No returtype with Argument method


-----------------------------------
If we have arguments then we need to ask inputs inside main method.

Number of arguments depends upon number of inputs.

Q)Write a java program to perform sum of two numbers using no returntype with
argument method?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter the first number :");


int a=sc.nextInt();
System.out.println("Enter the second number :");
int b=sc.nextInt();

//caller method
sum(a,b);
}
//callie method
public static void sum(int a,int b)
{
int c=a+b;
System.out.println("sum of two numbers is ="+c);
}
}

Q)Write a java program to find out given number is even or odd by using no
returntype with argument method?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt();

//caller method
find(n);

}
//callie method
public static void find(int n)
{
if(n%2==0)
System.out.println("It is even number");
else
System.out.println("It is odd number");
}
}
3)With returntype with No argument method
==========================================
A returntype is completely depends upon output datatype.

Q)Write a java program to perform sum of two numbers using with returntype with no
argument method?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
//caller method
int k=sum();
System.out.println("sum of two numbers is ="+k);
}
//callie method
public static int sum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number :");
int a=sc.nextInt();
System.out.println("Enter the second number :");
int b=sc.nextInt();

//logic
int c=a+b;
return c;
}
}

Q)Write a java program to find out area of a circle using with returntype with no
argument method?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
float k=circle();
System.out.println("Area of a circle is ="+k);
}
//callie method
public static float circle()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the radius :");
int r=sc.nextInt();

//logic
float area=3.14f*r*r;

return area;
}
}

4)With returntype with argument method


=======================================

Q)Write a java program to perform sum of two numbers by using with returntype with
argument method?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number :");
int a=sc.nextInt();
System.out.println("Enter the second number :");
int b=sc.nextInt();

//caller method
System.out.println("sum of two numbers is ="+sum(a,b));

}
//callie method
public static int sum(int a,int b)
{
int c=a+b;
return c;
}
}

Q)Write a java program to check given number is even or odd using with returntype
with argument method?

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number :");
int n=sc.nextInt();

//caller method
System.out.println(find(n));

}
//callie method
public static String find(int n)
{
if(n%2==0)
return "It is even number";
else
return "It is odd number";
}
}

You might also like