import java.awt.*; import java.applet.*; public class BeanJar extends Applet { // Value of PI private static final double PI = 3.1416; // Volume of one bean private static final double VOLUME_OF_ONE_BEAN = (PI * 0.125 * 0.125 * 0.15) + (0.25 * 0.25 * 0.15); // Our Ad Label advertisement = new Label ("How many coffee beans can fit in a jar"); // Prompt for the user Label prompt = new Label ("Enter the jar length and width (whole inches)"); // Field of entering the height TextField heightField = new TextField (10); // Field of entering the width TextField widthField = new TextField (10); // Calculate button Button calculateButton = new Button ("Calulate"); // Area for results TextArea resultSection = new TextArea (2, 25); // Initialize my applet public void init () { // Add all the GUI items add (advertisement); add (prompt); add (heightField); add (widthField); add (calculateButton); add (resultSection); } public boolean action (Event event, Object what) { try { // Get the height String heightString = heightField.getText (); int height = Integer.parseInt (heightString); // Get the width String widthString = widthField.getText (); int width = Integer.parseInt (widthString); // Calculate the volume of the jar double jarVolume = PI * width * height; // Calculate the number of beans long numberOfBeans = Math.round (jarVolume / VOLUME_OF_ONE_BEAN * 0.6); // Make a result string String result = "The jar will hold \n" + numberOfBeans + " coffee beans"; // Set the results resultSection.setText (result); } catch (Exception exception) { // // In case of exceptions // String result = "Cannot understand input"; resultSection.setText (result); } return true; } }