Results 1 to 7 of 7
- 03-05-2012, 11:39 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
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.

need to know how to make buttons work properly and do calculations. anyhelp would be appriciated.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()); } } }
- 03-06-2012, 04:38 AM #2
Re: Help with Postfix Calculator in netbeans
Why do they call it rush hour when nothing moves? - Robin Williams
- 03-12-2012, 11:06 PM #3
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-12-2012, 11:09 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Re: Help with Postfix Calculator in netbeans
well its not my fault ur last advice was shit
-
Re: Help with Postfix Calculator in netbeans
- 03-13-2012, 04:38 AM #6
Re: Help with Postfix Calculator in netbeans
Last edited by DarrylBurke; 03-13-2012 at 04:44 AM.
Why do they call it rush hour when nothing moves? - Robin Williams
- 03-13-2012, 04:43 AM #7
Re: Help with Postfix Calculator in netbeans
The code appears to have been copied verbatim, author and all, fromheres what we got.
http://zhidao.baidu.com/question/143631506
http://wenwen.soso.com/z/QzoneQuestion.e?sp=186160505
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Help with postfix expression
By javajavajava in forum New To JavaReplies: 3Last Post: 11-12-2010, 11:11 AM -
Infix to Postfix using array
By Franneldort in forum New To JavaReplies: 0Last Post: 10-11-2010, 05:57 PM -
Postfix this!!
By hasysf in forum New To JavaReplies: 4Last Post: 09-07-2009, 06:44 PM -
Postfix Evaluation using a Stack, Help?
By dalangley in forum New To JavaReplies: 2Last Post: 02-16-2009, 10:43 PM -
Postfix-Notation
By little_polarbear in forum New To JavaReplies: 9Last Post: 09-09-2008, 04:24 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks