3 Replies - 1085 Views - Last Post: 10 November 2012 - 12:24 PM Rate Topic: -----

#1 vmia17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-November 12

Java Gas/Fuel Calculaor Error

Posted 10 November 2012 - 11:55 AM

So, I have created a java calculator, it compiles with no errors however, when I run it I get this error: Main method not found in class MPGCalculatorWindow, please define method as public static void main(String[] args).

  // This program displays the number of miles that the car may be driven per gallon of gas.
    import java.util.*;
    import javax.swing.*; // Needed for JOptionPane.
    import java.text.DecimalFormat; // Keeping proper decimal format.
    import java.awt.*; // Needed for BorderLayout class.
    import java.awt.event.*;

	  // MPG.java   Simple program to compute gas mileage

	 class MPG {



	      /**
	       *  Ask user for input, and compute result
	       */

	      public static void main(String[] args) {


	  	if (args.length!=3) {
	  	    System.out.println("Usage: java MPG start end gallons");
	  	    System.exit(0);
	  	}

	  	int start = Integer.parseInt(args[0]);
	  	int end = Integer.parseInt(args[1]);
	  	double gallons = Double.parseDouble(args[2]);

	  	int distance = end - start;
	  	double mpg = distance/gallons;

	  	System.out.println("MPG is " + mpg);

	      }

}


    public class MPGCalculatorWindow extends JFrame
    {



    private JPanel panel; // A holding panel
    private JLabel messageLabel; // A message to the user
    private JLabel messageLabel1; // A message to the user
    private JTextField TextField; // To hold user input for gallons of gas
    private JTextField TextField1;
    private JPanel ButtonPanel; // To hold the buttons
    private JButton calcButton; // To calculate miles per gallon (MPG)
    private JButton exitButton; // To exit the application
    private final int WINDOW_WIDTH = 500; // Window width
    private final int WINDOW_HEIGHT = 200; // Window height
    /**
    Constructor
    */
    public MPGCalculatorWindow()
    {
    // Set the title bar text.
    setTitle("MPG Calculator");
    // Set the size of the window.
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    // Create a BorderLayout manager to content pane.
    setLayout(new BorderLayout(13,15));
    // Specify an action for the close button.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Build the panel and add it to the frame.
    buildPanel();
    //Add the panel and add it to the frame.
    add(panel);
    // Display the window.
    setVisible(true);
    }
    /**
    The buildButtonPanel method adds a label, textfield, and buttons to a pane
    */
    private void buildPanel()
    {
    // Create a panel for the buttons.
    ButtonPanel = new JPanel();
    // Create the label.
    messageLabel = new JLabel("Enter number of miles " +
    "driven on a full tank:");
    // Create a text field.
    TextField = new JTextField(20);
    // Create the label.
    messageLabel1 = new JLabel("Enter number of gallons of gas " +
    "your car holds:");
    //Create the text field.
    TextField1 = new JTextField(20);
    // Create the buttons with captions
    calcButton = new JButton("Calculate");
    exitButton = new JButton("Exit");
    //Register the action listeners to the buttons.
    calcButton.addActionListener(new CalcButtonListener());
    exitButton.addActionListener(new ExitButtonListener());
    // Add buttons to the button panel
    ButtonPanel.add(calcButton);
    ButtonPanel.add(exitButton);
    // Create a panel object and add let the panel reference to it.
    panel = new JPanel();
    // Add the label, textfield, and button components to the panel.
    panel.add(messageLabel);
    panel.add(messageLabel1);
    panel.add(TextField);
    panel.add(TextField1);
    panel.add(calcButton);
    panel.add(exitButton);
    }
    /** Private inner class that handles the event when the user clicks any of the two buttons.
    */
    private class CalcButtonListener implements ActionListener
    {
    /**
    The actionPerformed method executes when the user clicks on the Calculate button.
    @param e the event object.
    */
    public void actionPerformed(ActionEvent e)
    {
    String input1;
    String input2;
    double total_miles;
    double gallons;
    double MPG;
    input1 = TextField.getText();
    input2 = TextField1.getText();
    total_miles = Double.parseDouble(input1);
    gallons = Double.parseDouble(input2);
    // Formula for finding the miles-per-gallon.
    MPG = total_miles/gallons;
    // Create a DecimalFormat object.
    DecimalFormat formatter = new DecimalFormat("#00.00");
    // Display the calculation
    JOptionPane.showMessageDialog(null,"The total miles that your car may be driven per gallon of gas: " +
    formatter.format(MPG));
    }
    }
    /** Private inner class that handles the event when the user clicks the Exit button.
    */
    private class ExitButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    }
    }



What I am saying is if it compiles correctly, whyam I getting this error when trying to run it in TextPad 6.0

Is This A Good Question/Topic? 0
  • +

Replies To: Java Gas/Fuel Calculaor Error

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Java Gas/Fuel Calculaor Error

Posted 10 November 2012 - 12:01 PM

You have 2 top-level classes, MPG and the public class MPGCalculatorWindow, and your main() method is in MPG. If your source file is named MPG.java, you can remove the public modifier from class MPGCalculatorWindow, and all should be well.
Was This Post Helpful? 0
  • +
  • -

#3 vmia17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-November 12

Re: Java Gas/Fuel Calculaor Error

Posted 10 November 2012 - 12:19 PM

View PostGregBrannon, on 10 November 2012 - 12:01 PM, said:

You have 2 top-level classes, MPG and the public class MPGCalculatorWindow, and your main() method is in MPG. If your source file is named MPG.java, you can remove the public modifier from class MPGCalculatorWindow, and all should be well.

And declare it as what?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Java Gas/Fuel Calculaor Error

Posted 10 November 2012 - 12:24 PM

I assume you're asking if an access modifier needs to be added to MPG. Actually, no. You can leave it blank which will result in a default access, or you can make it public. Either will "work," though I personally believe giving it public access is more correct and clearer to anyone reading the program. Your instructor may have his/her own belief about it which may not agree with mine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1