import java.io.*;
import javax.swing.*;
import javax.tools.*;
import java.util.*;

class functiongenerator3D extends Thread
 {
  String fonksiyon;
  String dosyaismi;
  String sinif;
  //function input variable defined as w
  //If a function with x[] or y is given it should be converted to w
  public static String convertX_to_W(String s)
  { //converts array variable x[] to array variable w[]
    // in the input statement
  int n=s.length();
  String s1="",s2="",s3="";
  for(int i=0;i<n;i++)
  {  if(i<n-1) {s1=s.charAt(i)+""+s.charAt(i+1);s3=""+s.charAt(i);}
     else      {s1=""+s.charAt(i);s3=""+s.charAt(i);};
     if(s1.equals("a[") || s1.equals("b[") || s1.equals("b[") ||
        s1.equals("d[") || s1.equals("e[") || s1.equals("f[") ||
        s1.equals("g[") || s1.equals("h[") || s1.equals("i[") ||
        s1.equals("j[") || s1.equals("k[") || s1.equals("l[") ||
        s1.equals("m[") || s1.equals("n[") || s1.equals("o[") ||
        s1.equals("p[") || s1.equals("q[") || s1.equals("r[") ||
        s1.equals("s[") || s1.equals("t[") || s1.equals("u[") ||
        s1.equals("v[") || s1.equals("x[") || s1.equals("y[") ||
        s1.equals("z[")) s2+="w";
     else s2+=s3;
  }
  return s2;
  }
  public functiongenerator3D(String dosyaismi,String fonksiyon)
  {   
	  this.fonksiyon=fonksiyon;
      this.dosyaismi=dosyaismi;
  }

  public functiongenerator3D(String fonksiyon)
  {this.fonksiyon=fonksiyon;
   dosyaismi="f3D";
  }
  public functiongenerator3D()
  {fonksiyon="w[0]*w[0]+w[1]*w[1]-4.0";
   dosyaismi="f3D";
  }
  public void addfunction(String fonksiyon)
  {
   this.fonksiyon=fonksiyon;
  }
  
  public String toString()
  {
   return fonksiyon;
  }
    
 public  String createfunction()
 {String isim=dosyaismi+".java";
 String s="";
 String fonksiyon1=convertX_to_W(fonksiyon);
 try {PrintWriter fout=new PrintWriter(new BufferedWriter(new FileWriter(isim))); 
 s+="class "+dosyaismi+" extends f_xj\n";
 s+="{\n";
 s+="public double func(double w[])\n";
 s+="{//function to be solved\n";
 s+="int n=w.length;\n";
 s+="double x=w[0];\n";
 s+="double y=0;\n";
 s+="if(n>1) y=w[1];\n";		
 s+="double ff;\n";
 s+="ff="+fonksiyon1+";\n";
 s+="return ff;\n";
 s+="}\n";
 s+="}\n";
 sinif=s;
 //System.out.println(s);
 fout.println(s);
 fout.close();
 }
 catch(IOException e1) {System.err.println("girdi hatası.");} 
 catch(NumberFormatException e2){};
 JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
 int rc = javac.run(null, null, null,isim);
 return fonksiyon;
}

 protected void finalize() throws Throwable
 {super.finalize();}}

