import awb.*; import java.awt.*; import java.awt.event.*; public class Fence extends java.applet.Applet implements ActionListener { TextField mInstruct; Label lLength; DoubleField gLength; Button bSimulate, bDisplay; TextArea mResults; int k; public void init() { lLength = new Label("Length"); mInstruct = new TextField(70); mInstruct.setText("Enter length of fence, the press Simulate or Display"); gLength = new DoubleField(10); bSimulate = new Button("Simulate"); bDisplay = new Button("Display"); mResults = new TextArea(25,60); bSimulate.addActionListener(this); bDisplay.addActionListener(this); add(mInstruct); add(lLength); add(gLength); add(bSimulate); add(bDisplay); add(mResults); } public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); double fenceLength; if (cause == bSimulate) { fenceLength = gLength.getDouble(); fenceTable(fenceLength); } if (cause == bDisplay) { fenceLength = gLength.getDouble(); fencePlot(fenceLength); } } void fenceTable(double fenceLength) { double area, x, y; x = 0.0; y = fenceLength - 2.0 * x; mResults.setText("Fence Optimization Table\n"); while (y >= 0.0) { area = x * y; mResults.append("x = "+x+" y = "+y+" area = "+area+"\n"); x = x + 1.0; y = fenceLength - 2.0 * x; } } void fencePlot(double fenceLength) { double area, x, y; x = 0.0; y = fenceLength - 2.0 * x; mResults.setText("Fence Optimization Plot\n"); while (y >= 0.0) { area = x * y; mResults.append(x+"\t"+plotString(area)+"\n"); x = x + 1.0; y = fenceLength - 2.0 * x; } } String plotString(double area) { String s = ""; while (area > 0) { s = s + "*"; area = area - 5.0; } return s; } }