import java.applet.*;
import java.awt.*;
import java.lang.Integer;

public class Operator extends Applet
{
    final int BIG = 24;
    final int FONTSIZE = 18;
    final int OFFSET = FONTSIZE + 2;
    final int BASE = 100;
    int PLUS = 165;
    int EXP1 = 215;
    int DIV = 190;
    int MINUS = 250;
    int TIMES1 = 296;
    int TIMES2 = 425;
    int EXP2 = 333;
    int LINE = 50;
    int SQRT = 360;

    Font fnorm = new Font("TimesRoman", Font.PLAIN, FONTSIZE);
    FontMetrics fm = getFontMetrics(fnorm);

    Font f1 = new Font("TimesRoman", Font.PLAIN, FONTSIZE - 4);
    Font f2 = new Font("TimesRoman", Font.PLAIN, FONTSIZE - 7);
    Font f3 = new Font("TimesRoman", Font.PLAIN, BIG);

    String front[] = new String[12];
    String click[] = new String[12];
    String back[] = new String[12];
    String e1, e2, e3, e4;
    String r1, r2, r3;
    String msg, msg2;
    String choice, first, second, pick1, pick2;
    int a, b;

    int counter;
    int Cnt;
    int whichpath;
    boolean valid=true;

//**************************************************
 
    public void init()
    {
	counter=0;
        Cnt = 0;
	choice = first = second = pick1 = pick2 = "";
	setBackground(Color.white);
	pick_op();
	repaint();

    }

//*********************************************************
    public boolean mouseDown(Event e, int x, int y)
    {   a=x; b=y;
	if ((LINE-15 <= y) && (y <= LINE)) {
		if ((PLUS <= x) && (x <= PLUS+15)) {
		 // mouse is on the + sign
		 choice = "+"; }
		else if ((EXP1 <= x) && (x <= EXP1+25)) {
		// mouse is on the ** sign
		choice = "**1"; }
		else if ((DIV <= x) && (x <= DIV+12)) {
		// mouse is on the / sign
		choice = "/"; }
		else if ((MINUS <= x) && (x <= MINUS+15)) {
		// mouse is on the - sign
		choice = "-"; }
		else if ((TIMES1 <= x) && (x <= TIMES1+17)) {
		// mouse is on the first * sign
		choice = "*1"; }
		else if ((TIMES2 <= x) && (x <= TIMES2+17)) {
		// mouse is on the second * sign
		choice = "*2"; }
		else if ((EXP2 <= x) && (x <= EXP2+24)) {
		// mouse is on the second ** sign
		choice = "**2"; }
		else if ((SQRT <= x) && (x <= SQRT+45)) {
		// mouse is on the SQRT function
		choice = "sqrt"; }
		else {
		// mouse not on an operator
		choice = ""; }
	} // mouse is on the equation
			counter++;
			pick_op();
			repaint();
			return true;
    }

//**********************************************************
 
    public void restart()
    {
	valid = true;
	counter = 0;
	Cnt = 0;
	LINE = 50;
	choice = first = second = pick1 = pick2 = "";
	e1 = e2 = e3 = e4 = r1 = r2 = r3 = "";
	msg = "";
	msg2 = "";
	PLUS = 165;
        EXP1 = 215;
        DIV = 190;
        MINUS = 250;
        TIMES1 = 296;
        TIMES2 = 425;
        EXP2 = 333;
        LINE = 50;
        SQRT = 360;

    }

//*************************************************************
        
