//count mileage and gallon
import java.awt.*;       //import java.awt package
import java.applet.Applet;public class Mileage extends Applet{
   Label prompt1;       //prompt user to input mileage
   TextField input1;    //input mileage 
   Label prompt2;       //prompt user to input gallon
   TextField input2;    //input gallon
   int counter;
   int mileage,gallon;
   double sum;
//setup the graphical interface components
//and initialize variables   public void init ( )
   {
      prompt1 = new Label ( "Enter mileage" );
      input1 = new TextField ( 10 );
      prompt2 = new Label ( "Enter gallon and press Enter" );
      input2 = new TextField (10 );
      add ( prompt1 );     //put prompt1 on applet
      add ( input1 );     //put input1 on applet
      add ( prompt2 );     //put prompt2 on applet
      add ( input2 );      //put input2 on applet
      sum = 0;
      counter = 0;
   }
//display the results
   public void paint ( Graphics g )
   {
      g.drawString ( "Enter 10000 to end!" ,40,120);
      while ( mileage != 10000 || gallon != 10000 )
      {
         sum += ( double ) mileage / gallon;
         counter += 1;
      }
      sum = ( double ) sum / counter;
      g.drawString ( "An mileage An gallon  " + sum,40,150);
   }
//process user's action on the input2 text field
   public boolean action ( Event event, Object o )
   {
      if ( event.target == input2 ){
         mileage = Integer.parseInt ( input1.getText () );
         gallon = Integer.parseInt ( input2.getText () );
         repaint ();
      }
      return true;       //indicates user's action was processed
   }
}