1 // Applet to calculate sphere volumes 2 3 import java.awt.*; 4 import java.awt.event.*; 5 6 public class VolumeApplet extends java.applet.Applet 7 implements ActionListener 8 { 9 int radius; 10 double volume; 11 Label prompt1,prompt2; 12 TextField input, output; 13 14 public void init() 15 { 16 prompt1 = new Label("Enter the radius of a sphere: "); 17 add(prompt1); 18 19 input = new TextField(10); 20 input.addActionListener(this); 21 add(input); 22 23 prompt2 = new Label("The volume of the sphere is: "); 24 add(prompt2); 25 26 output = new TextField(30); 27 add(output); 28 } 29 30 31 public void actionPerformed (ActionEvent e) 32 { 33 Double vol; 34 35 radius = Integer.parseInt (input.getText() ); 36 37 volume = (4.0/3.0) * Math.PI * Math.pow(radius,3); 38 vol = new Double(volume); 39 output.setText(vol.toString()); 40 } 41 42 } 43 44 45