// Compute e**x
import java.awt.*;
import java.applet.Applet;
public class methods extends Applet {
Label prompt; // prompt user to input x value
TextField input; // input x value here
double x = 0.0;
double numerator = 1.0;
public void init(){
prompt = new Label( "Enter an integer and press Enter" );
input = new TextField( 10 );
add( prompt ); // put prompt on applet
add( input ); // put input on applet
}
// process user's action on the input text field
public boolean action( Event event, Object o ){
x = (double) Integer.parseInt( input.getText() );
input.setText("");
repaint();
return true; // indicates user's action was processed
}
public void paint( Graphics g ){
double e2x = 1.0; // e2x holds the estimate of e**x
numerator = 1.0;
int yPos = 85;
g.drawString( "0 terms: " + e2x, 100, yPos );
for ( int i = 1; i <= 15; i++ ){
numerator *= x;
e2x += numerator / fact(i);
g.drawString(i + " terms: " + e2x, 100, yPos += 15 );
}
}
public long fact( int n){
long fact = 1;
if ( n < 0 ) return 0;
for (int i = 2; i <= n; i++ )
fact *= i;
return fact;
}
}