Results 1 to 7 of 7

Thread: Equation program

  1. #1
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Equation program

    I am trying to write a program where the user inputs a number and presses a button and then it plugs the number into an equation and gives an answer. Here is the code I am working with. Im getting errors all over the place. any help would be great.
    Code:
    import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
    
    // panel contianing components
        public class CalculatorPanel extends JPanel{
       // declare the parts of the equasion
          private JLabel valueLabel, resultLabel, imageLabel, answerLabel;
          private JTextField posInt;
          private JButton calculateButton;
          private double solution;
          ImageIcon icon = new ImageIcon ("expression.GIF");
       
       
       // set up the GUI
           public CalculatorPanel() {
           
          // create labels
             valueLabel = new JLabel("Enter a positive value:");
             resultLabel = new JLabel("Value of expression: ");
             imageLabel = new JLabel(icon);
          
          
          
          // add text field
             posInt = new JTextField (10);
          
          // add listener and button
             calculateButton = new JButton("Calculate Expression");
             calculateButton.addActionListener (new PositiveIntListener());
          
          // add to panel
             add (imageLabel);
             add (valueLabel);
             add (posInt);
             add (calculateButton);
             add (resultLabel);
             add (answerLabel);
          
          // set size and background colo
             setPreferredSize(new Dimension(300, 150));
             setBackground(Color.green);
          }
       
       //*******************************
       // Listener functions
       //*******************************
           private class PositiveIntListener implements ActionListener
          {
          	// plug in the int into the equation
              public void actionPerformed(ActionEvent event)
             {
                int integer;
                integer = Integer.parseInt (text);
                double result = Math.sqrt (Math.abs( 3* Math.pow((Integer), 5) - 12* Math.pow((Integer), 4) - 9* Math.pow((Integer), 2) + 2*Integer));
                answerLabel.setText(result);
             }
          }
       }
    I have the driver program and im pretty sure it works. the 1st error im getting is around the integer = Integer.parseInt....

    ANy advice will be great.

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Based on my limited Java knowledge I think there are some issues in the source code you have furnished in your posting.

    1.
    Code:
    public class CalculatorPanel extends JPanel
    There is no main method in this class.

    2.
    Code:
    integer = Integer.parseInt (text);
    I wonder where you've declared the variable text. What is it?

    3.
    Code:
    double result = Math.sqrt (Math.abs( 3* Math.pow((Integer), 5) - 12* Math.pow((Integer), 4) - 9* Math.pow((Integer), 2) + 2*Integer));
    Java is case sensitive you used Integer (java.lang.Integer) instead of integer (small case letter) in the above expression.

  3. #3
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    OK. 2 and 3 were right. Main is in my driver script. I fixed those 2 things and now im getting the error

    edit--------------
    i got it to compile but its not working. everything else is the same except this
    Code:
       int integer;
                String text = posInt.getText();
                integer = Integer.parseInt (text);
                double result = Math.sqrt (Math.abs( 3* Math.pow((integer), 5) - 12* Math.pow((integer), 4) - 9* Math.pow((integer), 2) + 2*integer));
                answerLabel.setText(Double.toString(result));
    Last edited by craigtb; 02-07-2007 at 05:37 AM.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I'm surprised you got it to compile. Integer.parseInt() can throw a NumberFormatException, which needs to be caught at some point.

    "Not working" isn't an adequate description of a problem. What's the output? What's the expected output? What was the original equation? If no output, what happens instead?

    As codeexploiter said, you haven't defined a public static void main(String[]). Are you trying to call the class directly? If so, that won't work without a main().
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok this is the error i am getting when i try and run it.
    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1019)
    at java.awt.Container.add(Container.java:351)
    at CalculatorPanel.<init>(CalculatorPanel.java:38)
    at Project4b.main(Project4b.java:15)
    ANd here is my code.
    Code:
     import javax.swing.*;
    // main driver class
       import javax.swing.JFrame;
    
        public class Project4b
       {
       //---------
       // creates the main program frame
       //---------
           public static void main(String[]args)
          {
             JFrame frame = new JFrame("Calculate Expression");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
             frame.getContentPane().add(new CalculatorPanel());
          
             frame.pack();
             frame.setVisible(true);
          }
       }
    and
    Code:
       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
    
    // panel contianing components
        public class CalculatorPanel extends JPanel{
       // declare the parts of the equasion
          private JLabel valueLabel, resultLabel, imageLabel, answerLabel;
          private JTextField posInt;
          private JButton calculateButton;
          private double solution;
          ImageIcon icon = new ImageIcon ("expression.GIF");
       
       
       // set up the GUI
           public CalculatorPanel() {
           
          // create labels
             valueLabel = new JLabel("Enter a positive value:");
             resultLabel = new JLabel("Value of expression: ");
             imageLabel = new JLabel(icon);
          
          
          
          // add text field
             posInt = new JTextField (10);
          
          // add listener and button
             calculateButton = new JButton("Calculate Expression");
             calculateButton.addActionListener (new PositiveIntListener());
          
          // add to panel
             add (imageLabel);
             add (valueLabel);
             add (posInt);
             add (calculateButton);
             add (resultLabel);
             add (answerLabel);
          
          // set size and background colo
             setPreferredSize(new Dimension(300, 150));
             setBackground(Color.green);
          }
       
       //*******************************
       // Listener functions
       //*******************************
           private class PositiveIntListener implements ActionListener
          {
          	// plug in the int into the equation
              public void actionPerformed(ActionEvent event)
             {
                int integer;
                String text = posInt.getText();
                integer = Integer.parseInt (text);
                double result = Math.sqrt (Math.abs( 3* Math.pow((integer), 5) - 12* Math.pow((integer), 4) - 9* Math.pow((integer), 2) + 2*integer));
                answerLabel.setText(Double.toString(result));
             }
          }
       }
    sorry i was very undescript in my last post, i was in a bit of a rush.

    and the output needs to have a picture being displayed, a box for the user to type in, a button, then the answer after its calculated.

    thanks

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    NullPointerException is the segfault of the Java world: in a complex application, it usually means long hours of caffeine-fuelled debugging await, and the sight of it sends grown men crying in fear

    Well, OK, maybe not quite that bad, but you get the idea.

    In your application, thankfully, it's simple: answerLabel hasn't been initialised at the point where you try to add it to the panel.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I am confused by what your saying. What needs to be done to initialize it?

    ***edit***
    Nevermind i understand what you are saying. THANKS!!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •