import java.io.*;
import javax.swing.*;
import javax.tools.*;
import java.util.*;
import java.lang.Integer;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Locale;
import java.text.*;
import java.util.*;
import javax.swing.table.*;

class ce15 
{ 	 	
	 public ce15(double x1,double x2,String s) 
	 {     
        double r;
        r= adaptive_simpson_integral(new fa(),x1,x2);
        JOptionPane.showMessageDialog(null," adaptive simpson integal  : "+r,
        "f(x) = "+s,JOptionPane.PLAIN_MESSAGE);
     }

public static double adaptive_simpson_integral(f_x ff,double a,double b)
{return adaptive_simpson_integral(ff,a,b,1.0e-20,30);}

public static double adaptive_simpson_integral(f_x ff,double a,double b,double eps)
{return adaptive_simpson_integral(ff,a,b,eps,30);}

public static double adaptive_simpson_integral(f_x ff,double a,double b,int MAX_ITERATION)
{return adaptive_simpson_integral(ff,a,b,1.0e-12,MAX_ITERATION);}

public static double adaptive_simpson_integral(f_x ff,double a,double b,double eps,int MAX_ITERATION)
{   int i;
    double h=(b-a);
    double h2=h/4.0;
	double x[]=new double[5];
    double f[]=new double[5];
    double w1[]=new double[3];
    double z1[]=new double[3]; 
    int j;
    for(i=0;i<5;i+=2) {j=i/2;x[i]=a+i*h2;f[i]=ff.func(x[i]);w1[j]=x[i];z1[j]=f[i];}
	double sum=h*(f[0]+4.0*f[2]+f[4])/6.0; 
	double Q=0;
	Plot pp=new Plot(ff,a,b,100);
	pp.setPlotType(0,0);// 	 
	pp.addData(w1,z1,28);
	int k=0;
    Q=simpson1_3(ff,x,f,sum,eps,k,MAX_ITERATION,Q,pp);
    pp.plot();
    return Q;	
}
	
public static double simpson1_3(f_x ff,double x[],double f[],double sum,double eps,int k,int MAX_ITERATION,double Q,Plot pp)
{
        //adaptive Simpson 1/3 rule
        double h=(x[4]-x[0]);
        double h1=h/2.0;
        double h2=h/4.0;        
        int p,i;
        double x1[]=new double[5];
        double f1[]=new double[5];
        double x2[]=new double[5];
        double f2[]=new double[5];
        double w1[]=new double[2];
        double z1[]=new double[2]; 
        int j;
        for(i=1;i<5;i+=2) {j=i/2;x[i]=x[0]+i*h2;w1[j]=x[i];f[i]=ff.func(x[i]);z1[j]=f[i];}           
        double sum1=h1*(f[0]+4.0*f[1]+f[2])/6.0;
        double sum2=h1*(f[2]+4.0*f[3]+f[4])/6.0;     
        x1[0]=x[0];f1[0]=f[0];
        x1[2]=x[1];f1[2]=f[1];
        x1[4]=x[2];f1[4]=f[2];
        x2[0]=x[2];f2[0]=f[2];
        x2[2]=x[3];f2[2]=f[3];
        x2[4]=x[4];f2[4]=f[4];  
        k++;    
        if(1.0/15.0*Math.abs(sum1+sum2-sum)<eps || k>MAX_ITERATION) Q+=sum1+sum2;        
        else Q+=simpson1_3(ff,x1,f1,sum1,eps,k,MAX_ITERATION,Q,pp)+simpson1_3(ff,x2,f2,sum2,eps,k,MAX_ITERATION,Q,pp);
        pp.addData(w1,z1,28);
        //pp.setPlotType(k,28);//              
        return Q;
}

	 
 }
