The program runs but it doesn't calculate the average MPG correctly. It is calculating something, instead of zeroing out like earlier, but it sure isn't MPG! :mad: Any suggestions???
Iamnew2java 0 Newbie Poster
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MPG extends Applet implements ActionListener
{
Label First; //First line
Label Second; //second line
Label IMiles; //label for miles to be entered
Label IGallons; //label for gallons to be entered
TextField EMiles; //input miles
TextField EGallons; //enter gallons
Label Outp; //Output trip label
Label OutpAgain; //Output average label
float Miles; //miles for calculations
float Gallons; //gallons for calculations
float MPG;
float AMiles;
float AGallons;
float AMPG;
Panel pan1, pan2, pan3, pan4, pan5, pan6;
//setup the graphical user interface components
//and initialize variables
public void init()
{
setLayout(new GridLayout(6,1));
pan1=new Panel();
pan2=new Panel();
pan3=new Panel();
pan4=new Panel();
pan5=new Panel();
pan6=new Panel();
First = new Label ("Here is the output of my program:");
Second = new Label ("Enter information and press enter");
IMiles = new Label ("Enter Miles");
IGallons = new Label ("Enter Gallons");
EMiles = new TextField (10);
EGallons = new TextField(10);
Outp = new Label("MGP this tank is ");
OutpAgain = new Label ("TOTAL MPG is ");
Miles = 0;
Gallons = 0;
MPG = 0;
AMiles = 0;
AGallons = 0;
AMPG = 0;
EMiles.addActionListener(this);
EGallons.addActionListener(this);
pan1.add(First);
pan2.add(Second);
pan3.add(IMiles);
pan3.add(EMiles);
pan4.add(IGallons);
pan4.add(EGallons);
pan5.add(Outp);
pan6.add(OutpAgain);
add(pan1);
add(pan2);
add(pan3);
add(pan4);
add(pan5);
add(pan6);
}
public void actionPerformed (ActionEvent e)
{
Miles = Integer.parseInt(EMiles.getText());
Gallons = Integer.parseInt (EGallons.getText());
EMiles.setText( "" );
EGallons.setText( "");
MPG = Miles/Gallons;
Outp.setText("MPG This Tank is " + MPG);
AMiles = AMiles + Miles;
AGallons = AGallons + Gallons;
AMPG = AMiles/AGallons;
OutpAgain.setText("TOTAL MPG is " + AMPG);
}
}
- 1 Contributor
- 0 Replies
- 82 Views
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.