Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

User Defined Functions in Java

In this article, we will focus on the core Java language features of User define function.

Java is a modern, evolutionary computing language that associates an elegant language design with powerful features that were previously available primarily in some special languages. In addition to the core language features, Java platform scatterings include different powerful and supporting software libraries and APIs for tasks such as database, network, and graphical user interface (GUI) programming.

Java is a complete object oriented programming language. This language supports all rules and regulation of object oriented programming language. Function is very important part in our language specification and software development criteria so that using the function model we can develop our programming strategy.

Generally this function is of two types, one is predefined function and other is user defined function. User defined function is totally based on the user control and through this control we can develop our own function model and easily set in our commercial project.

A Java program is basically a set of classes. A class is defined by a set of declaration statements and methods or functions. The java programming supports different types of function as line User defined function [A function which is created by user so it is known as user defined function.] and also there are some functions available with java system that provides some imperative support to the java programmer for developing their programming logic as well as their programming architecture with very smooth and fine way.

A method is the object-oriented equivalent of a procedure in non-object-oriented languages. That is, a method is a program paradigm that affords the mechanism (method) for performing some action. Given an instance of some entity, we invoke behavior with a dot (.) syntax that links an instance with a method in the class definition:

Listing 1: Method Invocation Syntax is given below

<instance>.<behavior>()
<variable> = <instance>.<behavior>(<arguments>...)

Listing 2: Method Definition Syntax is given below

void <method-name>(<arguments>...) {
<statements>...
}
<return-type> <method-name>(<arguments>...) {
<statements>...
}

With the Java class and method syntax in hand, we can enterprise a Java program. Java applications consist of one or more classes that define data and behavior. Java applications are interpreted into a distilled format by a Java compiler. Now we discuss some major features present in java user defined function that is following.

Features of user define function in Java:

  • Using the user defined function model, we can set our own function as per our requirement.
  • Function type may change as like return value types (int, char, double) function or only return type function like void.
  • UDF (User define function) always accessed publicly, privately and protected formation.
  • When we create user define function name then we must remember some special point that points are given below:
    • We cannot use any reserved word that includes existing with the System function names.
    • We cannot overload existing java System functions name.
    • The function name must be irreplaceable among user-defined functions for the number of arguments.
    • We can use the same function name for the different numbers of types of arguments list.
    • So, we can overload our user-defined functions in our system.
    • The function name can only comprehend letters, numbers, and the underscore (_).
    • But you must be remembering that the function name must start with a letter.
    • The function names which cannot be exceed 128 characters.
  • Function overloading is very popular concept of User defines function. This process also called polymorphism that means “Single Interface multiple Method”. By using this protocol we use a function name through different function arguments and also work with very popular way at the time of software developing process.

Concept of function model:

Function models are divided basically in the following four categories. These categories are given below:

  • Function returns type and does not return value.
  • Function returns type and does return value.
  • Function returns type and return array.
  • Function returns type and does return pointer.

Listing 3: Showing calculation

class sample
{
    float calculation(float x, float y)	//float type parameterized function 
    {
        return x + y;
    }
    void calculation (int x, float y)	    // void type parameterized function
    {
        	double s=x+y;
	System.out.println("Sum of the value.. = " + s);

    }
    int calculation(int x, int y)	//int type parameterized function, formal parameter
    {
        return x + y;
    }
}
class func
{
    public static void main(String srimani[])
    {
        	sample ob=new sample();   //Object creation part		
     	int s=ob.calculation(145,125); //Actual function call integer method 
	float f=ob.calculation(14.5,1.25); //Actual function float method
    }
}

Through the above example we can see what types of value can be accessed through the function. And how can we pass data in actual function through formal parameter.

Access Methods:

In order for the value of an instance variable to vary over time span, we can use a method to change its value; such a method is typically referred to as an access method. By convention, a method that is provided to simply affect a change to an instance variable's value begins with the word "set":

void setBark(String barkSound) 
{
this.barkSound = barkSound;
}

