title: Numeric Integration in Java
body: <p>I need your help again.</p>
  <p>I need to know how I can send any functions to my class in order that my method works.</p>
  <p>For example, in the code below, I can send only log function. But I would like to send any expressions as a function. Did I clear?</p>
  <p>Thanks everyone for helping.</p>
  <p><strong>The class:</strong>
  package appMatematica;</p>
  <pre><code>import java.util.*;
  
  public class Simpsons13 {
  
      // Given function to be integrated
        // Function to calculate f(x)
      static float func(float x)
      {
          return (float)Math.log(x);
      }
   
      // Function for approximate integral
      static float simpsons13(float ll, float ul,
                                         int n)
      {
          // Calculating the value of h
          float h = (ul - ll) / n;
   
          // Array for storing value of x
          // and f(x)
          float[] x = new float[10];
          float[] fx= new float[10];
   
          // Calculating values of x and f(x)
          for (int i = 0; i &lt;= n; i++) {
              x[i] = ll + i * h;
              fx[i] = func(x[i]);
          }
   
          // Calculating result
          float res = 0;
          for (int i = 0; i &lt;= n; i++) {
              if (i == 0 || i == n)
                  res += fx[i];
              else if (i % 2 != 0)
                  res += 4 * fx[i];
              else
                  res += 2 * fx[i];
          }
           
          res = res * (h / 3);
          return res;
      }
  }
  </code></pre>
  <p><strong>THE MAIN:</strong></p>
  <pre><code>package appMatematica;
  
  import java.util.Scanner;
  import appMatematica.Gauss;
  import appMatematica.Simpsons38;
  import appMatematica.Trapezios;
  
  public class Principal {
  
      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          int op = 0;
  
          System.out.println(&quot;Escolha qual operação quer realizar\n&quot;);
          System.out.println(&quot;1-Trapézios \n2-Simpsons 3/8 \n3-Simpsons 1/3 \n4-Quadratura de GAUSS&quot;);
          op = sc.nextInt();
  
          if (op == 1) {
  
              double x1;
              double x2;
              int i;
              System.out.println(&quot;Digite o valor de x1\n&quot;);
              x1 = sc.nextDouble();
              System.out.println(&quot;Digite o valor de x2\n&quot;);
              x2 = sc.nextDouble();
              System.out.println(&quot;Digite o valor do intervalo\n&quot;);
              i = sc.nextInt();
              System.out.println(Trapezios.trapezio(x1, x2, i));
  
          }
  
          if (op == 2) {
  
              float limite_inferior;
              float limite_superior;
              int i;
  
              System.out.println(&quot;Digite o limite inferior\n&quot;);
              limite_inferior = sc.nextFloat();
              System.out.println(&quot;Digite o limite superior\n&quot;);
              limite_superior = sc.nextFloat();
              System.out.println(&quot;Digite o número de iterações\n&quot;);
              i = sc.nextInt();
  
              System.out.println(Simpsons38.calculate38(limite_inferior, limite_superior, i));
  
          }
  
          if (op == 3) {
  
              float limite_inferior;
              float limite_superior;
              int i;
  
              System.out.println(&quot;Digite o limite inferior\n&quot;);
              limite_inferior = sc.nextFloat();
              System.out.println(&quot;Digite o limite superior\n&quot;);
              limite_superior = sc.nextFloat();
              System.out.println(&quot;Digite o número de iterações\n&quot;);
              i = sc.nextInt();
  
              System.out.println(Simpsons13.simpsons13(limite_inferior, limite_superior, i));
  
          }
  
      }
  
  }
  </code></pre>