first class GUI -
basically trying to use my stack calculation class to use in my first class GUI
in my enter postfix expression actionPerformed area? What is the best step to
add my stack calculation to evaluate an expression?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

import java.util.LinkedList;
import java.util.Scanner;
public class PostfixCalculator extends JFrame 
{
    JButton enterButton = new JButton ("Enter Postfix Expression");
    JButton helpButton = new JButton ("Help");
    JButton quitButton = new JButton ("Quit");


    public static void main()
    {
        PostfixCalculator myWindow = new PostfixCalculator();
        myWindow.setSize(250,110);
        myWindow.setTitle("Postfix Calculator:");
        myWindow.createGUI();
        myWindow.setVisible(true);
    }

    private void createGUI()
    {
        Container window = getContentPane();
        window.setLayout (new FlowLayout());
        enterButton.addActionListener(new EnterAction());
        window.add(enterButton);
        helpButton.addActionListener(new HelpAction());
        window.add(helpButton);
        quitButton.addActionListener(new QuitAction());
        window.add(quitButton);
    }

    private class EnterAction implements ActionListener
    {
        public void actionPerformed(ActionEvent e)  {   

} 
   } 

    private class HelpAction implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            // explain what it is
            JOptionPane.showMessageDialog(null, "Explanations");
        }
    }

    private class QuitAction implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }
}

second class

import javax.swing.*;
public class StackArray 
{
    private Object stack[];
    private int sizeIndex = 5;
    private int topPtr;

    public StackArray()
    {
        stack = new Object[sizeIndex];  //What is object?
    }

    public void push(Object stackItem)
    { 
        if(!isFull())
        {
            stack[topPtr] = stackItem;  //What is Ptr stands for in topPtr?
            topPtr++;       // why topPtr++?
        }    
        else
        {
            JOptionPane.showMessageDialog(null,"Stack is Full!","Error",JOptionPane.ERROR_MESSAGE);
        }      
    }

    public Object pop()
    {
        Object x = null;      // why x is initially null?
        if(!isEmpty())
        {
            x = stack[--topPtr];    //Why --topPtr?
            stack[topPtr] = null;
        }
        return x;           //Why we return to x?
    }

    public Object top()     //What is ts stands for?
    {   
        Object ts = null;       //Why null?
        if(!isEmpty())
        {
            ts = stack[topPtr - 1];  //Why we subtract 1 from topPtr?
        }
        return ts;          //why we return to ts?
    }

    public boolean isEmpty()
    {
        return topPtr == 0;
    }

    public boolean isFull()
    {
        return topPtr == sizeIndex;     
    }


    public boolean contains(Object stackItem)
    {
         boolean contains = false;  //Why false, not rue?
         if(!isEmpty())
         {
            for(int i = 0; i < sizeIndex; i++)   //meaning of this
            {
             if(stack[i].equals(stackItem))
             {
                contains = true;     //Why true?
                break;
             }
            }
         }
         return contains;       //Why return to contains?
    }

    public String toString()    //What is tostring and purpose?
    {
        StringBuffer display = new StringBuffer();  //what is StringBuffer?

        display.append("{");    //What is append and purpose of this "{"?
            for(int i = 0; i< sizeIndex; i++)
            {
                if(stack[i] != null)
                {
                    display.append(stack[i]);
                    if( i < topPtr - 1)  //Why we subtract 1
                    {
                        display.append(",");
                    }
                }
            }

        display.append("}");
        return display.toString();
    }
}

Assuming you have somewhere to enter a postfix expression, use the postfix algorithm to evaluate it.

Are you also looking for answers to your questions in StackArray?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.