Computer Science, asked by soniya4818, 1 year ago

how to make functions in a java program?

Answers

Answered by Anonymous
3
Simple custom Java functions can be called in expressions in the context of most StreamBase components, other than the Aggregate operator. Follow these steps to create a custom Java simple function:

Use the New StreamBase Java Function wizard to create the base code, as described in Using the StreamBase Java Function Wizard.

Implement a public static in a public Java class.

Observe the guidelines in Method Parameter and Return Types.

For example, Hypotenuse.java in the sample application declares the following class and method:

public class Hypotenuse { public static double calculate(double a, double b) { return Math.sqrt(a*a + b*b); } }

At compile time, the calljava() implementation looks for only a single method matching the exact types of the function call in the StreamBase expression. But there can be multiple matching methods, such as these two functionally equivalent ones:

public static boolean isZero(int i) { return i == 0; } public static Boolean isZero(Integer i) { return i == null ? null : Boolean.valueOf(i.intValue() == 0); }

If this case occurs, StreamBase throws an error.

Creating Custom Java Aggregate Functions

Follow these steps to create a custom Java aggregate function:

Use the New StreamBase Java Function wizard to create the base code, as described in Using the StreamBase Java Function Wizard.

Define a Java class that extends the com.streambase.sb.operator.AggregateWindowclass.

Observe the guidelines in Method Parameter and Return Types.

Observe the guidelines in the annotations to the example below.

Consider the following annotated example:

package com.mycompany; import com.streambase.sb.operator.AggregateWindow; public class MyStdev extends AggregateWindow {  private double sum; private double sumOfSquares; private int count; public void init() {  sum = sumOfSquares = 0.0; count = 0; } public double calculate() {  return Math.sqrt((count * sumOfSquares - sum*sum) / count*(count-1)); } public void accumulate(double value) {  sum += value; sumOfSquares += value*value; ++count; } public void release() { /* nothing to release in this example */ }  }

The following annotations describe points of interest in the preceding example:



Declare a public class that extends the AggregateWindow class (as documented in the StreamBase Java Client library).



The init() method is called at the start of each new use of the class. Since custom aggregate objects are likely to be reused, perform all initialization in init() rather than in the constructor. (The constructor is called only once, while init() is called before each use.)



Your implementation must contain a calculate()method that takes no arguments and returns a value that is convertible to a StreamBase data type. The calculate() method may be called several times, or not at all.



Your implementation must provide at least one accumulate() method, and can optionally provide several overloaded accumulate() methods, one per data type. calljava() determines which one to call based on type. The argument types for accumulate() and the return type for calculate() can be any of the types described in the table in the next section.



The release() method is called at the end of each use of the class.

Similar questions