This method is interesting because it uses two different variables with the same name, barkSound. First, the barkSound defined as a parameter in the new barking sound. So any not qualified reference to barkSound in this method refers to the data passed as an argument. We also have a barkSound instance variable for each dog that is instantiated in the application. With the help of Java, we can utilize the special "instance handle" which refers to the current instance of Dog.

Here this.barkSound = barkSound; replaces the current value of the instance variable (this.barkSound with the new value passed as an argument (barkSound) to setBark(). To put the variable in standpoint, supposing we create an instance of Dog referred to as cei. Then, if we execute setBark() with respect to cei, namely, cei.setBark("Ruff."); the this instance in setBark()is cei and, in actual, this.barkSound is the instance variable of barkSound for the cei object.

List of some user define function in through Java program:

Listing 4: Through this java program we can process data and also execute the same method using static function model.

// A sample program of static member
class calc
{
    static float add(float x, float y)
    {
        return x + y;
    }
    static float divide(float x, float y)
    {
        return x / y;
    }
}
class staticta
{
    public static void main(String args[])
    {
        float a= calc.add(6,2);
        float d= calc.divide(a,2);
        System.out.println("Division = " + d);
    }
}

Listing 5: Through this java program we can process data and also execute the function using static function model.

class usstatic
{
      static int a=3;
      static int b=10;
      static void callme()
      {
          System.out.println("a="+a);
      }
}
public class staticex
{
     public static void main(String args[])
     {
          usstatic.callme();    //static function call without object direct through class name
          System.out.println("b="+usstatic.b);   //calling static variable without object
     }
}

Listing 6: Through this java program we can process data and also execute the factorial value using recursive function model.

class factorial
{
    // this is recursive function
    int fact(int n)
    {
        int result;
        if (n==1) 
return 1;
        result = fact(n-1) * n;
        return result;
    }
}
class recursion
{
    public static void main(String args[])
    {
        factorial f = new factorial();
        System.out.println("Factorial of 4 = " + f.fact(4));
    }
}

Listing 7: Through this java program we can input data and also execute the interest by the function model.

import java.io.*;
class invest
{
	public static void main(String args[])
	{
		Float pramt=new Float(0);
		Float rate=new Float(0);
		int time=0;
		try
		{
			DataInputStream in=new DataInputStream(System.in);

			System.out.print("Enter Principal Amount      :");
			System.out.flush();
			String pramtString=in.readLine();
			pramt=Float.valueOf(pramtString);

			System.out.print("Enter Interest Rate(%)      :");
			System.out.flush();
			String rateString=in.readLine();
			rate=Float.valueOf(rateString);

			System.out.print("Enter Number of Years       :");
			System.out.flush();
			String timeString=in.readLine();
			time=Integer.parseInt(timeString);
		}
		catch(IOException e)
		{
			System.out.println("I/O Error.......");
			System.exit(1);
		}
		float value=loan(pramt.floatValue(),rate.floatValue(),time);
		printline();
		System.out.println("Final Value..."+value);
		printline();
	}
	//Method to compute Final Value
	static float loan(float p,float r,int t)
	{
		int y=1;
		float sum=p;
		while(y<=t)
		{
			sum=sum*(1+r);
			y=y+1;
		}
		return sum;
	}
	//Method to draw line
	static void printline()
	{
		for(int i=1; i<=30;i++)
		{
			System.out.print("=");
		}
		System.out.println(" ");
	}
}

Conclusion

There is no confusion that user define function is taken a major role in the platform of the software development. So in every platform the user defines function is very popular as well as very essential for all types of programming. We have a clear idea that predefine function have lot of facility to give us at the time of software development or system development so that using the features of user define function we set up all types of our requirement job and also access the data or method from all over the system if it is declare for public method process.



Website: www.techalpine.com Have 16 years of experience as a technical architect and software consultant in enterprise application and product development. Have interest in new technology and innovation area along with technical...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login