Wednesday, November 5, 2014

Methods

There are two stages in methods
1.       Declaring the methods.
2.       Calling the methods.
Declaring the methods.
·         In this stage we give a name to the method and it’s return[D1]  type. We can give any name to a method. But in the run time pc only execute the codes in the main method.
Example:
class cal{
void add(){
int x=2;
int y=3;
System.out.println  (“sum = ” + (x+y ));

}
void sub(){
int x=2;
int y=3;
System.out.println  (“sub = ” + (x-y ));

}
void mul(){
int x=2;
int y=3;
System.out.println  (“mul = ” + (x*y ));
}
void div(){
int x=2;
int y=3;
System.out.println  (“div = ” + (x/y ));

}
}                                   



















                                                                                                                                                                               
In the above example there is no main method. Therefore the output should be empty.
output



so we should call them in to the main method.
Ex :-
Entity: person
Properties: name, age, date of birth, weight
Behaviours: run, jump, talk         
1.       Real world entity can be represented by a java class.
2.       The properties of the entity will be map to variable.
3.       The behaviours of the entity will be map to methods.


class person {
  int age= 23;
String name = “Kamal”;
Int height = 125;
int weight = 90;
 
  void run(){
                  //////////////
                                /////////////////
}
                void run(){
                                //////////////
                                /////////////////
}
}



Object orient programming concept.


Object/entity : -
·         Object In the real world can be represented from a java class.
Properties /atribures : -
·         Properties In the entity are map to global variable in the java class.
The behaviours : -
·         The behaviours in the entity are map to methods in the java class.

Formore about Object orient programming concept.
                           
1
 Public static void main (String args[]){
2
                               Calculator c1= new Calculator();
3
                                      c1.add();
4
                                      c1.subs();
5
                                      c1.mul();
6
                                      c1.div();
7
                                      c1.mod();
8
}
                           


To call a method / variable in a class an object/instant must be created of the given class.
        Calculator c1= new Calculator (); 

Calculator                            : reference data type ( Hold the reference memory address of the calculator object)
c1                                            : variable name
 new Calculator ();           : create the calculator object
·         multiple classes can be declared with different with different names.
·         An object/instant can be created out of each declared class.
·         Therefore each declared class type can become a reference   data type.






Method parameters
The compiler identifies / differentiate a method using the method signature.

class Calculator{
void add ( int x , int y ){
                 System.out.println();
}
public static void main(String args[]){
Calculator c1= new Calculator();
             c.add( 2,3);
c.add(-2,10);

}
}



In the line   void add ( int x , int y ){ , the method parameters are int x , int y  .

             c.add( 2,3);
c.add(-2,10);

2,3 ; -2,10 are method arguments .


  
Method overloading.
Declaring multiple methods in a class with the same method name but with different method parameters.

       
class Calculator{

void add ( int x , int y ){
                       System.out.println();
}

void add ( double x , double y ){
                       System.out.println();
}


       public static void main(String args[]){
       Calculator c1= new Calculator();
                    c.add( 2,3);

       }
}


Method return types.

class Test{
                void m(){ -----------------------------------à no value will be return.
int x;
x=10;
int y =3;
System.out.println(“Total = ”+ (x+y) );


}
               
int d(){  -----------------------------à int value will be return.
int x;
x=10;
int y =3;
System.out.println(“Total = ”+ (x+y) );
Return ( x+y );              ->>>>>>>> return statement
}             

public static void main (String args []){
Test t= new Test();
           t.m();
           int x = t.d();
           System.out.println ( x );

}

}








No comments:

Post a Comment