    public void pick_op()
    {
	switch(counter)
	{
	  case 0:
		restart();
		// go down and get started
		break;
	  case 1:
		// go down and print intro
		break;
	  case 2:
		if ((choice != "*1") && (choice != "*2")) {
			counter--;  valid = false;	
                msg = "Invalid operator sequence.  Select an operator.";
		}
		else {
		  first = choice;
		  if (choice == "*1") {
			second = "*2";
			msg = "Multiply 1.5 and the value stored in C.";
			e1 = "= A + 5 / B ** 2 - ";
			r1 = "(1.5 * C)";
			e2 = " ** SQRT(B * 4.0)";
			r2 = "= A + 5 / B ** 2 - 4.5 ** SQRT(B * 4.0)";
			e3 = e4 = r3 = "";
			valid = true;
			// redefine x-coords for ops **2 and *2
			EXP2 = 290;
			TIMES2 = 382;  
			SQRT = 317;
			// take TIMES1 out
			TIMES1 = 1000;
		  } 
		  else  {  // choice == "*2"
			second = "*1";
			msg = "Multiply the value stored in B and 4.0.";
			e1 = "= A + 5 / B ** 2 - (1.5 * C) ** SQRT";
			r1 = "(B * 4.0)";
			r2 = "= A + 5 / B ** 2 - (1.5 * C) ** SQRT(16.0)";
			e2 = e3 = e4 = r3 = "";
			valid = true;
			// take TIMES2 out
			TIMES2 = 1000;
		  }
		}		
                break;

	  case 3:	
		if (second == "*1") {
		// we still have 2 more possible choices
		   if ((choice != "*1") && (choice != "sqrt")) {
			counter--;  valid = false;
		   msg = "Invalid operator sequence.  Select an operator.";
		   }
		   else {
		      pick1 = choice;
		      if (choice == "*1") {
		           pick2 = "sqrt";
			   msg = "Multiply 1.5 and the value stored in C.";
			   e1 = "= A + 5 / B ** 2 - ";
			   r1 = "(1.5 * C)";
			   e2 = " ** SQRT(16.0)";
			   r2 = "= A + 5 / B ** 2 - 4.5 ** SQRT(16.0)";
			   e3 = e4 = r3 = ""; 
			   valid = true;
			   // redefine x-coords for ops **2 and sqrt 
			   EXP2 = 290; 
			   SQRT = 317; 
			   // take TIMES1 out
			   TIMES1 = 1000; }
		      else {  // choice == "sqrt"
		           pick2 = "*1";
			   msg = "Evaluate the square root of 16.0.";
			   e1 = "= A + 5 / B ** 2 - (1.5 * C) ** ";
			   r1 = "SQRT(16.0)";
			   r2 = "= A + 5 / B ** 2 - (1.5 * C) ** 4.0";
			   e2 = e3 = e4 = r3 = ""; 
			   valid = true; 
			   // take SQRT out
			   SQRT = 1000; } 
		    }
		}
		else { // second == "*2" - only 1 choice here
		   if (choice != "*2") {
			counter--;  valid = false;
		   msg = "Invalid operator sequence.  Select an operator.";
		   }
		   else {
		       pick1 = pick2 = "";
		       msg = "Multiply the value stored in B and 4.0.";
		       e1 = "= A + 5 / B ** 2 - 4.5 ** SQRT";
		       r1 = "(B * 4.0)";
		       r2 = "= A + 5 / B ** 2 - 4.5 ** SQRT(16.0)";
		       e2 = e3 = e4 = r3 = "";
		       valid = true; 
		       // take TIMES2 out
		       TIMES2 = 1000;
		   }
		}
                break;

	  case 4:
		if ((pick2 == "sqrt") || (pick2 == "")) {
		// now we must choose the SQRT function
		   if (choice != "sqrt") {
			counter--;  valid = false;
			msg = "Invalid operator sequence.  Select an operator.";
		    }
		    else {
			msg = "Evaluate the square root of 16.0.";
			e1 = "= A + 5 / B ** 2 - 4.5 ** ";
			r1 = "SQRT(16.0)";
			r2 = "= A + 5 / B ** 2 - 4.5 ** 4.0";
			e2 = e3 = e4 = r3 = "";
			valid = true;
			// take SQRT out
			SQRT = 1000;
		    }
		}
		else if (pick2 == "*1") {
		// now we must choose the *1 operator
		   if (choice != "*1") {
			counter--;  valid = false;
		        msg = "Invalid operator sequence.  Select an operator.";
		   }
		   else {
			msg = "Multiply the value stored in C and 1.5.";
			e1 = "= A + 5 / B ** 2 - ";
			r1 = "(1.5 * C)";
			e2 = " ** 4.0";
			r2 = "= A + 5 / B ** 2 - 4.5 ** 4.0";
			e3 = e4 = r3 = "";
			valid = true;
			// adjust the x-coord for the **2 operator
			EXP2 = 290;
			// take TIMES1 out
			TIMES1 = 1000;
		   }
		} 		
		break;

          case 5:
                if ((choice != "**1") && (choice != "**2")) {
			counter--;  valid = false;
			msg = "Invalid operator sequence.  Select an operator."; 
                }
		else {
		    first = choice;
		    if (choice == "**1") {
			second = "**2";
			msg = "Evaluate the value in B to the power 2.";
			e1 = "= A + 5 / ";
			r1 = "B ** 2";
			e2 = " - 4.5 ** 4.0";
			r2 = "= A + 5 / 16.0 - 4.5 ** 4.0";
			e3 = e4 = r3 = "";
			valid = true;
			// redefine x-coords for ops - and **2
			MINUS = 235;
			EXP2 = 275;
			// take EXP1 out
			EXP1 = 1000;
		     }
		     else { // choice == "**2"
			second = "**1";
			msg = "Evaluate 4.5 to the power 4.0."; 
			e1 = "= A + 5 / B ** 2 - ";
			r1 = "4.5 ** 4.0";
			r2 = "= A + 5 / B ** 2 - 410.062";
			e2 = e3 = e4 = r3 = "";
			valid = true;
			// take EXP2 out
			EXP2 = 1000;
		    }
		}	
		break;
     
          case 6:
                if (second == "**2") {
		// now take care of the second exponentiation
		   if (choice != "**2") {
			counter--; valid = false;
			msg = "Invalid operator sequence.  Select an operator.";
                    }
		    else {
			msg = "Evaluate 4.5 to the power 4.0.";
			e1 = "= A + 5 / 16.0 - ";
			r1 = "4.5 ** 4.0";
			r2 = "= A + 5 / 16.0 - 410.062";
			e2 = e3 = e4 = r3 = "";
			valid = true;
			// take EXP2 out
			EXP2 = 1000;
		    }
		}
		else { // second == "**1"
		// now take care of the first exponentiation
		   if (choice != "**1") {
			counter--; valid = false;
			msg = "Invalid operator sequence.  Select an operator.";
		   }
		   else {
			msg = "Evaluate the value in B to the power 2.";
			e1 = "= A + 5 / ";
			r1 = "B ** 2";
			e2 = " - 410.062";
			r2 = "= A + 5 / 16.0 - 410.062";
			e3 = e4 = r3 = "";
			// redefine x-coord for - op
			MINUS = 234;
			// take EXP1 out
			EXP1 = 1000;
		   }
		}
		break;
     
          case 7:
		// next is division operator
                if (choice != "/") {
			counter--; valid = false;
			msg = "Invalid operator sequence.  Select an operator.";
                }
		else {
		   msg = "Divide 5 by 16.0.";
		   e1 = "= A + ";
		   r1 = "5 / 16.0";
		   e2 = " - 410.062";
		   r2 = "= A + 0.31250 - 410.062";
		   valid = true;
		   e3 = e4 = r3 = "";
		   // take / out
		   DIV = 1000;
		   // redefine x-coord for - op
		   MINUS = 239;  // not a big shift, huh?
		}
		break;
     
          case 8:
                // next is addition operator
		if (choice != "+") {
			counter--; valid = false;
			msg = "Invalid operator sequence.  Select an operator.";
                }
		else {
		   msg = "Add the value in A and 0.31250.";
		   e1 = "= ";
		   r1 = "A + 0.31250";
		   e2 = " - 410.062";
		   r2 = "= 250.31250 - 410.062";
		   e3 = e4 = r3 = "";
		   valid = true;
		   // take + out
		   PLUS = 1000;
		   // redefine x-coord for - op
		   MINUS = 224;
                } 
		break;
     
          case 9:
                // next is subtraction operator
		if (choice != "-") {
			counter--; valid = false;
			msg = "Invalid operator sequence.  Select an operator.";
                }
		else {
		   valid = true;
		   msg = "Subtract the value 410.062 from 250.31250.";
		   msg2 = "Value -160.062 is stored in variable ANSWER.";
		   e1 = "= ";
		   r1 = "250.31250 - 410.062";
		   r2 = "= -160.062";
		   e2 = e3 = e4 = r3 = "";
		   // take - out
		   PLUS = 1000;
		}
		break;
     
	}	
    }

//*************************************************************

