blog

Home / DeveloperSection / Blogs / Class Concepts in Java: Declaring Methods

Class Concepts in Java: Declaring Methods

Ailsa Singh 1553 21-May-2016

Our Point class declared here contains only fields. Now, we will add a method to the Point class which will help us understand the method declaration and calling syntax.

class Point
 {
    int x;
    int y;
}

 

The aim of adding a method to the class definition is to give some functionality to it. The functionality we are going to add to our Point class is to determine the distance of the point from the origin. Thus, we will add a method called getDistance() that  will return the distance between the point and the origin.

Point class with method declaration:
import java.util.*;

class Point {
             int x;
             int y;
             double getDistance() {
                   return (Math.sqrt(x * x + y * y));
          }
}
 Test Class:  

class TestPoint
 {
          public static void main(String[] args)
             {
                   System.out.println("Create a Point object ... ");
                   Point p1 = new Point();
                   System.out.println("Initializing an object ...");

                   p1.x = 6;
                   p1.y = 8;
                   double distance = p1.getDistance();
                   StringBuilder sb = new StringBuilder();
                   Formatter formatter = new Formatter(sb, Locale.US);
                   formatter.format("Distance of Point p1(" + p1.x + "," + p1.y                                                                           + ") from origin is %.02f", distance);
                 System.out.println(sb);
                   System.out.println();
                   sb.delete(0, sb.length());
                   System.out.println("Create another Point object ... ");
                   Point p2 = new Point();
                   System.out.println("Initializing an object ...");
                   p2.x = 3;
                   p2.y = 4;
                   distance = p2.getDistance();
                   formatter.format("Distance of Point p2(" + p2.x + "," + p2.y                                       + ") from origin is %.02f", distance);
                   System.out.println(sb);
          }
}

 Output:

Create a Point object ...

Initializing an object ...

Distance of Point p1(6,8) from origin is 10.00

Create another Point object ...

Initializing an object ...

Distance of Point p2(3, 4) from origin is 5.00
 

The Point class now has a method added to its definition. The method getDistance computes the point’s distance from the origin and returns a double value to the caller:

double getDistance() {
return (Math.sqrt(x * x + y * y));
}

The distance is computed using the sqrt method of the built-in Math class. In the main method of the TestPoint class, first we create an instance, p1, of the Point class and after this we initialize its data members using the syntax we used in our earlier example. Then, we call the getDistance method to calculate the point’s distance from the origin:

double distance = p1.getDistance();

To invoke or call a method, we use the syntax objectReference.methodName. In this case, objectReference is the reference to the Point object (that is, p1) and methodName is getDistance. To dump the object’s contents along with the distance, we use the StringBuilder class:

StringBuilder sb = new StringBuilder();


Formatter formatter = new Formatter(sb, Locale.US);

formatter.format("Distance of Point p(" + p1.x + ","

                                                                        + p1.y + ") from origin is %.02f", distance);

System.out.println (sb);

 

After building the string, we print it to the console. To illustrate that we can create more than one Point object in our application using the same class definition, we have created another instance of the Point class called p2. The program initializes its fields to some values, computes its distance from the origin by calling the getDistance() method, and dumps it along with the object’s contents to the console.

As we can see, the same functionality (getDistance) is exhibited by both objects of the Point class. Thus, the methods allow us to define a common functionality for all objects belonging to the same class type.


Updated 15-Mar-2018

Leave Comment

Comments

Liked By