Sunday, 19 January 2014

INTRODUCING METHODS

In java,there are some fundamentals that you need to learn now so that you can begin to add methods to your classes.Although it is perfectly fine to create a class that contains only data,it rarely happens.Most of the time you will use methods to access the instance variables defined by the class.


General form of method:

type name(parameter-list)
{
  //body of the method
}


Here,type specifies the type of data returned by the method.If the method doesn't return a value,its return type must be void.The name of the method is specified by  name.the parameter-list is a sequence of type and identifier pairs seperated by commas.The method body contains a collection of statements that define what the method does.

Example:

class Areaexp
{
double length;
double breadth;
void area()
{
System.out.println("Area is ");
System.out.println(length*breadth);
}
}
class area1
{
public static void main(String args[])
(
Areaexp rec1=new Areaexp();
rec1.length=5;
rec1.breadth=2;
rec1.area();
}
}

output:
Area is 10

CALLING A METHOD


     In creating a method, you give a definition of what the method is to do. To use a method, you have to call or invoke it. There are two ways to call a method; the choice is based on whether the method returns a value or not.When a program calls a method, program control is transferred to the called method. A called method returns control to the caller when its return statement is executed or when its method-ending closing brace is reached.

Example:

public class TestMax 
{
   /** Main method */
   public static void main(String[] args) 
{
      int i = 5;
      int j = 2;
      int k = max(i, j);
      System.out.println("The maximum between " + i +
                    " and " + j + " is " + k);
   }
  /** Return the max between two numbers */
   public static int max(int num1, int num2) 
{
      int result;
      if (num1 > num2)
         result = num1;
      else
         result = num2;
        return result; 
   }
}

This would produce the following result:

The maximum between 5 and 2 is 5

RETURNING A VALUE


The return statement is used to return the value within the body of method.A method returns to the code that invoked it when it
  • completes all the statements in the method,
  • reaches a return statement, or
  • throws an exception (covered later), whichever occurs first.
You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value.
General form of return:

return; 

Example:

class ReturnValue
{
public static void main (String[] args)
{
System.out.println("The Biggest Number is: "+GetBiggestNumber(10, 15, 20));
}
public static int GetBiggestNumber (int num1, int num2, int num3)
{
int biggest = 0;
if ((num1 > num2) && (num1 > num3))
biggest = num1;
else
if ((num2 > num3) && (num2 > num1))
biggest = num2;
else
biggest = num3;
return biggest;
}
}

This would produce the following result:

The Biggest number is:20


METHOD PARAMETERS


Method parameters makes it possible to pass values to the method, which the method can operate on. The method parameters are declared inside the parantheses after the method name. In the example below the method parameters are marked in bold:

public void writeText(String text1, String text2) 
{
    System.out.print(text1);
    System.out.print(text2);
}

The method writeText method in the example above takes two parameters called text1 and text2. The parameters are both of type String as written in front of each parameter name.

No comments:

Post a Comment