    public void drawLines(Graphics g)
    {
	for (int i=0; i<Cnt; i++) {
	   g.drawString(front[i], (BASE/2)+83, 50+(i*25));
	   g.setColor(Color.red);
	   g.drawString(click[i], (BASE/2)+fm.stringWidth(front[i])+83,
				50+(i*25));
	   g.setColor(Color.black);
	   if (back[i] != "")
		g.drawString(back[i], (BASE/2)+fm.stringWidth(front[i])
			+fm.stringWidth(click[i])+83, 50+(i*25));
	}
    }

//*************************************************************
        
    public void origformula(Graphics g)
    {
	g.drawString("ANSWER = A + 5 / B ** 2 - (1.5 * C) ** SQRT(B * 4.0)",
		BASE/2, 50);

    }

//*************************************************************
    public void paint(Graphics g)
    {
	g.setFont(fnorm);
	
//each one of the colors started after stringWidth() of other

	origformula(g);

	if (counter == 0) {
	   g.drawString("Welcome to an interactive operator" ,
			BASE, 150);
	   g.drawString("precedence example in FORTRAN.", BASE, 175);
	   g.setColor(Color.red);
	   g.drawString("Please click your left mouse button to get started.",
			BASE/2, 300);
	   g.setColor(Color.black);
	} 
	else if (counter == 1) {
	   g.drawString("Here is a sample arithmetic expression in FORTRAN.",
			BASE/2, 200);
	   g.setColor(Color.red);
	   g.drawString("Click on the appropriate operator in the correct order",
			BASE/2, 275);
	   g.drawString("to evaluate the expression.",BASE/2,300);
	   g.setColor(Color.black);
	}
	// draw expression
	else if (valid) { 
	   g.drawString(e1, (BASE/2)+83, LINE);
	   g.setColor(Color.red);
	   g.drawString(r1, (BASE/2)+fm.stringWidth(e1)+83, LINE); 
	   g.setColor(Color.black);
	   if (e2 != "") {
		g.drawString(e2, (BASE/2)+fm.stringWidth(e1)+
		fm.stringWidth(r1)+83, LINE); 
	   }
	   g.drawString(r2, (BASE/2)+83, LINE+25);
	   front[Cnt] = e1; 
	   click[Cnt] = r1;
	   back[Cnt] = e2;
	   Cnt++;
	   LINE += 25;
	   
        }
	else if (!valid) {
	   g.drawString(r2, (BASE/2)+83, LINE);
	}
	   g.drawString(msg, BASE/2, 325);
	   if (msg2 != "") {
		g.drawString(msg2, BASE/2, 350);
		msg2 = "";
	   }
	// draw output in either case
	drawLines(g);
        if (counter == 9) {
           g.setColor(Color.blue);
           g.drawString("ANSWER = -160.062", BASE/2,LINE+25);
	   g.setColor(Color.red);
           g.drawString("Click your left mouse button to restart.",
                BASE/2, 390);
           g.setColor(Color.black);
           restart();
        }


     }
   
}


