Reverse Polish Notation calculator

Discussion in 'Java' started by matrix584@gmail.com, Feb 6, 2006.

  1. Guest

    I gotta make another class to use this, and i have no idea how...any
    ideas

    (please dont post the answer, i just need a little guidance)

    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.html.*;
    import javax.swing.border.*;


    class HPCalculatorApp
    extends JFrame
    implements ActionListener
    {

    private JMenuBar menu;
    private JMenuItem fileExit;

    private static final int
    ZERO = 0,
    ONE = 1,
    TWO = 2,
    THREE = 3,
    FOUR = 4,
    FIVE = 5,
    SIX = 6,
    SEVEN = 7,
    EIGHT = 8,
    NINE = 9,
    SIGN = 10,
    DOT = 11,
    ADD = 12,
    SUBTRACT = 13,
    MULTIPLY = 14,
    DIVIDE = 15,
    CLEAR = 16,
    BACK = 17,
    STORE = 18,
    RESTORE = 19,
    ENTER = 20,
    NUM_BUTTONS = 21;

    private static final String[] LABELS = {
    "+/-", ".", "+", "-", "*", "/", "C", "<-", "MS", "MR", "ENTER"
    };

    private JButton[] bButtons;
    private JLabel visor;
    private Font font;

    private HPCalculator hp;
    private String number;
    private boolean entered;

    public HPCalculatorApp()
    {
    hp = new HPCalculator();
    number = new String();
    entered = false;

    font = new Font("SansSerif", Font.PLAIN, 12);

    setTitle( "HP Calculator" );
    setSize(180, 240);
    BorderLayout mainLayout = new BorderLayout();
    getContentPane().setLayout(mainLayout);
    mainLayout.setVgap(1);

    // Create visor
    visor = new JLabel("", SwingConstants.RIGHT);
    visor.setFont(font);
    visor.setPreferredSize(new Dimension(100, 20));
    visor.setBorder(new BevelBorder(BevelBorder.LOWERED));
    visor.setBackground(Color.white);
    getContentPane().add(visor, BorderLayout.NORTH);

    // Create buttons
    int WIDTH = 30;
    int HEIGHT = 30;
    bButtons = new JButton[NUM_BUTTONS];
    for (int i=0; i<NUM_BUTTONS-1; i++) {
    bButtons = new JButton(Integer.toString(i));
    bButtons.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    bButtons.setForeground(Color.blue);
    bButtons.setFont(font);
    bButtons.setMargin(new Insets(1, 1, 1, 1));
    bButtons.addActionListener(this);
    }

    for (int i=SIGN; i<=ENTER; i++) {
    bButtons = new JButton(LABELS[i-SIGN]);
    bButtons.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    bButtons.setForeground(Color.red);
    bButtons.setFont(font);
    bButtons.setMargin(new Insets(1, 1, 1, 1));
    bButtons.addActionListener(this);
    }

    bButtons[SIGN].setForeground(Color.blue);
    bButtons[DOT].setForeground(Color.blue);

    bButtons[ENTER].setPreferredSize(new Dimension(100, 18));
    bButtons[ENTER].setForeground(Color.white);
    bButtons[ENTER].setBackground(new Color(155, 155, 155));
    bButtons[ENTER].setMargin(new Insets(1, 1, 1, 1));

    int VGAP = 1, HGAP = 1;
    JPanel centerPanel = new JPanel();
    GridLayout grid = new GridLayout(4, 1);
    grid.setHgap(HGAP);
    grid.setVgap(VGAP);
    centerPanel.setLayout(grid);

    JPanel row1 = new JPanel();
    JPanel row2 = new JPanel();
    JPanel row3 = new JPanel();
    JPanel row4 = new JPanel();
    JPanel row5 = new JPanel();

    FlowLayout defLayout;
    defLayout = new FlowLayout();
    defLayout.setHgap(HGAP);
    defLayout.setVgap(VGAP);

    row1.setLayout(defLayout);
    row1.add(bButtons[NINE]);
    row1.add(bButtons[EIGHT]);
    row1.add(bButtons[SEVEN]);
    row1.add(bButtons[ADD]);
    row1.add(bButtons[BACK]);
    centerPanel.add(row1);

    defLayout = new FlowLayout();
    defLayout.setHgap(HGAP);
    defLayout.setVgap(VGAP);
    row2.setLayout(defLayout);
    row2.add(bButtons[SIX]);
    row2.add(bButtons[FIVE]);
    row2.add(bButtons[FOUR]);
    row2.add(bButtons[SUBTRACT]);
    row2.add(bButtons[CLEAR]);
    centerPanel.add(row2);

    defLayout = new FlowLayout();
    defLayout.setHgap(HGAP);
    defLayout.setVgap(VGAP);
    row3.setLayout(defLayout);
    row3.add(bButtons[THREE]);
    row3.add(bButtons[TWO]);
    row3.add(bButtons[ONE]);
    row3.add(bButtons[MULTIPLY]);
    row3.add(bButtons[STORE]);
    centerPanel.add(row3);

    defLayout = new FlowLayout();
    defLayout.setHgap(HGAP);
    defLayout.setVgap(VGAP);
    row4.setLayout(defLayout);
    row4.add(bButtons[ZERO]);
    row4.add(bButtons[SIGN]);
    row4.add(bButtons[DOT]);
    row4.add(bButtons[DIVIDE]);
    row4.add(bButtons[RESTORE]);
    centerPanel.add(row4);

    defLayout = new FlowLayout();
    defLayout.setHgap(HGAP);
    defLayout.setVgap(VGAP);
    row5.setLayout(defLayout);
    row5.add(bButtons[ENTER]);

    centerPanel.add(row5);

    getContentPane().add(centerPanel, BorderLayout.CENTER);
    getContentPane().add(row5, BorderLayout.SOUTH);

    // Create menu
    menu = new JMenuBar();
    setJMenuBar(menu);

    fileExit = new JMenuItem("Exit");
    fileExit.setFont(font);
    fileExit.addActionListener(this);
    menu.add(fileExit);

    pack();

    //Anonymous inner class to terminate program.
    this.addWindowListener( new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit( 0 );
    }
    } );//end addWindowListener
    }

    public void actionPerformed(ActionEvent e)
    {
    if (e.getSource() instanceof JMenuItem) {
    JMenuItem i = (JMenuItem)e.getSource();
    if (i == fileExit)
    System.exit(0);
    }

    if (e.getSource() instanceof JButton) {
    if (processNumber(e.getSource()))
    return;
    if (processOperation(e.getSource()))
    return;
    if (processClear(e.getSource()))
    return;
    if (processMemory(e.getSource()))
    return;
    if (processEnter(e.getSource()))
    return;

    }
    }

    public static void main(String args[])
    {
    // Create an instance of the test application
    HPCalculatorApp mainFrame = new HPCalculatorApp();
    mainFrame.setVisible(true);
    }

    private void backspace()
    {
    if (number.length() == 0)
    return;
    if (entered) {
    clearNumber();
    return;
    }
    number = number.substring(0, number.length()-1);
    if (number.length() == 1 && number.startsWith("-"))
    clearNumber();
    else if (number.length() == 2 && number.startsWith("-0"))
    clearNumber();
    else if (number.length() == 1 && number.startsWith("0"))
    clearNumber();
    }

    private void clearVisor()
    {
    clearNumber();
    updateVisor();
    }

    private void clearNumber()
    {
    number = "";
    }

    private void updateVisor()
    {
    visor.setText(number);
    }

    private double getNumber(String n)
    {
    double num;
    try {
    num = (double)Double.parseDouble(n);
    }
    catch (NumberFormatException e) {
    return 0.0;
    }
    return num;
    }

    private boolean checkNumber(String n)
    {
    try {
    Double.parseDouble(n);
    }
    catch (NumberFormatException e) {
    return false;
    }
    return true;
    }

    private boolean addToNumber(int i)
    {
    if (entered == true || number.length() == 0) {
    entered = false;
    if (i != 0)
    number = Integer.toString(i);
    else
    number = "0.";
    return true;
    }

    String n = new String(number);
    n += i;
    if (!checkNumber(n))
    return false;
    number = n;
    entered = false;
    return true;
    }

    private void toNumber(double n)
    {
    number = Double.toString(n);
    }

    private boolean invertSign()
    {
    if (number.length() == 0)
    return false;

    if (number.startsWith("-"))
    number = number.substring(1);
    else
    number = "-" + number;
    entered = false;
    return true;
    }

    private boolean addDot()
    {
    if (number.length() == 0) {
    entered = false;
    number = "0.";
    return true;
    }

    if (number.indexOf('.') != -1)
    return false;

    number += '.';
    //?? entered = false;
    return true;
    }

    private boolean processNumber(Object o)
    {
    for (int i=ZERO; i<=NINE; i++) {
    if (bButtons == (JButton)o) {
    if (addToNumber(i)) {
    updateVisor();
    return true;
    }
    }
    }
    if (bButtons[SIGN] == (JButton)o) {
    if (invertSign()) {
    updateVisor();
    return true;
    }
    }
    if (bButtons[DOT] == (JButton)o) {
    if (addDot()) {
    updateVisor();
    return true;
    }
    }

    return false;
    }

    private void add()
    {
    if (!entered)
    enter();
    try {
    hp.add();
    }
    catch (Error e) {
    return;
    }
    toNumber(hp.result());
    updateVisor();
    entered = true;
    }

    private void subtract()
    {
    if (!entered)
    enter();
    try {
    hp.subtract();
    }
    catch (Error e) {
    return;
    }
    toNumber(hp.result());
    updateVisor();
    entered = true;
    }

    private void divide()
    {
    if (!entered)
    enter();
    try {
    hp.divide();
    }
    catch (Error e) {
    return;
    }
    toNumber(hp.result());
    updateVisor();
    entered = true;
    }

    private void multiply()
    {
    if (!entered)
    enter();
    try {
    hp.multiply();
    }
    catch (Error e) {
    return;
    }
    toNumber(hp.result());
    updateVisor();
    entered = true;
    }

    private boolean processOperation(Object o)
    {
    if (bButtons[ADD] == (JButton)o) {
    add();
    return true;
    }
    else if (bButtons[SUBTRACT] == (JButton)o) {
    subtract();
    return true;
    }
    else if (bButtons[DIVIDE] == (JButton)o) {
    divide();
    return true;
    }
    else if (bButtons[MULTIPLY] == (JButton)o) {
    multiply();
    return true;
    }
    return false;
    }

    private boolean processClear(Object o)
    {
    if (bButtons[CLEAR] == (JButton)o) {
    clearVisor();
    return true;
    }
    else if (bButtons[BACK] == (JButton)o) {
    backspace();
    updateVisor();
    return true;
    }
    return false;
    }

    private boolean processMemory(Object o)
    {
    if (!checkNumber(number))
    return false;

    double n;
    if (bButtons[STORE] == (JButton)o) {
    n = getNumber(number);
    hp.store(n);
    return true;
    }
    else if (bButtons[RESTORE] == (JButton)o) {
    n = hp.restore();
    toNumber(n);
    updateVisor();
    entered = false;
    return true;
    }
    return false;
    }

    private boolean enter()
    {
    if (!checkNumber(number))
    return false;
    hp.enter(getNumber(number));
    entered = true;
    return true;
    }

    private boolean processEnter(Object o)
    {
    if (bButtons[ENTER] == (JButton)o) {
    return enter();
    }
    return false;
    }
    }
     
    , Feb 6, 2006
    #1
    1. Advertising

  2. Roedy Green Guest

    On 5 Feb 2006 20:10:12 -0800, ""
    <> wrote, quoted or indirectly quoted someone who
    said :

    >I gotta make another class to use this, and i have no idea how...any
    >ideas


    I won't even look at your class. Here is the generic answer.

    If your class has a main method, see
    http://mindprod.com/jgloss/main.html
    for the fine print, then you can run it from the command line or
    simulate a command line with the args[] parameter.

    If the class has public static methods, you can use them. Just give
    them suitable parameters.

    To use the public instance (non-static) methods, you must find a
    constructor, and use new to construct an object of that class. then
    you can call the public instance methods on it.

    see http://mindprod.com/jgloss/static.html
    http://mindprod.com/jgloss/instance.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Feb 6, 2006
    #2
    1. Advertising

  3. Oliver Wong Guest

    "Roedy Green" <> wrote in
    message news:...
    > On 5 Feb 2006 20:10:12 -0800, ""
    > <> wrote, quoted or indirectly quoted someone who
    > said :
    >
    >>I gotta make another class to use this, and i have no idea how...any
    >>ideas

    >
    > I won't even look at your class.


    I didn't read the OP's code either. It's much too long, and I quickly
    got bored with it. Read http://mindprod.com/jgloss/sscce.html, especially
    the part about "short":

    <quote>
    Try to prune away anything not strictly relevant to the problem. In the
    process of doing this, you often solve your own problem. If you don't prune,
    You are making the task unnecessarily difficult for those you want to help
    you by clouding the issue with irrelevancies, or hiding relevancies. If you
    don't prune, you are implicitly saying "my time is valuable and yours is
    not.", hardly a winning attitude to solicit help. Purely out of self
    interest you should prune as much as practical, because if you ask only a
    small favour, that minimally inconveniences, irritates or frustrates
    another, your chances of someone granting it are much higher. You will also
    get more responses, which increases your odds of getting a helpful, correct
    response.
    </quote>

    Also, the OP should state what the exact problem is. That is, (s)he
    should say what (s)he expects the program to do, and contrasts it with what
    the program actually does.

    - Oliver
     
    Oliver Wong, Feb 6, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ron
    Replies:
    1
    Views:
    907
    Stewart Gordon
    Jul 2, 2003
  2. dogbite
    Replies:
    4
    Views:
    716
    osmium
    Oct 10, 2003
  3. zrebecca
    Replies:
    1
    Views:
    903
    zrebecca
    Mar 7, 2011
  4. zrebecca
    Replies:
    1
    Views:
    392
    zrebecca
    Mar 7, 2011
  5. matt
    Replies:
    5
    Views:
    141
Loading...

Share This Page