//using if statements, relational
//operators, and equality operators
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class Poly2RootApplet extends Applet implements ActionListener
{
//polynomial a*x^2+b*x+c=0
    Label prompt1;     //prompt Roots of a*x^2+b*x+c = 0 
    Label prompta;     //prompt a :
    TextField inputa;  //input a value here
    Label promptb;     //prompt b :
    TextField inputb;  //input b value here
    Label promptc;     //prompt c :
    TextField inputc;  //input c value here
    double a,b,c;      //store a,b,c values
    double delta=0.0;  //store determinate, delta values 
    double x1,x2;      //store roots
    //setup the graphical user interface components
    //and initilise variables
    public void init()
    {
       prompt1=new Label("Roots of a*x^2 + b*x + c = 0                  ");
       prompta=new Label("a :      ");
       inputa =new TextField(40);
       promptb=new Label("b :      ");
       inputb =new TextField(40);
       promptc=new Label("c :      ");
       inputc =new TextField(40);
       inputc.addActionListener(this);
       add(prompt1); //put prompts and inputs on applet
       add(prompta);
       add(inputa);
       add(promptb);
       add(inputb);       
       add(promptc);
       add(inputc);
    }

    // display the results
    public void paint(Graphics g)
    {
       g.drawString("The roots of the polynomial are : ",70,175);
       if(delta<0)
         {
         g.drawString("Root 1 :"+(-b/2.0/a)+"+ i* "+Math.sqrt(-delta)/2.0/a,100,190 );
         g.drawString("Root 2 :"+(-b/2.0/a)+"- i* "+Math.sqrt(-delta)/2.0/a,100,205);         
         }
       if(delta==0)
         {
         g.drawString("Root 1 :"+(-b/2.0/a),100,190 );
         g.drawString("Root 2 :"+(-b/2.0/a),100,205 );
         }
       if(delta>0)
         {
         x1=-b/2.0/a+Math.sqrt(delta)/20/a;
         x2=-b/2.0/a-Math.sqrt(delta)/2.0/a;
         g.drawString("Root 1 :"+x1,100,190 );
         g.drawString("Root 2 :"+x2,100,205);         
         }
    }

    //process user's action on the inputc text field
    public void actionPerformed( ActionEvent e)
    {
         Double sayi1=new Double(inputa.getText() );
         Double sayi2=new Double(inputa.getText() );
         Double sayi3=new Double(inputa.getText() );
         a=sayi1.doubleValue();
         b=sayi2.doubleValue();
         c=sayi3.doubleValue();
         delta=b*b-4.0*a*c;
         repaint();
    }
}
