import awb.*; import java.awt.*; public class Recurse extends java.applet.Applet { TextField gMake, gStyle, gColor, gOwner, mInstruct; IntField gN; DoubleField gX; Label lN, lX; Button bFact, bExp; TextField mResults; int k, n; double x; public void init() { lN = new Label("N"); lX = new Label("X"); mInstruct = new TextField(60); mInstruct.setText("Enter N and X, then press button for function"); gN = new IntField(10); gX = new DoubleField(10); bFact = new Button("Factorial"); bExp = new Button("Exponential"); mResults = new TextField(60); add(mInstruct); add(lN); add(gN); add(lX); add(gX); add(bFact); add(bExp); add(mResults); } public boolean action(Event e, Object arg) { int yea; String mak, sty, col, own; if (e.target == bFact) { n = gN.getInt(); x = gX.getDouble(); mResults.setText(n+" factorial = "+fact(n)); return true; } if (e.target == bExp) { n = gN.getInt(); x = gX.getDouble(); mResults.setText(x+" to the "+n+" power = "+expon(x, n)); return true; } return false; } int fact(int n) { if (n<=1) { return 1; } return n * fact(n-1); } double expon(double x, int n) { double xnot; if (n == 0) { return 1.0; } xnot = expon(x, n/2); if ( n == 2*(n/2)) { return xnot * xnot; } else { return x * xnot * xnot; } } }