import static java.lang.Math.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class NA1
{

public static double bisection(inter_f_x f,double xl,double xu)
{
//bisection root finding method
double test;
double xr=0;
double es,ea;
double fxl,fxr,fxu;
int maxit=100,iter=0;
es=0.00000001;
ea=1.1*es;
String s="";
fxl= f.func(xl);
fxu= f.func(xu);

if(fxl*fxu>0) System.out.println("xl="+xu+"xr="+xr+"fxl="+fxl+"fxu="+fxu+"error");
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);
    s+="xr="+xr+"fxr="+fxr+"iter="+iter+"\n";
    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); 
//JOptionPane.showMessageDialog(null,s,
//     "bisection root finding method : ",JOptionPane.PLAIN_MESSAGE);
   return xr;
}
     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 : "));
     double r;
     inter_f_x f1=x->x*x-2.0;
     r= bisection(f1,a,b);
     JOptionPane.showMessageDialog(null," ROOT  : "+r+"\nFUNCTION VALUE : "+f1.func(r),
     "bisection root finding method : ",JOptionPane.PLAIN_MESSAGE);
     System.exit(0);
     } 
}