This Recursion example uses a List Box and a TextField. Enter the value for the size ( no larger than 4 is best..). The output is in the List Box. See the working Applet below.
import java.applet.Applet; import java.awt.*;
public class RecOutList extends Applet {
TextField sizeTxt; List output; int size;
public void init() { sizeTxt = new TextField(); add(sizeTxt); output = new List(10); add(output); }
public void Moves(int s) { output.addItem("begin"); if (s<2) output.addItem("base "+s); else { Moves(s-1); output.addItem("mid "+s); Moves(s-2); output.addItem("done "+s); } }
public boolean action(Event e,Object o) { size = Integer.parseInt(sizeTxt.getText()); Moves(size); return true; } }
|