//CIS  790               L.A.T.T.C                PROF. AUCIELLO
// Programmed by Joseph Auciello, CIS Professor, LA Trade Technical College.
// amath5.java
/* 731


sin of 90 deg = 1
cos of 90     = 0
degrees       = .017453 * radians

Topics:
        .      Overall Applet Structure.
        .      Text to String to Double (wow!)
               ( Converting a string to a double precision number. )
        .      String to Integer = Integer.valueOf(string)
        .      "int"     = name of data type holding 4 bytes.
        .      "Integer" = name of a Class that contains math methods.
	.      "class": template or blueprint from which an
                        object is made.
                        "an abstraction of a group of entities."
                        (generalizes)

        .      "instantiation": 
                       act of creating an instance;
                       instance means 'subset' of class;
                       act of creating an object from a class.

        .      casting -- high speed data type conversions.
        .      trignometric functions -- sin, cos, tan.
               (sine of pi radians  = 0)
               discuss degrees and radians.
        .      arithmetic operations.
        .      data precision -- limits of float type.

*/
//  (1) links to libraries go 1st!
import corejava.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.Font;
import java.awt.Color;
import java.lang.Double;
import java.lang.Integer;
import java.lang.Math;
import java.lang.String;
import java.net.*;
import java.util.*;
import java.util.Random;

// (2) this starts your program (main routine)
//      Note the "{"  (main bracket) immediately follows.
public class amath5 extends Applet
{  TextField inputField1, inputField2, inputField3, 
             inputField4, inputField5, inputField6,
             inputField7, inputField8;

   Button button1, button2;                           // ...... button.
   int i = 1;                                // define int and
   double d3a, d3s, d3m, d3d;                // double types.
   double d1, d2, d3, d7, d8;
   double d11;
   long l5, l6;
   String sx;
   Random r = new Random();
// (3) in the above section, all "global" variables are defined.

// (4) the start() method is run every time the applet program runs.
   public void start()
     { Label namelabel1 = new Label("Enter Operand 1 (d1):",Label.RIGHT);
       add (namelabel1);                     // def and add label.
       inputField1      = new TextField(5); // create, add, and
       add (inputField1);                    // initialize input field1.
       inputField1.setText("1.414");


       Label namelabel2 = new Label(" Operand 2 (d2):",Label.RIGHT);
       add (namelabel2);                     // def and add label.
       inputField2      = new TextField(5);  // create, add, and
       add (inputField2);                    // initialize input field1.
       inputField2.setText("2");

       Label namelabel3 = new Label("   Operand 3 (d3):",Label.RIGHT);
       add (namelabel3);                     // def and add label.
       inputField3      = new TextField(5);  // create, add, and
       add (inputField3);                    // initialize input field1.
       inputField3.setText("2");


       Label namelabel7 = new Label("Angle(deg.)(d7):",Label.RIGHT);
       add (namelabel7);                     // def and add label.
       inputField7      = new TextField(5);  // create, add, and
       add (inputField7);                    // initialize input field1.
       inputField7.setText("90");

       Label namelabel8 = new Label("Angle(radians)(d8):",Label.RIGHT);
       add (namelabel8);                     // def and add label.
       inputField8      = new TextField(7);  // create, add, and
       add (inputField8);                    // initialize input field1.
       inputField8.setText("1.5708");

       button1          = new Button("Enter Numbers");  // def button.
       add(button1);

       button2          = new Button("Random Numbers");  // def button.
       add(button2);
//     the above section (start() method) sets up the labels, textfields
//     and buttons.
     } // end init()

// (5) the paint() method does calculations, and output.
   public void paint(Graphics g)
     { Font fonts1   = new Font("Helvetica",Font.BOLD,16);
       g.setFont(fonts1);
       g.setColor(new Color(240,25,25));             // base color
       g.drawString("CIS 790     JAVA PROGRAMMING      PROF. AUCIELLO",20, 315);
       g.drawString("Press 'Random' or Enter Input", 20,365);
       Font fonts2   = new Font("Helvetica",Font.BOLD,12);
       g.setFont(fonts2);
       g.setColor(new Color(25,25,245));  // base color
// the above section sets up Fonts, Colors and Titles.

// (6) all of the work is done in the "try" section.
//     Note that it is a block of code, so is the "catch" section.
//     This allows for errors to be "handled" internally by the
//     program, so that it does not "crash" and return to the
//     operating system.

       try
          { double d3a   =  d1 + d2;
            double d3s   =  d1 - d2;
            double d3m   =  d1 * d2;
            double d3d   =  d1 / d2;
            //     d3d   =  i1 / i2;                // triggers error trap.
      
            double d41   = Math.pow(d1,d2);         // power function.
/*
            double d42   = d41 + .005;      // legacy rounding method.
            double d43   = d42 * 100;
            int    i43   = (int)d43;
            double d44   = (double)i43/100;
*/
            
// illustrates "rounding" function (to 2 dec. places).

            double d3a1  = (double)(int)(100*(d3a+.005))/100;  
            double d3s1  = (double)(int)(100*(d3s+.005))/100;
            double d3m1  = (double)(int)(100*(d3m+.005))/100;
            double d3d1  = (double)(int)(100*(d3d+.005))/100;

            double d45   = (double)(int)(100*(d41+.005))/100;

            double d71   = Math.sqrt(d3);
            double d72   = (double)(int)(100*(d71+.005))/100;

// trignometry section.
            //             .017543293
            double d73   = Math.sin(d7*0.017453);  // input in radians.
            double d74   = (double)(int)(100*(d73+.005))/100;
            //     rad   = constant    * deg
            double d74r  = 0.0174536 * d7;          // degrees to radians.
                   d74r  = (double)(int)(10000*(d74r+.00005))/10000;

            double d83   = (d8/0.017453);  // here <-----
            double d84d  = (double)(int)(100*(d83+.005))/100;

            double dc1   = Math.cos(d7*0.017543294);
            double dc2   = (double)(int)(100*(dc1+.005))/100;

            double dt1   = Math.tan(d7*0.017543294);
            double dt2   = (double)(int)(100*(dt1+.005))/100;
            
// (6.2)  output to screen.
            // g.drawString(d1,20,10);
            g.drawString("( "+d84d+") degrees  = ", 100,70);
            g.drawString("( "+d74r+") radians", 288,70);
            g.drawString(" d1            d2  =  result (rounded to 2 dec. places)",66,100);
            g.drawString(" d1 = " + d1, 525,100);
            g.drawString(" d2 = " + d2, 525,114);
            g.drawString(" d3 = " + d3, 525,128);
            g.drawString(" d7 = " + d7, 525,142);
            g.drawString(" d8 = " + d8, 525,156);

            g.drawString(d1 + "  +  " + d2 + "  =  " + d3a1,66,115);
            g.drawString(d1 + "  -  " + d2 + "  =  " + d3s1,66,129);
            g.drawString("      Make d2 > d1.  Note negative result.", 188,129);
            g.drawString(d1 + "  *  " + d2 + "  =  " + d3m1,66,144);
            g.drawString(d1 + "  /  " + d2 + "  =  " + d3d1,66,158);
            g.drawString("      Make d2 a 0.   Note result. ", 188,144);
            g.drawString("   Math.pow("+d1+",   "+d2+") =  " + d45, 0,174);
            g.drawString("      Math.sqrt("+d3+") = " + d72, 228,174);
            g.drawString("     " + d1 + " raised to the "+d2+" power = "+d45,188,186);
            g.drawString("      What is any number to the 0 power? To the 1st power?",188,200);

            g.drawString("      The sine of 'pi' radians = 0.",188,228);
            g.drawString("Sine of 'pi'/2 radians = 1.", 378,228);      
            g.drawString("      The sine of 90 degrees = 1.     Sine of 30 deg = .5",188,242);
            g.drawString("Math.sin(" + d7 + ") =" + d74,20,242);
            g.drawString("Math.cos(" + d7 + ") =" + dc2,20,256);
            g.drawString("      The cosine of 90 degrees = 0. ",188,256);
            g.drawString("Math.tan(" + d7 + ") =" + dt2,20,270); 
          }  // ends "try" block.

// (6.3) this block "catches" any program errors.
         catch (Exception e)                                    
           { String err = e.toString();
             g.drawString("In Error-Trapping Routine. ",180,265);
             g.drawString("" + err,180,277);
             g.drawString("Check for Divide by 0 Error",180,299);
           } // catch
     }  // end paint()

// (7) this section "listens" when the "enter button" is
//     is clicked.
//     If 'Random' is clicked, random numbers are generated.

