import javax.swing.*;
import java.io.*;
public class NA1a
{    
public static double[] bisection(double xl,double xu)
{
f2D f=new f2D();
//bisection root finding method
double test;
double xr=0;
double es,ea;
double fxl,fxr;
int maxit=100,iter=0;
es=0.00000001;
ea=1.1*es;
while((ea>es)&&(iter<maxit))
{
    xr=(xl+xu)/2.0;
    iter++;
    if((xl+xu)!=0)
        { ea=Math.abs((xu-xl)/(xu+xl))*100;}
    fxl= f.func(xl);
    fxr= f.func(xr);
    test= fxl*fxr;
    if(test==0.0)      ea=0;
    else if(test<0.0)  xu=xr;
    else              {xl=xr;}
} 
if(iter>=maxit) JOptionPane.showMessageDialog(null,"Maximum number of iteration is exceeded \n"+
   " result might not be valid","MAKSİMUM NUMBER OF ITERATION WARNING",JOptionPane.WARNING_MESSAGE); 
double xx[]={xr,f.func(xr)};
return xx;
}
     public static void main (String args[]) throws IOException  
     { 
     double a,b;   
     a=Double.parseDouble(JOptionPane.showInputDialog(" INPUT LOWER LİMİT OF SEARCH AREA a : "));
     b=Double.parseDouble(JOptionPane.showInputDialog(" INPUT UPPER LIMIT OF SEARCH AREA b : "));
     String s=JOptionPane.showInputDialog(" INPUT FUNCTION f(x): ");
     NA.defineFunc(s);
     double r[]= bisection(a,b);
     JOptionPane.showMessageDialog(null," ROOT  : x = "+r[0]+"\nFUNCTION VALUE : ("+s+") = "+r[1],
     "bisection root finding method : ",JOptionPane.PLAIN_MESSAGE);
     System.exit(0);
     } 
}