0% found this document useful (0 votes)
12 views12 pages

Method Yvbi

The document discusses methods in Java. Some key points: 1. A method is a block of code that adds functionality to a class. It has a return type, name, parameters and body. 2. Methods help reduce code redundancy and improve reusability. 3. Parameters passed to methods are called formal parameters, while values passed during method calls are called actual parameters. 4. Methods can return a value to the caller using the return statement. Only one return statement should be used.

Uploaded by

rajvirkumar5710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views12 pages

Method Yvbi

The document discusses methods in Java. Some key points: 1. A method is a block of code that adds functionality to a class. It has a return type, name, parameters and body. 2. Methods help reduce code redundancy and improve reusability. 3. Parameters passed to methods are called formal parameters, while values passed during method calls are called actual parameters. 4. Methods can return a value to the caller using the return statement. Only one return statement should be used.

Uploaded by

rajvirkumar5710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Method:

A Method is a block of Code, which is used to add some functionality in the class.

Advantages:
> Helps to reduce code redundancy.
> Helps in code reusability.

Structure of the method.:


[Access Modifiers] return_type method_Name ([args_list])
{
---body---
-----
[return some_value];
}

> Where return_Type is to specify which type of data the present method is returning. In java
application, all primitive types, all user-defined datatypes and 'void' are allowed for methods as
return type.

Note: 'void' return type is representing "Nothing is returned" from the Methods.

> where Access Modifiers are of two types:

1. to provide visibility: public , private, protected and default.

2. to provide extra nature to the method: static , volatile, final etc.

> where method_Name must follow identifier rules.


> where args_list is the value provided to the method definition. In java, we can pass all primitive
types and all user defined datatypes as parameter.

Method Signature and Method Prototype:


Method Signature is the description of the method , it includes method name and parameter list.
E.g reverse(int n)
Method prototype is the description of the method, it will include access modifiers list, return type ,
method name, parameter list etc.
e.g.. public static int reverse(int n)
return keyword:
===============
> return is a transfer statement which is used to transfer the flow of execution from the calling to
the caller.
> return statement must be used at the end of the method.
> return statement must return only one value to the caller.
> only one return statement should be executed.
> No statement should be written after return statement.

e.g.

public class Demo


{
int reverse(int n)
{
int r=0;
while(n!=0)
{
int d=n%10;
r=r*10+d;
n/=10;
}
return(r);

}
public static void main(String[] args)
{
Demo obj = new Demo();
int n=197;
int v=obj.reverse(n);
System.out.println(v);
n=789;
v=obj.reverse(n);
System.out.println(v);
}
}

Formal parameter or Arguments:


The parameters which is used in the method header is called Formal Parameters
Acutal parameters :
The parameters which is used in the Method Calling is called Actual Parameters.

public class Demo


{

int reverse(int n)// Formal parameter


{
int r=0;
while(n!=0)
{
int d=n%10;
r=r*10+d;
n/=10;
}
return(r);

}
public static void main(String[] args)
{
Demo obj = new Demo();
int n=197;
int v=obj.reverse(n);
System.out.println(v);
n=789;
v=obj.reverse(n);
System.out.println(v);
}
}

pg. 1.
Write a program to input a Number and check whether the Number is prime or Not. Use the
method name as check(int n). The method returns "true", if the Number is prime otherwise , it
returns false.

import java.util.Scanner;
public class Demo
{
static boolean check(int n)
{
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
return(c==2);
}
public static void main(String args[])
{
Scanner in =new Scanner(System.in);
System.out.println("Enter a Number");
int n=in.nextInt();
boolean ch=check(n);
if(ch)
System.out.println("Prime");
else
System.out.println("Not Prime");
}
}

pg.2
Write a program to input a Number. Use a Method Armstrong(int n). The method returns 1, if the
Number is Armstrong, Otherwise zero(0)

import java.util.Scanner;
public class Demo
{
int armstrong(int n)
{
int t=n;
int s=0;
while(n>0)
{
int d=n%10;
s=s+d*d*d;
n/=10;
}
if(t==s)
return(1);
else
return(0);
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a Number");
int n=in.nextInt();
Demo obj=new Demo();
int ch=obj.armstrong(n);
if(ch==1)
System.out.println("Armstrong Number");
else
System.out.println("Not Armstrong Number");
}
}
pg no 317
Q3.Write a program to accept a number and check whether the number is palindrome or not by
using the method name reverse(int n). The method returns the reversed number to the main
program that checks the palindrome number.

import java.util.Scanner;
class Demo
{
int reverse(int n)
{
int r=0;
while(n!=0)
{
int d= n%10;
r=r*10+d;
n=n/10;
}
return(r);
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a Number");
int n= in.nextInt();
Demo obj= new Demo();
int r= obj.reverse(n);
if(r==n)
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
}

pg No. 318
q4.
==========

import java.util.Scanner;
class Demo
{
static int automorphic(int n)
{
int sq=n*n;
int nd=0;
while(n!=0)
{
nd++;
n/=10;
}
int p=(int)Math.pow(10, nd);
return(sq%p);
}

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
System.out.println("Enter a Number");
int n=in.nextInt();
int d=automorphic(n);
if(d==n)
System.out.println("Automorphic");
else
System.out.println("Not Automorphic");

}
}

pg no. 320
Q.No.5
==========
import java.util.Scanner;
class Demo
{
boolean isPrime(int n)
{
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
return(c==2);
}
int twin(int m,int n)
{
if(isPrime(m) && isPrime(n) && (m-n==-2 || m-n==2))
return(1);
else
return(0);
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter two Number");
int a = in.nextInt();
int b=in.nextInt();
Demo ob = new Demo();
int k = ob.twin(a, b);
if(k==1)
System.out.println("Twin Prime");
else
System.out.println("Not Twin Prime");
}
}

pass by value or call by value.


a value passing mechanism in which any changes in formal parameter will not reflect onto actual
parameter
e.g.
class Demo
{
static int m1(int n)
{
n=n*100;
return(n);
}
public static void main(String[] args)
{
int n=9;
int k=m1(n);
System.out.println(k);
System.out.println(n);
}
}

Output: 9
9

call by reference:
a value passing mechanism in which any changes in formal parameter will not reflect
onto actual parameter
e.g.
class Demo
{
int m1(int n[])
{
for(int i=0;i<n.length;i++)
n[i]=n[i]+5;
}
public static void main(String[] args)
{
int n={10,20,30,40,50};
Demo ob = new Demo();
int k=ob.m1(n);
for(int i=0;i<n.length;i++)
{
System.out.println(n[i]);
}
}
}
output: 15 25 35 45 55

>Write a program to use a method fibonacci , which takes an integer argument(n) and generates
the fibonacci series for 'n' number of times.
call the above method in main method .
// 0,1,1,2,3,5,8,13,21,34..........................
import java.util.Scanner;
class Demo
{
void fibonacci(int n)
{
int a=-1;
int b=1;
for(int i=1;i<=n;i++)
{
int c=a+b;
System.out.print(c+",");
a=b;
b=c;
}
}
public static void main(String[] args)
{
Demo ob = new Demo();
Scanner in = new Scanner(System.in);
System.out.println("Enter Number of Terms:");
int n=in.nextInt();
ob.fibonacci(n);
}
}

pg no 338
Q NO. 5
==========
Simport java.util.Scanner;
class Demo
{
static int fact(int n)
{
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
if(n==0)
return (1);
else
return(f);
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of m");
int m=in.nextInt();
System.out.println("Enter the value of n");
int n=in.nextInt();
double s=(double)fact(n)/ (fact(m)*fact(n-m));
System.out.println(s);
}
}

You might also like