Wednesday, 11 July 2012

Java - Methods

Java - Methods

A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println method, for example, the system actually executes several statements in order to display a message on the console.

Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, overload methods using the same names, and apply method abstraction in the program design.
Creating a Method:

In general, a method has the following syntax:

modifier returnValueType methodName(list of parameters) {
  // Method body;
}

A method definition consists of a method header and a method body. Here are all the parts of a method:

    Modifiers: The modifier, which is optional, tells the compiler how to call the method. This defines the access type of the method.

    Return Type: A method may return a value. The returnValueType is the data type of the value the method returns. Some methods perform the desired operations without returning a value. In this case, the returnValueType is the keyword void.

    Method Name: This is the actual name of the method. The method name and the parameter list together constitute the method signature.

    Parameters: A parameter is like a placeholder. When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.

    Method Body: The method body contains a collection of statements that define what the method does.



Note: In certain other languages, methods are referred to as procedures and functions. A method with a nonvoid return value type is called a function; a method with a void return value type is called a procedure.
Example:

Here is the source code of the above defined method called max(). This method takes two parameters num1 and num2 and returns the maximum between the two:

/** 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;
}


Here is the code of the program :
class square{
  int sqarea(int side){
  int area = side * side;
  return(area);
  }
  int sqpari(int side){
  int pari = 4 * side;
  return(pari);
  }
}
class rectangle{
  int rectarea(int length,int breadth){
  int area = length * breadth;
  return(area);
  }
  int rectpari(int length,int breadth){
  int pari = 2*(length + breadth);
  return(pari);
  }
}
public class ObjectClass{
  public static void main(String args[]){
  int sarea1,sarea2,pari1,pari2;
  int rectarea1,rectarea2,rectpari1,rectpari2;
  square sq = new square();
  rectangle rect = new rectangle();
  int a=20;
  System.out.println("Side of first square = " + a);
  sarea1 = sq.sqarea(a);
  pari1 = sq.sqpari(a);
  System.out.println("Area of first square = " + sarea1);
  System.out.println("Parimeter of first square = " + pari1);
  a = 30;
    System.out.println("Side of second square = " + a);
  sarea2 = sq.sqarea(a);
  pari2 = sq.sqpari(a);
  System.out.println("Area of second square = " + sarea2);
  System.out.println("Parimeter of second square = " + pari2);
  int x = 10, y = 20;
  System.out.println("Length of first Rectangle = " + x);
    System.out.println("Breadth of first Rectangle = " + y);
  rectarea1 = rect.rectarea(x,y);
  rectpari1 = rect.rectpari(x,y);
  System.out.println("Area of first Rectangle = " + rectarea1);
  System.out.println("Parimeter of first Rectangle = " + rectpari1);
  x = 15;
  y = 25;
  System.out.println("Length of second Rectangle = " + x);
  System.out.println("Breadth of second Rectangle = " + y);
  rectarea2 = rect.rectarea(x,y);
  rectpari2 = rect.rectpari(x,y);
  System.out.println("Area of second Rectangle = " + rectarea2);
  System.out.println("Parimeter of first Rectangle = " + rectpari2);
  }
}

No comments:

Post a Comment