how to instantiate an obect - ?

I have a class LinkedStack (see first code snippet) but when I try to call the method to push an object onto it netbeans keeps telling me it cannot find the method?


Tks,
Owen.



public class LinkedStack 
{
    private node top;
    private int size;
    public LinkedStack()
    {
        top = null;
        size = 0;
    }
    public int size()
    {
        return size;
    }
    public boolean isEmpty()
    {
    if(top == null)
        return true;
    else return false;
    }
    public Object top() throws StackException
    {if (isEmpty())
        throw new StackException("Stack is empty");
        return top.getElement();
    }
    public void push(Object obj)
    {
    node n = new node();
    n.setElement(obj);
    n.setNext(top);
    top = n;
    size++;
    }
#####################
public class linkQueue extends javax.swing.JFrame {
 
    public linkQueue() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
 
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        empNumTextField = new javax.swing.JTextField();
        empFirstNameTextField = new javax.swing.JTextField();
        empLastNameTextField = new javax.swing.JTextField();
        empAgeTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        insertButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();
        qSizeButton = new javax.swing.JButton();
        frontButton = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Emp Number:");
 
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("First Name:");
 
        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Last Name:");
 
        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("Age:");
 
        empNumTextField.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
 
        empFirstNameTextField.setFont(new java.awt.Font("Tahoma", 1, 12));
 
        empLastNameTextField.setFont(new java.awt.Font("Tahoma", 1, 12));
 
        empAgeTextField.setFont(new java.awt.Font("Tahoma", 1, 12));
 
        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);
 
        insertButton.setLabel("Insert");
        insertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertButtonActionPerformed(evt);
            }
        });
 
        deleteButton.setText("Delete");
 
        searchButton.setText("Search");
 
        qSizeButton.setText("Q Size");
 
        frontButton.setText("Front");
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3)
                                .addComponent(jLabel4))
                            .addGap(91, 91, 91)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(empAgeTextField)
                                .addComponent(empLastNameTextField)
                                .addComponent(empFirstNameTextField)
                                .addComponent(empNumTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(insertButton, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteButton)
                        .addGap(15, 15, 15)
                        .addComponent(searchButton)
                        .addGap(15, 15, 15)
                        .addComponent(qSizeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(frontButton, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(empNumTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(empFirstNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(empLastNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(empAgeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(insertButton)
                    .addComponent(deleteButton)
                    .addComponent(searchButton)
                    .addComponent(qSizeButton)
                    .addComponent(frontButton))
                .addContainerGap(19, Short.MAX_VALUE))
        );
 
        pack();
    }// </editor-fold>
 
private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
     String inText = empFirstNameTextField.getText() + empLastNameTextField.getText() + 
            empNumTextField.getText() + empAgeTextField.getText();
     
    push(inText); // error here, please help
}
    
  
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new linkQueue().setVisible(true);
                LinkedStack myStack = new LinkedStack();
            }
            
        });
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton deleteButton;
    private javax.swing.JTextField empAgeTextField;
    private javax.swing.JTextField empFirstNameTextField;
    private javax.swing.JTextField empLastNameTextField;
    private javax.swing.JTextField empNumTextField;
    private javax.swing.JButton frontButton;
    private javax.swing.JButton insertButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton qSizeButton;
    private javax.swing.JButton searchButton;
    // End of variables declaration

Open in new window

OwenMoriartyAsked:
Who is Participating?
 
mkosbieConnect With a Mentor Commented:
You need an actual LinkedStack you're working with, then you call the method of that instance.
public class linkQueue extends javax.swing.JFrame {
    //add a LinkedStack here
    private LinkedStack stack;
 
    //add to your contructor
    public linkQueue() {
        initComponents();
        stack = new LinkedStack();
    }
 
    //
    // all the other code you already have
    //
 
// edited insertButtonActionPerformed
private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
     String inText = empFirstNameTextField.getText() + empLastNameTextField.getText() + 
            empNumTextField.getText() + empAgeTextField.getText();
     
    stack.push(inText); // error here, please help
}
 
}

Open in new window

0
 
MatthiasVanceConnect With a Mentor Commented:
You have to do something like the following in your code.

Kind regards,

Matthias Vance
// Instantiate a LinkedStack object
LinkedStack ls = new LinkedStack();
ls.push(inText);

Open in new window

0
 
OwenMoriartyAuthor Commented:
Did that but now I get cannot find symbol in three places. 1. where I name myStack a linked stack, 2. where i give it new and 3. where I try to invoke the push method.. Any suggestion. I have attached a screenshot. Please also note the way the source files are laid out, is there a problem with that?

Thank you,
Owen.
stacks-EE.JPG
0
 
OwenMoriartyAuthor Commented:
Figured out my second problem, I allowed netbeans to refactor when I moved all source files to the same directory. Tks for help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.