I could use just some help, I have came along ways?

here is what the assignment calls for

Due to the high cost of gas we want to keep track of Miles per Gallon. Write a JAVA Application or Applet to keep track of Miles Per Gallon. Driver will input miles driven and gallons used for each tankful. The program should calculate the MPG for that tankful and also the average so far. All the average calculations should use floating point. Have the program check for or catch invalid numeric data and also divide by xeroe. Use textfields for the input and labels for the output.

Here is what I have

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class Mpg extends Applet implements ActionListener {

Label prompt1, prompt2, prompt3;
TextField input,output;
TextField input2, output2;

double miles;
double gallons;
double mpg;
int counter;
double tmpg;
double avg;


public void init()

{


prompt1 = new Label("Enter Information and Press Enter"+"
");
prompt2 = new Label("Enter Miles ");
prompt3 = new Label(" Enter Gallons ");
input = new TextField(5);
input2 = new TextField(5);
output = new TextField(20);
output2 = new TextField(20);
input.addActionListener(this);
input2.addActionListener(this);
add(prompt1);
add(prompt2);
add(input);
add(prompt3);
add(input2);
add(output);
add(output2);

}

public void actionPerformed(ActionEvent e)
{




miles = Double.parseDouble(input.getText());
++counter;
gallons = Double.parseDouble(input2.getText());

mpg = (miles/gallons);
tmpg =(mpg+mpg);
avg = (tmpg/counter);
output.setText( "MPG this tank is " + mpg);
if (counter <= 1)
{
output2.setText("Total MPG is " + mpg);
}
if(counter>1)
{
output2.setText("Total Mpg is " + avg);
}
}
//try

// catch (NumberFormatException ex) {
// outp.setText("invalid input");

}


should i be using some array or what to compute my average
or just some for loop, or what?


Comments

  • : here is what the assignment calls for
    :
    : Due to the high cost of gas we want to keep track of Miles per Gallon. Write a JAVA Application or Applet to keep track of Miles Per Gallon. Driver will input miles driven and gallons used for each tankful. The program should calculate the MPG for that tankful and also the average so far. All the average calculations should use floating point. Have the program check for or catch invalid numeric data and also divide by xeroe. Use textfields for the input and labels for the output.
    :
    : Here is what I have
    :
    : import java.awt.*;
    : import java.applet.Applet;
    : import java.awt.event.*;
    :
    : public class Mpg extends Applet implements ActionListener {
    :
    : Label prompt1, prompt2, prompt3;
    : TextField input,output;
    : TextField input2, output2;
    :
    : double miles;
    : double gallons;
    : double mpg;
    : int counter;
    : double tmpg;
    : double avg;
    :
    :
    : public void init()
    :
    : {
    :
    :
    : prompt1 = new Label("Enter Information and Press Enter"+"
    ");
    : prompt2 = new Label("Enter Miles ");
    : prompt3 = new Label(" Enter Gallons ");
    : input = new TextField(5);
    : input2 = new TextField(5);
    : output = new TextField(20);
    : output2 = new TextField(20);
    : input.addActionListener(this);
    : input2.addActionListener(this);
    : add(prompt1);
    : add(prompt2);
    : add(input);
    : add(prompt3);
    : add(input2);
    : add(output);
    : add(output2);
    :
    : }
    :
    : public void actionPerformed(ActionEvent e)
    : {
    :
    :
    :
    :
    : miles = Double.parseDouble(input.getText());
    : ++counter;
    : gallons = Double.parseDouble(input2.getText());
    :
    : mpg = (miles/gallons);
    : tmpg =(mpg+mpg);
    : avg = (tmpg/counter);
    : output.setText( "MPG this tank is " + mpg);
    : if (counter <= 1)
    : {
    : output2.setText("Total MPG is " + mpg);
    : }
    : if(counter>1)
    : {
    : output2.setText("Total Mpg is " + avg);
    : }
    : }
    : //try
    :
    : // catch (NumberFormatException ex) {
    : // outp.setText("invalid input");
    :
    : }
    :
    :
    : should i be using some array or what to compute my average
    : or just some for loop, or what?
    :
    :
    :


    Just a suggestion but since mpg = miles/gallons then in effect tmpg = mpg * 2, shouldn't tpmg = tmpg + mpg
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion