How to create your own helper class in Java

In this section, we are going to learn about the Helper Class in Java. Helper Class is used to hold the functions that are going to be used repetitively in the multiple classes. These are the functions that can be used outside as well as inside the helper class. Helper methods are generally declared as ” public static ” so that they can be invoked independently with the class name like Classname.method() and can be accessed from anywhere in the class. We can have more than one helper class in the same class according to our requirements. Different helper classes belonging to the same class can have a function with the same name.

Java program to implement a Helper Class

public class TestingClass {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    int arr[]= {1,2,3,3,4,5};
    int target=3;
    Helper h1=new Helper();
    System.out.println("--------UpperBound--------");
    System.out.println(h1.UpperBound(arr, target));
    System.out.println("--------LowerBound---------");
    System.out.println(h1.LowerBound(arr, target));
    System.out.println("--------BinarySearch--------");
    System.out.println(h1.binarySearch(arr, target));
    System.out.println("--------Sum-----------------");
    System.out.println(h.sum(arr[0],arr[1]));
  }

}
class h{
  public static int sum(int a,int b) {
    return a+b;
  }
}
class Helper{
  
  public static int UpperBound(int arr[],int target) {
    int lo=0,hi=arr.length-1,mid=0,ind=-1;
    while(lo<=hi) {
      mid=(lo+hi)/2;
      if(arr[mid]<target) {
        lo=mid+1;
        
      }
      else if(arr[mid]>target) {
        hi=mid-1;
      }
      else {
        ind=mid;
        lo=mid+1;
      }
    }
    return ind;
  }
  
  public static int LowerBound(int arr[],int target) {
    int lo=0,hi=arr.length-1,mid=0,ind=-1;
    while(lo<=hi) {
      mid=(lo+hi)/2;
      if(arr[mid]>target) {
        hi=mid-1;
      }else if(arr[mid]<target) {
        lo=mid+1;
      }else {
        ind=mid;
        hi=mid-1;
      }
    }
    return ind;
  }
  
  public static int binarySearch(int arr[],int item) {
    int lo=0;
    int hi=arr.length-1;
    int mid=0;
    while(lo<=hi) {
      mid=(lo+hi)/2;
      if(arr[mid]<item) {
        lo=mid+1;
      }
      else if(arr[mid]>item) {
        hi=mid-1;
      }
      else {
        return mid;
      }
    }
    return -1;	
  }
}

Output

--------UpperBound--------
3
--------LowerBound---------
2
--------BinarySearch--------
2
--------Sum-----------------
3

Understanding  of the program

  • We have created two helper classes,i.e Helper and h.
  • Helper includes three functions, i.e. UpperBound: to find the position of the last occurrence of a given number in a sorted array. Lower bound: to find the position of the first occurrence of a given number in a sorted array. BinarySearch: to find the index on which element is found in the array.
  • h includes one function, i.e. Sum: to return the sum of two numbers.
  • Then, we have called the function of helper classes in the main class.

Also read: How to create object of a Java class?

Leave a Reply