Solved

how to instantiate an obect  - ?

Posted on 2008-10-20
4
355 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
  • 2
4 Comments
 
LVL 8

Assisted Solution

by:MatthiasVance
MatthiasVance earned 100 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 400 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now