Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to instantiate an obect  - ?

Posted on 2008-10-20
4
Medium Priority
?
371 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:OwenMoriarty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Assisted Solution

by:MatthiasVance
MatthiasVance earned 400 total points
ID: 22762427
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
 
LVL 8

Accepted Solution

by:
mkosbie earned 1600 total points
ID: 22762473
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
 

Author Comment

by:OwenMoriarty
ID: 22762684
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
 

Author Closing Comment

by:OwenMoriarty
ID: 31508041
Figured out my second problem, I allowed netbeans to refactor when I moved all source files to the same directory. Tks for help.
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question