Tuesday, August 1, 2023

Java Methods

 

                     Methods

Method definitions have four basic parts:

·      The name of the method

·      The type of object or primitive type the method return

·      A list of parameters

·      The body of the method

Method Syntax

type name(parameter-list){

//body of method

}

Method static syntax

access_specifier static return_type method_name(parameter)

{

return statement;

}

Example1

File name: BoxM.java

class BoxM

{

       double width;

       double height;

       double depth;

  void volume()

{

     double vol =width*height*depth;

      System.out.println("Volume is:"+vol);

 

         }

}

 

Calling Methods

class BoxDemoM

{

       public static void main(String args[])

        {

             BoxM b1,b2;

             b1=new BoxM();

             b1.width=10;b1.height=5;b1.depth=8;

           b1.volume();

 

            b2=new BoxM();

            b2.width=3;b2.height=15;b2.depth=4;

         b2.volume();

        }

}

Output



Returning a Value

 Example 2

File name: Boxv.java

class Boxv{

double width;

double height;

double depth;

double volume(){

       return width*height*depth;

    }

}

File name: BoxReturn.java

class BoxReturn

{

          public static void main(String args[]) {

            Boxv mybox1=new Boxv();

            Boxv mybox2=new Boxv();

            double vol;

            mybox1.width=10;

            mybox1.height=20;

            mybox1.depth=15;

            mybox2.width=3;

            mybox2.height=6;

            mybox2.depth=9;

            vol=mybox1.volume();

           System.out.println("Volume  is"+vol);

            vol=mybox2.volume();

            System.out.println("Volume  is"+vol);

           }

}


Output


Passing values(Parameters) to Methods

File Name: Pass.java

class Pass{

double width;

double height;

double depth;

void setDim(double w,double h,double d)

{

      width=w;

       height=h;

       depth=d;    

    }

double volume()

{

double vol=width*height*depth;

return vol;

}

}

Here is the modified version of box Demo class.

File name: Boxpass.java

class Boxpass{

public static void main(String args[]){

            Pass mybox1=new  Pass();

            Pass mybox2=new Pass();

            double vol;

            mybox1.setDim(10,20,30);

            mybox2.setDim(5,10,15);

            vol=mybox1.volume();

           System.out.println("volume is"+vol);

           vol=mybox2.volume();

            System.out.println("volume is"+vol);

           }

}

 

Output

 


Overloading Methods

Two or more methods within the same class method name same ,argument different is called Overloading.

File name: OverloadDemo.java

class OverloadDemo{

     void multi(){

       System.out.println("No values to multiply");

}

     void multi(int a) {

       System.out.println("a*a:"+a*a);

}

      void multi(int a,int b) {

        System.out.println(a+"and"+b+":"+a*b);

}

  double multi(double a){

          return a*a;

}

public static void main(String args[]){

     OverloadDemo ob =new OverloadDemo();

      double result;

     ob.multi();

     ob.multi(10);

     ob.multi(10,20);

     result=ob.multi(123.25);

     System.out.println("Result of ob.multi(123.25):"+result);

}

}

 

Output


 
 

Argument Passing

call-by-value

 

In java simple type of method ,it is passed by value is called call by value.

File name: CallTest.java

class CallTest{

      void change(int i)

    {

        i=i+1;

        System.out.println("Inside function i ="+i);

       }

}

File name: CallByValue.java

class CallByValue  {

   public static void main(String args[]){

     CallTest ob=new CallTest();

      int a=10;

      System.out.println("Before call a="+a);

      ob.change(a);

            System.out.println("Before call a="+a);

          }

}


Output



Call-by-reference

File name: RefTest.java

class RefTest{

      int a,b;

      void change(RefTest o){

            o.a+=2;

            o.b-=2;

         }

            void show(){                                        

            System.out.println("A="+a+",B="+b);

           }

}

 File name: CallByRef.java

class CallByRef{

             public static void main(String args[]){

             RefTest ob1=new RefTest();   

             RefTest ob2=new RefTest();

            ob2.a=10;

            ob2.b=20;

            System.out.println("Before calling change");

            ob2.show();

            ob1.change(ob2);

            System.out.println("After calling change");

            ob2.show();

            }

}

Output



0 comments:

Post a Comment