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