Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By ozzyman
  • 1 Post By DarrylBurke

Thread: Help with Postfix Calculator in netbeans

  1. #1
    b3222 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Help with Postfix Calculator in netbeans

    hello, i have to do an assignment with a postfix calculator but dont know where to start or how to get it working. can any1 help? heres what we got.

    Help with Postfix Calculator in netbeans-untitled.jpg

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package calculator;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    /**
     *
     * @author itmpm
     */
    
    // No Number exception for when you try to retrieve a number that isn't there
    
    class NoNumberException extends Exception {
        public NoNumberException() {
            super("There is currently no number");
        }
    }
    
    
    /*
     * NumberButton -- extends a JButton so it has a digital value.
     */
    class NumberButton extends JButton {
        double value;
    
    // Constructor -- initialize value
        public NumberButton(int n) {
            super(Integer.toString(n));
            value = n;
        }
    
    // getValue -- return the value
            public double getValue() {
                return value;
            }
    }
    
    /*
     * Helper class to build a number
     */
    class NumberBuilder {
       // This is where you add methods and other members to build a number,
       // digit by digit.
    }
    
    
    abstract class Sizer {
        public static void setSize(JButton victim) {
    //        victim.setPreferredSize(new Dimension(29,29));
            victim.setPreferredSize(new Dimension(60,45));
            victim.setMinimumSize(new Dimension(15,15));
            victim.setMaximumSize(new Dimension(70,70));
            victim.setFont(new Font("LucidaGrande", Font.PLAIN, 18));
        }
    }
    
    
    public class Calculator extends JFrame implements ActionListener {
    
        NumberBuilder myNum = new NumberBuilder();
        JLabel Display;
        NumberButton NumberButtons[];
        JButton ClearButton, ClearAllButton;
        JButton AddButton, SubtractButton, MultiplyButton,
                DivideButton, EnterButton;
        JButton ChangeSignButton, FactorialButton, PowerButton, DecimalButton;
        JLabel StackLabel, EquationLabel, MessageLabel;
    
        // Use the status bar for either the stack or the equation;
        // comment out the one you don't want.
        final String EquationPrefix = new String("Equation: ");
        final String StackPrefix = new String("Stack: ");
        final String MessagePrefix = new String(" ");
        
        
        // Constructor
        public Calculator () {
            
            // Initialize Frame
            Container container = getContentPane();
            container.setLayout(new BorderLayout());
            
            Display = new JLabel();
            Display.setText(" ");
            Font curFont = Display.getFont();
            Display.setFont(new Font(curFont.getFontName(), curFont.getStyle(), 16));
    //        System.out.println("Font is "+curFont.getFontName());
            Display.setBorder(BorderFactory.createLineBorder(Color.black));
            Display.setAlignmentY(RIGHT_ALIGNMENT);
            container.add(Display, BorderLayout.NORTH);
    
            NumberButtons = new NumberButton[10];
            
            
            // Populate NumberButtons array
            for (char i=0; (i<=9); i++) {
                NumberButtons[i] = new NumberButton(i);
                NumberButtons[i].addActionListener(this);
                Sizer.setSize(NumberButtons[i]);
            }
            
            // Initialize clear buttons
            ClearButton = new JButton("C");
            ClearButton.addActionListener(this);
            Sizer.setSize(ClearButton);
            ClearAllButton = new JButton("CA");
            ClearAllButton.addActionListener(this);
            Sizer.setSize(ClearAllButton);
    
            // Operator buttons
            AddButton = new JButton("+");
            AddButton.addActionListener(this);
            Sizer.setSize(AddButton);
            SubtractButton = new JButton("-");
            SubtractButton.addActionListener(this);
            Sizer.setSize(SubtractButton);
            MultiplyButton = new JButton("*");
            MultiplyButton.addActionListener(this);
            Sizer.setSize(MultiplyButton);
            DivideButton = new JButton("/");
            DivideButton.addActionListener(this);
            Sizer.setSize(DivideButton);
            EnterButton = new JButton("E");
            EnterButton.addActionListener(this);
            Sizer.setSize(EnterButton);
            ChangeSignButton = new JButton("+/-");
            ChangeSignButton.addActionListener(this);
            Sizer.setSize(ChangeSignButton);
            FactorialButton = new JButton("!");
            FactorialButton.addActionListener(this);
            Sizer.setSize(FactorialButton);
            PowerButton = new JButton("^");
            PowerButton.addActionListener(this);
            Sizer.setSize(PowerButton);
            DecimalButton = new JButton(".");
            DecimalButton.addActionListener(this);
            Sizer.setSize(DecimalButton);
            
    
            // create Buttons panel
            JPanel Buttons = new JPanel();
            Buttons.setLayout(new GridLayout(3,7));
    
            Buttons.add(NumberButtons[7]);
            Buttons.add(NumberButtons[8]);
            Buttons.add(NumberButtons[9]);
            Buttons.add(MultiplyButton);
            Buttons.add(DivideButton);
            Buttons.add(ClearButton);
            Buttons.add(ClearAllButton);
            Buttons.add(NumberButtons[4]);
            Buttons.add(NumberButtons[5]);
            Buttons.add(NumberButtons[6]);
            Buttons.add(AddButton);
            Buttons.add(SubtractButton);
            Buttons.add(FactorialButton);
            Buttons.add(PowerButton);
            Buttons.add(NumberButtons[1]);
            Buttons.add(NumberButtons[2]);
            Buttons.add(NumberButtons[3]);
            Buttons.add(NumberButtons[0]);
            Buttons.add(DecimalButton);
            Buttons.add(ChangeSignButton);
            Buttons.add(EnterButton);
            
    
            container.add(Buttons, BorderLayout.CENTER);
    
            //Now, add the status bar
                    //Create a status bar for y'all
            JPanel StatusBar = new JPanel(new GridLayout(3,1));
            //Set the borders of the items in the status bar
            Border loweredBevel = BorderFactory.createLoweredBevelBorder();
    
            //Add the status bar
            //Again, feel free to comment out any labels you don't need
            StackLabel = new JLabel(StackPrefix, SwingConstants.LEFT);
            StackLabel.setBorder(loweredBevel);
            EquationLabel = new JLabel(EquationPrefix, SwingConstants.LEFT);
            EquationLabel.setBorder(loweredBevel);
            MessageLabel = new JLabel(MessagePrefix, SwingConstants.LEFT);
            MessageLabel.setBorder(loweredBevel);
            StatusBar.add(StackLabel);
            StatusBar.add(EquationLabel);
            StatusBar.add(MessageLabel);
            container.add(StatusBar, BorderLayout.SOUTH);
    
    
    
            // Display that sukka
            pack();
            setTitle("Postfix Calculator");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //        setSize(250, 350);
    
            // Get the dimension of the screen
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    
            int x = (screenSize.width - getWidth())/2;
            int y = (screenSize.height - getHeight())/2;
    
            setLocation(x, y);
        }
    
       public void actionPerformed(ActionEvent ae) {
            try {
                if (ae.getSource() == NumberButtons[0]) {
                    JOptionPane.showMessageDialog(this,
                        "0");
               }
                else if (ae.getSource() == NumberButtons[1]) {
                    JOptionPane.showMessageDialog(this,
                        "Add a 1 to the number.\n"
                        + "(please, remove this dialog box!)");
               }
                else if (ae.getSource() == NumberButtons[2]) {
                    JOptionPane.showMessageDialog(this,
                        "Add a 2 to the number.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == NumberButtons[3]) {
                    JOptionPane.showMessageDialog(this,
                        "Add a 3 to the number.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == NumberButtons[4]) {
                    JOptionPane.showMessageDialog(this,
                        "Add a 4 to the number.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == NumberButtons[5]) {
                    JOptionPane.showMessageDialog(this,
                        "Add a 5 to the number.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == NumberButtons[6]) {
                    JOptionPane.showMessageDialog(this,
                        "Add a 6 to the number.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == NumberButtons[7]) {
                    JOptionPane.showMessageDialog(this,
                        "Add a 7 to the number.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == NumberButtons[8]) {
                    JOptionPane.showMessageDialog(this,
                        "Add a 8 to the number.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == NumberButtons[9]) {
                    JOptionPane.showMessageDialog(this,
                        "Add a 9 to the number.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == DecimalButton) {
                    JOptionPane.showMessageDialog(this,
                        "This enters a decimal point.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == ClearButton) {
                    JOptionPane.showMessageDialog(this,
                        "This clears the current number being entered.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == ClearAllButton) {
                    JOptionPane.showMessageDialog(this,
                        "This clears the current number being entered,\n"
                        + "and should also clear your stack.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == EnterButton) {
                        JOptionPane.showMessageDialog(this,
                            "This clears the current number being entered,\n"
                            + "and pushes it onto the stack.\n"
                            + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == AddButton) {
                    JOptionPane.showMessageDialog(this,
                        "Add the top two numbers on the stack.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == SubtractButton) {
                    JOptionPane.showMessageDialog(this,
                        "Subtract the top two numbers on the stack.\n"
                        + "(please, remove this dialog box!)");
    
                }
                else if (ae.getSource() == MultiplyButton) {
                    JOptionPane.showMessageDialog(this,
                        "Multiply the top two numbers on the stack.\n"
                        + "(please, remove this dialog box!)");
    
                }
                else if (ae.getSource() == DivideButton) {
                    JOptionPane.showMessageDialog(this,
                        "Divide the top two numbers on the stack.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == ChangeSignButton) {
                    JOptionPane.showMessageDialog(this,
                        "Change the sign of the current number.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == FactorialButton) {
                    JOptionPane.showMessageDialog(this,
                        "calculate the factorial of the number on top of the stack.\n"
                        + "(please, remove this dialog box!)");
                }
                else if (ae.getSource() == PowerButton) {
                    JOptionPane.showMessageDialog(this,
                        "Calculate powers, using the number on top of the stack\n"
                        + "as the exponent, and the next one down as the .\n"
                        + "(please, remove this dialog box!)");
                }
            }
            catch (Exception e) {
                    JOptionPane.showMessageDialog(this,
                        e.getMessage());
    
            }
        }
    }
    need to know how to make buttons work properly and do calculations. anyhelp would be appriciated.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Help with Postfix Calculator in netbeans

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Help with Postfix Calculator in netbeans

    Don't double post the same topic. The other thread you started has been closed.

    Also note that this is a forum, not a homework service. Ask a smart question and you'll get all the help you need; ask someone to earn your grade for you and you'll get nothing but derision.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    b3222 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Help with Postfix Calculator in netbeans

    well its not my fault ur last advice was shit

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Help with Postfix Calculator in netbeans

    Quote Originally Posted by b3222 View Post
    well its not my fault ur last advice was shit
    Well its not his fault your question was shit.

    Quote Originally Posted by b3222
    dont know where to start or how to get it working. can any1 help?
    At least he made an effort to find you links on the topics which he thought you need to learn to help you.

    Make an effort, ask a real question, and you may get some real advice.
    Fubarable likes this.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Help with Postfix Calculator in netbeans

    Quote Originally Posted by b3222 View Post
    well its not my fault ur last advice was shit
    You'll need more luck that anyone can reasonably hope for to get help on this or any other forum with that attitude.

    db
    Last edited by DarrylBurke; 03-13-2012 at 04:44 AM.
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Help with Postfix Calculator in netbeans

    heres what we got.
    The code appears to have been copied verbatim, author and all, from
    http://zhidao.baidu.com/question/143631506
    http://wenwen.soso.com/z/QzoneQuestion.e?sp=186160505

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help with postfix expression
    By javajavajava in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 11:11 AM
  2. Infix to Postfix using array
    By Franneldort in forum New To Java
    Replies: 0
    Last Post: 10-11-2010, 05:57 PM
  3. Postfix this!!
    By hasysf in forum New To Java
    Replies: 4
    Last Post: 09-07-2009, 06:44 PM
  4. Postfix Evaluation using a Stack, Help?
    By dalangley in forum New To Java
    Replies: 2
    Last Post: 02-16-2009, 10:43 PM
  5. Postfix-Notation
    By little_polarbear in forum New To Java
    Replies: 9
    Last Post: 09-09-2008, 04:24 PM

Posting Permissions

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