Wednesday, June 15, 2016

Method

As explained in previous post, the class contains a collection of data and method. Here is the general form of writing method in the class.

type methodName(list-of-parameter) {
   //code goes here
}

In the common form above, type is the data type that will be returned by the method. In Java, method is divided into two types: void and non-void. Method void is a method that return a value, and method non-void is a method that return a value. For a void method, returned data type should be written with void keyword.

If the method that we create is intended to return a value, then return statement must be included in the method, followed by the value to be returned, as follows:

return value;

value can be either constant or variable.


Defining Method


Here we will define a method into a class that we previously created, the Box class. We'll add a method printVolume() to print the value of the volume of the box object. This method is a method that does not return a value. Therefore, we must change the type of return with the void keyword. Here is the program code:

class Box {
   double length:
   double width;
   double heigth;

   //Define void method (does not return value)
   void printVolume() {
      System.out.println("Box volume = " + (length * width * heigth));
   }
}

class DemoMethod1 {
   public static void main(String[] args) {
      Box b1, b2, b3;

      b1 = new Box();
      b2 = new Box();
      b3 = new Box();

      //load data to object b1
      b1.length = 4;
      b1.width = 3;
      b1.heigth = 2;

      //load data to object b2
      b2.length = 6;
      b2.width = 5;
      b2.heigth = 4;

      //load data to object b3
      b3.length = 8;
      b3.width = 7;
      b3.heigth = 6;

      //call printVolume() method for each object
      b1.printVolume();
      b2.printVolume();
      b3.printVolume();
   }
}


The result given by the above program as follows:

Box volume = 24.0
Box volume = 120.0
Box volume = 336.0

Method call in a class is done by writing the object name and followed by a dot (.) and the method name to be executed.


Return a Value ​in Method


In the section above you've seen how to define a method does not return a value. Now, we will continue to defining method that return a value. Here, we will modify the previous program by changing the method printVolume() into method calculateVolume(), that will return a value of double type. Here is the program code:

class Box {
   double length:
   double width;
   double height;

   //Define method that return double type
   double calculateVolume() {
      //calculate volume
      double vol = length * width * height;
      //return value
      return vol;
   }
}

class DemoMethod2 {
   public static void main(String[] args) {
      Box b1, b2, k 3;
 
      b1 = new Box();
      b2 = new Box();
      b3 = new Box();

      b1.length = 4;
      b1.width = 3;
      b1.height = 2;

      b2.length = 6;
      b2.width = 5;
      b2.height = 4;

      b3.length = 8;
      b3.width = 7;
      b3.height = 6;

      System.out.println("Volume b1 = " + b1.calculateVolume());
      System.out.println("Volume b2 = " + b2.calculateVolume());
      System.out.println("Volume b3 = " + b3.calculateVolume());
   }
}



In the program above, method calculateVolume() returns a value of double type. The result of above program as follows:

Volume b1 = 24.0
Volume b2 = 120.0
Volume b3 = 336.0

The data type that returned by the method must be compatible to the type that defined in method. For example, the method that defined return boolean value can not return an integer value. If you want to store the value returned by method into a variable, then the data type of the variable must be compatible to the type that defined in the method. For example, if you want to store the value returned by the method calculateVolume() to vol variable, then the vol variable must be double type (according to the returned type that in calculateVolume() method.


Parameterized Method


Method can has one or more parameters. Method can return the value that varies according to the value of parameter. Take a look following without parameter method:

int rectangleArea() {
   return 10 * 5;
}

syntactically, the above method is correct and will return value 50. However, the above method is not flexible, because the method can only be used for the rectangle which has size 10 x 5 only. What if there is a rectangle whose length is 4 and width is 3? To solve such as this case, Java allows us to put the parameter in a method so that the method can be used for any size of rectangle. Here is the code that should be written:

int rectangleArea(int length, int width) {
   return length * width:
}

Through this way, we can fill any value into the method. For example, consider the following code:

int area1, area2, area3;

area1 = rectangleArea(10, 5); //The result is 50
area2 = rectangleArea(4, 3); //The result is 12
area3 = rectangleArea(5, 2); //The result is 10

There are two terms you need to know in working with the method: parameter and argument. A parameter is a variable that is defined in the method creation, while the argument is the value used when calling the method. Consider again the following method definition:

int rectangleArea(int length, int width) {
   return length * width;
}

Here, the length and width variables called parameter.

area1 = rectangleArea(10, 5);

As in the above statement, the value of 10 and 5 are called argument. Now, we are going to implement the above concepts into Box class. Here, the length, width, and height data will be filled by a method. Here is the program code:

class Box {
   double length;
   double width;
   double height;

   //Define method that using parameter
   void fillData (double l, double w, double h) {
      length = l;
      width = w;
      height = h;
   }

   double calculateVolume() {
      return (length * width * height)
   }
}

class DemoMethod3 {

   public static void main(String[] args) {

      Box b = new Box();

      //call fillData() method
      b.fillData(4, 3, 2);

      System.out.println("Box volume = " + b.calculateVolume());
   }
}


The results will be given by program above as follows:

Box volume = 24.0

In the above program we define a method that does not return a value and has three parameters: l, w, and h. These parameters then can be filled with the desired value during the process of the method call. In the above example, we pass value of 4, 3 and 2 as the argument of the method fillData() to fill length, width, and height data of the b object.

3 comments:

  1. Hi , While reading this tutorial i found some typo error. this would be the typo for experienced people but for fresher it will misguide them.

    "In Java, method is divided into two types: void and non-void. Method void is a method that return a value, and method non-void is a method that return a value".
    Here for void method return should not value.

    ReplyDelete
  2. Thank You for Sharing valuable information with us.

    E-Commerce is the fastest-growing platform which is becoming more demandable day by day this is a tremendous form of the vibrant sector that is bringing several opportunities to the new generation to become more skilled and professional. Taking up this great advantage in the E-Commerce sector would need the right path and the right skillset you need to learn the latest technologies and high-demand skills such as building payment gateways and developing an accessible and easy website along with the basics of E-commerce that‘s why Aptech learning official Pro E-Commerce training program in Dwarka, Janakpuri, and Gurgaon which will help to make a successful career in this booming industry. Java Programming Course helps to become an expert in Java web application development. It helps you to bring efficiency in collecting and analyzing the data through Java programming in this course you will learn effective web UI/UX design and Secure web applications by using the advanced technologies of the Java program.

    For more details please visit our website : Aptech Dwarka

    ReplyDelete