Function of Java
Function
in Java: The A function is a body of code that
returns a value. The
value returned may depend on arguments provided to the function. Basically function includes function
declaration, function body definition and calling the function as per our
requirement. In Java programming generally
three components used like type of function it means value returnable function
or value non-returnable function , Name of function means name of function which is define by
programmer and last number of argument or type
of argument .The function is block of code which is define for execution of particular task as per our
requirement. We can also create various type of function and we can used as per
our requirement in programming.
Types of function calling method:
·
With
argument and return value.
·
With
argument but not return value.
·
No argument
but return value.
·
No
argument and no return value.
Function declaration few word and
its meaning.
public static int methodName(int a,
int b)
{
// body
}
Here,
public static − modifier
int − return type
methodName − name of the method
a, b − formal parameters
int
a, int b − list of parameters
1) Write program to create a function
for square a given number?
import
java.io.*;
Class
square1
{
Public
static void main(String args[])throw IOException
{
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
int
a,b;
System.out.println(“Enter
The Value of Square:”);
a=integer.parse.Int(br.readLine());
b=a*a;
System.out.println(b);
}
}
Input
|
Output
|
Enter The Value of
Square: 10
|
100
|
2) Write program to create a function
for Area of Rectangle?
import
java.io.*;
Class
Area1
{
Public
static void main(String args[])throw IOException
{
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
int
h,w,x;
System.out.println(“Enter
The Value Height:”);
h=integer.parse.Int(br.readLine());
System.out.println(“Enter
The Value Width:”);
w=integer.parse.Int(br.readLine());
x=h*w;
System.out.println(“Area
of Rectangular =”+x);
}
}
Input
|
Output
|
Enter The Value of
Height: 50
Enter The Value of
Height: 30
|
Area of Rectangular
= 1500
|
3) Write a function to create a
function a number to solve following equation S=a2+b2+2ab.
import
java.io.*;
Class
Equation
{
Public
static void main(String args[])throw IOException
{
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
int
a,b,c,d,e,f,g;
System.out.println(“Enter
The Value of A:”);
a=integer.parse.Int(br.readLine());
System.out.println(“Enter
The Value of B:”);
b=integer.parse.Int(br.readLine());
c=a*a;
d=b*b;
e=c+d
f=a+b*2;
g=f+e;
System.out.println(“Square
of A =”+c);
System.out.println(“Square
of B =”+d);
System.out.println(“Sum
of Square =”+e);
System.out.println(“A
Multiplying B of 2 is =”+f);
System.out.println(“Sub
Total =”+g);
}
}
Input
|
Output
|
Enter The Value of A: 5
Enter The Value of B: 7
|
Square of A= 25
Square of B=49
Sum of Square = 74
A Multiplying B of 2
is =94
Sub Total = 242
|
4) Write a function to create a function
a number to solve following equation S=a3+b3+2a2b+2ab2.
import
java.io.*;
Class
Equation1
{
Public
static void main(String args[])throw IOException
{
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
int
a,b,c,d;
System.out.println(“Enter
The Value of A:”);
a=integer.parse.Int(br.readLine());
System.out.println(“Enter
The Value of B:”);
b=integer.parse.Int(br.readLine());
d=Cube(a)+Cube(b)+2*(sqr(a)*b)+2*(a*sqr(b));
System.out.println(d);
}
static
int sqr(int x)
{
Int
z=x*x;
Return
z;
}
Static
int Cube(int x)
{
Int
z=x*x*x;
Return
z;
}
}
Function By Reference
5) Write program to create a function
for calculating the simple interest for given principle, rate and time?
import
java.io.*;
Class
principle1
{
Public
static void main(String args[])throw IOException
{
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
int
p,r,t,x;
System.out.println(“Enter
the principle amount:”);
p=integer.parse.Int(br.readLine());
System.out.println(“Enter
the rate of interest:”);
r=integer.parse.Int(br.readLine());
System.out.println(“Enter
the time:”);
t=integer.parse.Int(br.readLine());
x=SI(p*r*t)/100;
System.out.println(“Sum
Total =”+x);
}
Static
int SI(int p,int r,int t)
{
int
a=p*r*t;
return
a;
}
}
Input
|
Output
|
Enter the principle
amount: 2000
Enter the rate of
interest: 5
Enter the time: 2
|
Sum Total = 20000
|
6) Write program to create a function
for calculating the Compound interest?
import
java.io.*;
Class
copmound1
{
Public
static void main(String args[])throw IOException
{
BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
double
p,r,t,s;
System.out.println(“Enter
the principle amount:”);
p=Double.parse.Double(br.readLine());
System.out.println(“Enter
the rate of interest:”);
r=
Double.parse.Double (br.readLine());
System.out.println(“Enter
the time:”);
t=
Double.parse.Double (br.readLine());
s=SI(p,r,t);
System.out.println(s);
}
Static
double SI(int x,int y,int z)
{
double
a ;
a=x*Math.pow((1+y/100),z)-x;
// Math class used for calculation.
return
a;
}
}
7) Write a Simple programming by reference
in Java?
import
java.io.*;
public class apple
{
Public
static void main()
{
int
ball=5;
apple
temp=new apple();
temp.play(ball);
System.out.println(“Total
No of Ball”+ball);
}
void
play(int ball){
System.out.println(“Total No play Ball” +ball);
ball++;
System.out.println(“Total
No play after Increment Ball” +ball);
}
}
THE END
Comments
Post a Comment