Thursday, June 22, 2017

Java-Methods

Methods
-------
A method is a subprogrm which performs a specific task.

we can divide a large program into small parts using methods,it is also called as modularization.

Methods provides code reusability.

Methods makes debugging easy.

Methods optimize code(reduces length of a code)

Methods provides readability.

Types
-----
There are 2 types of methods

i)default method
ii)Parameterized method

default method
--------------
A method without parameters is known as default  method

syntax
------
accessmodifier  returntype methodname()
{
  //statements
}

ex:
public void sum()
{
//
}

How to call a default method
----------------------------
syntax
------
referenceName.methodname();

ex:
s.sum();


class Triangle
{
 public void area()
 {
    double b=10.0,h=5.0,a;
    a=0.5*b*h;
    System.out.println(a);
  }
 
}
class TriTest
{
    public static void main(String args[])
    {
       Triangle t=new Triangle();
       t.area();
    }
}


Parameterized method
--------------------
A method with parameters is known as parameterized method.


syntax
------
accessmodifier returntype methodname(datatype var1,datatype var2,.....)
{
//
}

ex:
public void sum(int x,int y)
{
//statements
}
Parameterized methods are used to perform parameter passing.

Parameter passing
-----------------
Passing values from one method to another method is known as parameter passing.

How to call a parameterized method
----------------------------------
syntax
------
referencename.methodname(value1,value2,....);

or

referencename.methodname(var1,var2,....);

ex:
s.sum(10,20);

or

int a=10,b=20;
s.sum(a,b);



ex:
class Addition
{
   public void sum(int x,int y)//called method
   {                           //formal/dummy parameters
     int c=x+y;
     System.out.println(c);
   }
}
class AddTest
{
  public static void main(String args[])
  {
    Addition a=new Addition();

    int a=10,b=20;
    a.sum(a,b);//line1
               //method calling
               //actual parameters
   }
}

In the above program we are declaring a sum method with 2 parameters of int types.

The parameters declared with a method is called formal/dummy parameters.

The statement declared at line1 is called calling method/method calling.

The parameters passed to method calling is known as actual arguments.

whenever a method is called,the values of actual arguments are copied to formal arguments according to the order variables i.e first parameter from calling method is copied first argument of called method(the method itself).similarly second parameter to the second argument and so on.

The variables declared in a method are called local variables.

ex:
class Circle
{
   public void area(double r)
   {
     double pi=3.14,a;
     a=pi*r*r;
     System.out.println(a);
    }
}
class CirTest
{
   public static void main(String args[])
   {
      Circle c=new Circle();
      double x=10.0;
      c.area(x);
    }
}


Armstrong
---------
class ArmStrong
{
    public void logic(int n)
    {
       int temp=n,r,sum=0;
       while(n>0)
       {
           r=n%10;
           sum=sum+r*r*r;
           n=n/10;
        }
        if(sum==temp)
        {
          System.out.println("Arm");
        }
        else{
          System.out.println("Not An Arm");
        }
     }
}
class ArmTest
{
   public static void main(String args[])
   {
       ArmStrong s=new ArmStrong();
       s.logic(153);
   }
}



return type
-----------
It specifies the type of value a method is returning.

datatype before methodname is a returntype.

How to return value
-------------------
we can return a value from a method using the keyword

"return"

syntax
------
return value;

or

return variablename;


Note:
we can save a java program with any name and compile the program with that name we had saved, but while executing a program  we must run the program with the class that is having main method declared inside it.

ex:
class Book
{
   String name="twostates";
   String author="chetan";
   double price=150.00;
 
 
   //display  method
   public void display()
   {
       System.out.println(name);
       System.out.println(author);
       System.out.println(price);
     
   }

}
class BookTest
{
   public static void main(String args[])
   {
       Book b=new Book();
       b.display();
   }
}

       
save as

Book.java

comiple
javac Book.java

run
---
java BookTest

No comments: