Java method or function

Java Method or Function

Method in java

When we define a set of code within a block having a name is called method. It can call or invoke at any time.

We can define the Java method as follows


A simple Java method requires a minimum of three items:
Access Level        : public, private, protected
Return Type         : void, int, double, (etc.)
Name                   : whatever you want to call the method

Simple example of a function

private double simpleInterest(double p, double t, double r)
{
            double si=(p*t*r)/100;
            return si;
}

Here access specifier is private, return type double and the function name is simpleInterst with three parameters. In Java convention function start with a small character.

Access Specifier

 The access modifier or Access specifier is the access type of the method/Function. It specifies the visibility or accessibility of the method. Java has four types of access specifier or access modifier:

  1. Default: Java uses default access specifier by default. It is visible only from the same package only.
  2. Public: The method is accessible (visible in) by all classes when we use public specifier in Java application.
  3. Private: When we use a private access specifier, the method is accessible only in the classes in which it is defined.
  4. Protected: Protected access specifier when use, the method is accessible within the same package or subclasses in a different package.
A full example of a function
public class Function1
{
   int a,b,c;
   int Add()  // return method
   {
       a=10;
       b=30;
       c=a+b;
       return c;
    }
    public static void main(String smart[])
    {
        int x;
        Function1 ob=new Function1();
        x=ob.Add();
        System.out.println("Sum="+x);
     }      
}

What is the Actual parameter?

Ans: The actual parameter is a parameter as appear in a method/function. Actual parameters (also known as arguments) are what is passed by the caller.

What is a Formal parameter?

 Ans: the Identifier used in a method to stand for the value that is passed into the method by a caller. Formal parameters are also known as parameters of the function.

actual and formal parameter


Simple Interest in Java

import java.util.*;
public class SimpleInterest
{
    double p, t, r, si, amount; // Instance Variable
    void inputValue()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Principal, Time and Rate :");
        p=sc.nextDouble();
        t=sc.nextDouble();
        r=sc.nextDouble();
    }
    void simpleInt()
    {
        si=(p*t*r)/100.0;
        amount=p+si;
    }
    void display()
    {
        System.out.println("Simple Interest="+si);
        System.out.println("Amount="+amount);
    }
    public static void main()
    {
        SimpleInterest ob=new SimpleInterest();
        ob.inputValue();
        ob.simpleInt();
        ob.display();
    }
}

Prime Number in Java with Method

import java.util.*;
public class PrimeNumber
{
    int n, i;
    boolean p;
    boolean prime(int x)
    {
        n=x;
        p=true;
        for(i=2;i<n;i++)  
        {
            if(n%i==0)
            {
                p=false;
                break;
            }
        }
        return p;
    }
    public static void main()
    {
        int n; // Local variable
        boolean pr;
        Scanner sc=new Scanner(System.in);
        PrimeNumber ob=new PrimeNumber();
        System.out.println("Enter number:");
        n=sc.nextInt();
        pr=ob.prime(n); 
        if(pr==true)
            System.out.println("Prime Number");
        else
            System.out.println("Not Prime Number");
    }
}


Power and Factorial Method with return and Decimal Format

import java.io.*;
import java.math.RoundingMode;
import java.text.DecimalFormat;

public class SeriesPowFact
{
    //private static DecimalFormat df = new DecimalFormat("#.##");
    int i, p, f, x;
    int pow(int n)
    {
        x=n;
        p=1;
        for(i=0; i<x ; i++)
        {
            p=p*x;
        }
        return p;
    }

    int fact(int n)
    {
        f=1;
        x=n;
        for(i=1; i<=x; i++)
        {
            f=f*i;
        }
        return f;
    }

    public static void main(String args[])throws IOException
    {
        int i, po, fa, no;
        double s=0.0;
        SeriesPowFact ob=new SeriesPowFact();
        InputStreamReader input=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(input);
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a number :");
        no=Integer.parseInt(br.readLine());
        for(i=1;i<=no;i++)
        {
            po=ob.pow(i);
            fa=ob.fact(i);
            s=s + (double)fa/po;
        }
        System.out.println("Series="+s);
        DecimalFormat df = new DecimalFormat("#.###"); 
        String formatted = df.format(s); 
        System.out.println(formatted); //prints 2.46
    }
}


More Articles



SHARE THIS
Previous Post
Next Post