       public boolean action(Event event, Object arg)
       {   if (arg == "Random Numbers")
             {  sx = "Clicked Random Numbers";
                d1 = Math.abs(r.nextInt()%100+1);
                d2 = Math.abs(r.nextInt()%10+1);
                d3 = Math.abs(r.nextInt()%10+1);
                d7 = Math.abs(r.nextInt()%90+1);
                d8 = Math.abs(r.nextInt()%6+1);
                repaint();
                return true;
             }
//      If "Enter Numbers" is clicked, numbers come from text input.
           else  if (arg == "Enter Numbers")
                 {  sx = "Clicked Enter Numbers";
                    // get input text.
                    String s1    = inputField1.getText();
                           d1    = Double.valueOf(s1).doubleValue();
                    String s2    = inputField2.getText();   // get input as string.
                           d2    = Double.valueOf(s2).doubleValue();
                    String s3    = inputField3.getText();   // get input as string.
                           d3    = Double.valueOf(s3).doubleValue();
                    String s7    = inputField7.getText();   // get input as string.
                           d7    = Double.valueOf(s7).doubleValue();
                    String s8    = inputField8.getText();   // get input as string.
                           d8    = Double.valueOf(s8).doubleValue();
                    
                    repaint();
                    return true; 
                 }
               else
                  return false;

     }  // end action.  
}  //  end class

//  ===================== END   OF   PROGRAM  ===========================

// (8) Note the overall bracketing of the applet, with each
//     method "bracketed".


/*   

Activities:
     Work with "exponentiation" function including
     raising to the .5 (which is square root) power.
     Test it by using d3 which inputs to the sqrt function.

     Raise numbers to the 0 power, and the 1st power.

     Test add, sub, mult, and divide operations.
     Divide by 0 to test results.
     Work with negative numbers and mixed signs operations.
     Test neg * neg numbers, neg * pos numbers.

     Look up the definition of sine, cosine.
     Test sine of 0, 30 and 90 degrees.
     Understand that input to trig functions is in radians.
     Test degree to radian conversion.

     Note accuracy limits.  Note inaccuracy past a certain
     number of decimal places.

     Note use of 1.5708 to represent PI / 2.


Programming Assignment:
     Add to this applet the following operations:

     Display the average of  d1, d2, d3.

     Find the greater and lesser or d1, d2, using the min and 
     max functions.

     Display asin, acos, and atan.  (inverse of sin, cos, tan).


Questions:     
     Explain how data is inputted to this applet.

     What is a global variable?

     What is a local  variable?

     Where do global variables go?

     What is a radian?

     Explain how numbers are rounded off.

*/