import javax.swing.*;
public class reactiontest4
{
public static double Q(String s[],double N[][],double Treactant,double Tproduct)
{int k=s.length;
Gas g[]=new Gas[k];
for(int i=0;i<k;i++)
{g[i]=new Gas(s[i]);
}
double Qproduct=0;
double Qreactant=0;
for(int i=0;i<k;i++)
{Qproduct+=N[i][1]*g[i].ht(Tproduct);}
for(int i=0;i<k;i++)
{Qreactant+=N[i][0]*g[i].ht(Treactant);}
double Q=Qproduct-Qreactant;
return Q;
}

public static double Tadiabatic(String s[],double N[][],double Treactant,double Tproduct_low,double Tproduct_high)
{
//bisection root finding method
double xl=Tproduct_low;
double xu=Tproduct_high;
double test;
double xr=0;
double es,ea;
double fxl,fxr;
int maxit=100,iter=0;
es=0.0000001;
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= Q(s,N,Treactant,xl);
    fxr= Q(s,N,Treactant,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); 
return xr;
}


public static void main(String arg[])
{
String s[]={"ch4","o2","n2","co2","h2o"};
double N[][]={{1.0,0.0},{2.0,0.0},{7.523809524,7.523809524},{0.0,1.0},{0.0,2.0}};
double Tproduct=1000.0;//degree K
double Treactant=298.16;//degree K
double Tadiabatic=Tadiabatic(s,N,Treactant,298.16,3000.0);
double Q=Q(s,N,Treactant,Tproduct);
System.out.println("Tadiabatic="+Tadiabatic);
System.out.println("Q="+Q);	
}	
}