import javax.swing.*;

//============= Function to be define ================

class fb extends f_x
{
   public double func (double x)
   { return x*x-4.0;}
}
//===========================================================
public class NA2
{
public static double false_position(f_x f,double xl,double xu)
{
//false position root finding method
double test;
double xr=0;
double es,ea;
double fxl,fxu,fxr;
int maxit=100,iter=0;
es=0.001;
ea=1.1*es;
fxl= f.func(xl);
fxu= f.func(xu);
while((ea>es)&&(iter<maxit))
{
	xr=xu-fxu*(xl-xu)/(fxl-fxu);
	fxr= f.func(xr);
    iter++;
    if((xl+xu)!=0)
        { ea=Math.abs((xu-xl)/(xu+xl))*100;System.out.println("ea="+ea);}
    test= fxl*fxr;
    if(test==0.0)      ea=0;
    else if(test<0.0)  {xu=xr;fxu=fxr;}
    else               {xl=xr;fxl=fxr;}
} 
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); 
return xr;
}

     public static void main (String args[])  
     { 
     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;
     fb f=new fb();
     r= false_position(f,a,b);
     JOptionPane.showMessageDialog(null," ROOT  : "+r+"\nFUNCTION VALUE : "+f.func(r),
     "false position root finding method : ",JOptionPane.PLAIN_MESSAGE);
     System.exit(0);
     } 
}